1+ package cloudflare
2+
3+ import (
4+ "context"
5+ "fmt"
6+ "net/http"
7+ "testing"
8+
9+ "github.com/stretchr/testify/assert"
10+ )
11+
12+ func TestListFallbackDomain (t * testing.T ) {
13+ setup ()
14+ defer teardown ()
15+
16+ handler := func (w http.ResponseWriter , r * http.Request ) {
17+ assert .Equal (t , http .MethodGet , r .Method , "Expected method 'GET', got %s" , r .Method )
18+ w .Header ().Set ("content-type" , "application/json" )
19+ fmt .Fprintf (w , `
20+ {
21+ "success": true,
22+ "errors": [],
23+ "messages": [],
24+ "result": [
25+ {
26+ "suffix": "example.com",
27+ "description": "Domain bypass for local development"
28+ }
29+ ]
30+ }
31+ ` )
32+ }
33+
34+ want := []FallbackDomain {{
35+ Suffix : "example.com" ,
36+ Description : "Domain bypass for local development" ,
37+ }}
38+
39+ mux .HandleFunc ("/accounts/" + testAccountID + "/devices/policy/fallback_domains" , handler )
40+
41+ actual , err := client .ListFallbackDomains (context .Background (), testAccountID )
42+
43+ if assert .NoError (t , err ) {
44+ assert .Equal (t , want , actual )
45+ }
46+ }
47+
48+ func TestFallbackDomainDNSServer (t * testing.T ) {
49+ setup ()
50+ defer teardown ()
51+
52+ handler := func (w http.ResponseWriter , r * http.Request ) {
53+ assert .Equal (t , http .MethodGet , r .Method , "Expected method 'GET', got %s" , r .Method )
54+ w .Header ().Set ("content-type" , "application/json" )
55+ fmt .Fprintf (w , `
56+ {
57+ "success": true,
58+ "errors": [],
59+ "messages": [],
60+ "result": [
61+ {
62+ "suffix": "example.com",
63+ "description": "Domain bypass for local development",
64+ "dns_server": "['192.168.0.1', '10.1.1.1']"
65+ }
66+ ]
67+ }
68+ ` )
69+ }
70+
71+ want := []FallbackDomain {{
72+ Suffix : "example.com" ,
73+ Description : "Domain bypass for local development" ,
74+ DNSServer : "['192.168.0.1', '10.1.1.1']" ,
75+ }}
76+
77+ mux .HandleFunc ("/accounts/" + testAccountID + "/devices/policy/fallback_domains" , handler )
78+
79+ actual , err := client .ListFallbackDomains (context .Background (), testAccountID )
80+
81+ if assert .NoError (t , err ) {
82+ assert .Equal (t , want , actual )
83+ }
84+ }
85+
86+ func TestUpdateFallbackDomain (t * testing.T ) {
87+ setup ()
88+ defer teardown ()
89+
90+ handler := func (w http.ResponseWriter , r * http.Request ) {
91+ assert .Equal (t , http .MethodPut , r .Method , "Expected method 'PUT', got %s" , r .Method )
92+ w .Header ().Set ("content-type" , "application/json" )
93+ fmt .Fprintf (w , `
94+ {
95+ "success": true,
96+ "errors": [],
97+ "messages": [],
98+ "result": [
99+ {
100+ "suffix": "example_one.com",
101+ "description": "example one",
102+ "dns_server": "['192.168.0.1', '10.1.1.1']"
103+ },
104+ {
105+ "suffix": "example_two.com",
106+ "description": "example two"
107+ },
108+ {
109+ "suffix": "example_three.com",
110+ "description": "example three"
111+ }
112+ ]
113+ }
114+ ` )
115+ }
116+
117+ domains := []FallbackDomain {
118+ {
119+ Suffix : "example_one.com" ,
120+ Description : "example one" ,
121+ DNSServer : "['192.168.0.1', '10.1.1.1']" ,
122+ },
123+ {
124+ Suffix : "example_two.com" ,
125+ Description : "example two" ,
126+ },
127+ {
128+ Suffix : "example_three.com" ,
129+ Description : "example three" ,
130+ },
131+ }
132+
133+ mux .HandleFunc ("/accounts/" + testAccountID + "/devices/policy/fallback_domains" , handler )
134+
135+ actual , err := client .UpdateFallbackDomain (context .Background (), testAccountID , domains )
136+
137+ if assert .NoError (t , err ) {
138+ assert .Equal (t , domains , actual )
139+ }
140+ }
0 commit comments