github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/dns/recordsets_test.go (about) 1 package dns 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestDNS_GetRecordSets(t *testing.T) { 15 tests := map[string]struct { 16 zone string 17 args []RecordSetQueryArgs 18 responseStatus int 19 responseBody string 20 expectedPath string 21 expectedResponse *RecordSetResponse 22 withError error 23 }{ 24 "200 OK": { 25 zone: "example.com", 26 args: []RecordSetQueryArgs{}, 27 responseStatus: http.StatusOK, 28 responseBody: ` 29 { 30 "metadata": { 31 "zone": "example.com", 32 "page": 1, 33 "pageSize": 25, 34 "totalElements": 2, 35 "types": [ 36 "A" 37 ] 38 }, 39 "recordsets": [ 40 { 41 "name": "www.example.com", 42 "type": "A", 43 "ttl": 300, 44 "rdata": [ 45 "10.0.0.2", 46 "10.0.0.3" 47 ] 48 } 49 ] 50 }`, 51 expectedPath: "/config-dns/v2/zones/example.com/recordsets", 52 expectedResponse: &RecordSetResponse{ 53 Metadata: Metadata{ 54 LastPage: 0, 55 Page: 1, 56 PageSize: 25, 57 ShowAll: false, 58 TotalElements: 2, 59 }, 60 RecordSets: []RecordSet{ 61 { 62 Name: "www.example.com", 63 Type: "A", 64 TTL: 300, 65 Rdata: []string{"10.0.0.2", "10.0.0.3"}, 66 }, 67 }, 68 }, 69 }, 70 "500 internal server error": { 71 zone: "example.com", 72 args: []RecordSetQueryArgs{}, 73 responseStatus: http.StatusInternalServerError, 74 responseBody: ` 75 { 76 "type": "internal_error", 77 "title": "Internal Server Error", 78 "detail": "Error fetching authorities", 79 "status": 500 80 }`, 81 expectedPath: "/config-dns/v2/zones/example.com/recordsets", 82 withError: &Error{ 83 Type: "internal_error", 84 Title: "Internal Server Error", 85 Detail: "Error fetching authorities", 86 StatusCode: http.StatusInternalServerError, 87 }, 88 }, 89 } 90 91 for name, test := range tests { 92 t.Run(name, func(t *testing.T) { 93 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 94 assert.Equal(t, test.expectedPath, r.URL.String()) 95 assert.Equal(t, http.MethodGet, r.Method) 96 w.WriteHeader(test.responseStatus) 97 _, err := w.Write([]byte(test.responseBody)) 98 assert.NoError(t, err) 99 })) 100 client := mockAPIClient(t, mockServer) 101 result, err := client.GetRecordSets(context.Background(), test.zone, test.args...) 102 if test.withError != nil { 103 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 104 return 105 } 106 require.NoError(t, err) 107 assert.Equal(t, test.expectedResponse, result) 108 }) 109 } 110 } 111 112 func TestDNS_CreateRecordSets(t *testing.T) { 113 tests := map[string]struct { 114 zone string 115 sets *RecordSets 116 responseStatus int 117 responseBody string 118 expectedPath string 119 expectedResponse *RecordSetResponse 120 withError error 121 }{ 122 "200 OK": { 123 zone: "example.com", 124 sets: &RecordSets{ 125 []RecordSet{ 126 { 127 Name: "www.example.com", 128 Type: "A", 129 TTL: 300, 130 Rdata: []string{ 131 "10.0.0.2", 132 "10.0.0.3", 133 }, 134 }, 135 }, 136 }, 137 responseStatus: http.StatusNoContent, 138 expectedPath: "/config-dns/v2/zones/example.com/recordsets", 139 }, 140 "500 internal server error": { 141 zone: "example.com", 142 sets: &RecordSets{ 143 []RecordSet{ 144 { 145 Name: "www.example.com", 146 Type: "A", 147 TTL: 300, 148 Rdata: []string{ 149 "10.0.0.2", 150 "10.0.0.3", 151 }, 152 }, 153 }, 154 }, 155 responseStatus: http.StatusInternalServerError, 156 responseBody: ` 157 { 158 "type": "internal_error", 159 "title": "Internal Server Error", 160 "detail": "Error fetching authorities", 161 "status": 500 162 }`, 163 expectedPath: "/config-dns/v2/zones/example.com/recordsets", 164 withError: &Error{ 165 Type: "internal_error", 166 Title: "Internal Server Error", 167 Detail: "Error fetching authorities", 168 StatusCode: http.StatusInternalServerError, 169 }, 170 }, 171 } 172 173 for name, test := range tests { 174 t.Run(name, func(t *testing.T) { 175 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 176 assert.Equal(t, test.expectedPath, r.URL.String()) 177 assert.Equal(t, http.MethodPost, r.Method) 178 w.WriteHeader(test.responseStatus) 179 if len(test.responseBody) > 0 { 180 _, err := w.Write([]byte(test.responseBody)) 181 assert.NoError(t, err) 182 } 183 })) 184 client := mockAPIClient(t, mockServer) 185 err := client.CreateRecordSets(context.Background(), test.sets, test.zone) 186 if test.withError != nil { 187 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 188 return 189 } 190 require.NoError(t, err) 191 }) 192 } 193 } 194 195 func TestDNS_UpdateRecordSets(t *testing.T) { 196 tests := map[string]struct { 197 zone string 198 sets *RecordSets 199 responseStatus int 200 responseBody string 201 expectedPath string 202 expectedResponse *RecordSetResponse 203 withError error 204 }{ 205 "200 OK": { 206 zone: "example.com", 207 sets: &RecordSets{ 208 []RecordSet{ 209 { 210 Name: "www.example.com", 211 Type: "A", 212 TTL: 300, 213 Rdata: []string{ 214 "10.0.0.2", 215 "10.0.0.3", 216 }, 217 }, 218 }, 219 }, 220 responseStatus: http.StatusNoContent, 221 expectedPath: "/config-dns/v2/zones/example.com/recordsets", 222 }, 223 "500 internal server error": { 224 zone: "example.com", 225 sets: &RecordSets{ 226 []RecordSet{ 227 { 228 Name: "www.example.com", 229 Type: "A", 230 TTL: 300, 231 Rdata: []string{ 232 "10.0.0.2", 233 "10.0.0.3", 234 }, 235 }, 236 }, 237 }, 238 responseStatus: http.StatusInternalServerError, 239 responseBody: ` 240 { 241 "type": "internal_error", 242 "title": "Internal Server Error", 243 "detail": "Error fetching authorities", 244 "status": 500 245 }`, 246 expectedPath: "/config-dns/v2/zones/example.com/recordsets", 247 withError: &Error{ 248 Type: "internal_error", 249 Title: "Internal Server Error", 250 Detail: "Error fetching authorities", 251 StatusCode: http.StatusInternalServerError, 252 }, 253 }, 254 } 255 256 for name, test := range tests { 257 t.Run(name, func(t *testing.T) { 258 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 259 assert.Equal(t, test.expectedPath, r.URL.String()) 260 assert.Equal(t, http.MethodPut, r.Method) 261 w.WriteHeader(test.responseStatus) 262 if len(test.responseBody) > 0 { 263 _, err := w.Write([]byte(test.responseBody)) 264 assert.NoError(t, err) 265 } 266 })) 267 client := mockAPIClient(t, mockServer) 268 err := client.UpdateRecordSets(context.Background(), test.sets, test.zone) 269 if test.withError != nil { 270 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 271 return 272 } 273 require.NoError(t, err) 274 }) 275 } 276 }