1package cloudflare
2
3import (
4	"context"
5	"fmt"
6	"net/http"
7	"testing"
8	"time"
9
10	"github.com/stretchr/testify/assert"
11)
12
13var argoTimestamp, _ = time.Parse(time.RFC3339Nano, "2019-02-20T22:37:07.107449Z")
14
15func TestArgoSmartRouting(t *testing.T) {
16	setup()
17	defer teardown()
18
19	handler := func(w http.ResponseWriter, r *http.Request) {
20		assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
21		w.Header().Set("content-type", "application/json")
22		fmt.Fprintf(w, `{
23			"success": true,
24			"errors": [],
25			"messages": [],
26			"result": {
27				"id": "smart_routing",
28				"value": "on",
29				"editable": true,
30				"modified_on": "2019-02-20T22:37:07.107449Z"
31			}
32		}
33		`)
34	}
35
36	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/argo/smart_routing", handler)
37	want := ArgoFeatureSetting{
38		ID:         "smart_routing",
39		Value:      "on",
40		Editable:   true,
41		ModifiedOn: argoTimestamp,
42	}
43
44	actual, err := client.ArgoSmartRouting(context.Background(), "01a7362d577a6c3019a474fd6f485823")
45
46	if assert.NoError(t, err) {
47		assert.Equal(t, want, actual)
48	}
49}
50
51func TestUpdateArgoSmartRouting(t *testing.T) {
52	setup()
53	defer teardown()
54
55	handler := func(w http.ResponseWriter, r *http.Request) {
56		assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)
57		w.Header().Set("content-type", "application/json")
58		fmt.Fprintf(w, `{
59			"success": true,
60			"errors": [],
61			"messages": [],
62			"result": {
63				"id": "smart_routing",
64				"value": "off",
65				"editable": true,
66				"modified_on": "2019-02-20T22:37:07.107449Z"
67			}
68		}
69		`)
70	}
71
72	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/argo/smart_routing", handler)
73	want := ArgoFeatureSetting{
74		ID:         "smart_routing",
75		Value:      "off",
76		Editable:   true,
77		ModifiedOn: argoTimestamp,
78	}
79
80	actual, err := client.UpdateArgoSmartRouting(context.Background(), "01a7362d577a6c3019a474fd6f485823", "off")
81
82	if assert.NoError(t, err) {
83		assert.Equal(t, want, actual)
84	}
85}
86
87func TestUpdateArgoSmartRoutingWithInvalidValue(t *testing.T) {
88	setup()
89	defer teardown()
90
91	_, err := client.UpdateArgoSmartRouting(context.Background(), "01a7362d577a6c3019a474fd6f485823", "notreal")
92
93	if assert.Error(t, err) {
94		assert.Equal(t, "invalid setting value 'notreal'. must be 'on' or 'off'", err.Error())
95	}
96}
97
98func TestArgoTieredCaching(t *testing.T) {
99	setup()
100	defer teardown()
101
102	handler := func(w http.ResponseWriter, r *http.Request) {
103		assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
104		w.Header().Set("content-type", "application/json")
105		fmt.Fprintf(w, `{
106			"success": true,
107			"errors": [],
108			"messages": [],
109			"result": {
110				"id": "tiered_caching",
111				"value": "on",
112				"editable": true,
113				"modified_on": "2019-02-20T22:37:07.107449Z"
114			}
115		}
116		`)
117	}
118
119	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/argo/tiered_caching", handler)
120	want := ArgoFeatureSetting{
121		ID:         "tiered_caching",
122		Value:      "on",
123		Editable:   true,
124		ModifiedOn: argoTimestamp,
125	}
126
127	actual, err := client.ArgoTieredCaching(context.Background(), "01a7362d577a6c3019a474fd6f485823")
128
129	if assert.NoError(t, err) {
130		assert.Equal(t, want, actual)
131	}
132}
133
134func TestUpdateArgoTieredCaching(t *testing.T) {
135	setup()
136	defer teardown()
137
138	handler := func(w http.ResponseWriter, r *http.Request) {
139		assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)
140		w.Header().Set("content-type", "application/json")
141		fmt.Fprintf(w, `{
142			"success": true,
143			"errors": [],
144			"messages": [],
145			"result": {
146				"id": "tiered_caching",
147				"value": "off",
148				"editable": true,
149				"modified_on": "2019-02-20T22:37:07.107449Z"
150			}
151		}
152		`)
153	}
154
155	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/argo/tiered_caching", handler)
156	want := ArgoFeatureSetting{
157		ID:         "tiered_caching",
158		Value:      "off",
159		Editable:   true,
160		ModifiedOn: argoTimestamp,
161	}
162
163	actual, err := client.UpdateArgoTieredCaching(context.Background(), "01a7362d577a6c3019a474fd6f485823", "off")
164
165	if assert.NoError(t, err) {
166		assert.Equal(t, want, actual)
167	}
168}
169
170func TestUpdateArgoTieredCachingWithInvalidValue(t *testing.T) {
171	setup()
172	defer teardown()
173
174	_, err := client.UpdateArgoTieredCaching(context.Background(), "01a7362d577a6c3019a474fd6f485823", "notreal")
175
176	if assert.Error(t, err) {
177		assert.Equal(t, "invalid setting value 'notreal'. must be 'on' or 'off'", err.Error())
178	}
179}
180