github.com/akamai/AkamaiOPEN-edgegrid-golang/v5@v5.0.0/pkg/gtm/domain_test.go (about) 1 package gtm 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/gob" 7 "encoding/json" 8 "errors" 9 "net/http" 10 "net/http/httptest" 11 "testing" 12 13 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v5/pkg/session" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 ) 17 18 func TestGtm_NewDomain(t *testing.T) { 19 client := Client(session.Must(session.New())) 20 21 dom := client.NewDomain(context.Background(), "example.com", "primary") 22 23 assert.Equal(t, "example.com", dom.Name) 24 assert.Equal(t, "primary", dom.Type) 25 } 26 27 func TestGtm_ListDomains(t *testing.T) { 28 var result DomainsList 29 30 respData, err := loadTestData("TestGtm_ListDomains.resp.json") 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 if err := json.NewDecoder(bytes.NewBuffer(respData)).Decode(&result); err != nil { 36 t.Fatal(err) 37 } 38 39 tests := map[string]struct { 40 responseStatus int 41 responseBody string 42 expectedPath string 43 expectedResponse []*DomainItem 44 withError error 45 headers http.Header 46 }{ 47 "200 OK": { 48 headers: http.Header{ 49 "Content-Type": []string{"application/vnd.config-gtm.v1.4+json;charset=UTF-8"}, 50 }, 51 responseStatus: http.StatusOK, 52 responseBody: string(respData), 53 expectedPath: "/config-gtm/v1/domains", 54 expectedResponse: result.DomainItems, 55 }, 56 "500 internal server error": { 57 headers: http.Header{}, 58 responseStatus: http.StatusInternalServerError, 59 responseBody: ` 60 { 61 "type": "internal_error", 62 "title": "Internal Server Error", 63 "detail": "Error fetching domains", 64 "status": 500 65 }`, 66 expectedPath: "/config-gtm/v1/domains", 67 withError: &Error{ 68 Type: "internal_error", 69 Title: "Internal Server Error", 70 Detail: "Error fetching domains", 71 StatusCode: http.StatusInternalServerError, 72 }, 73 }, 74 } 75 76 for name, test := range tests { 77 t.Run(name, func(t *testing.T) { 78 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 79 assert.Equal(t, test.expectedPath, r.URL.String()) 80 assert.Equal(t, http.MethodGet, r.Method) 81 w.WriteHeader(test.responseStatus) 82 _, err := w.Write([]byte(test.responseBody)) 83 assert.NoError(t, err) 84 })) 85 client := mockAPIClient(t, mockServer) 86 result, err := client.ListDomains( 87 session.ContextWithOptions( 88 context.Background(), 89 session.WithContextHeaders(test.headers))) 90 if test.withError != nil { 91 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 92 return 93 } 94 require.NoError(t, err) 95 assert.Equal(t, test.expectedResponse, result) 96 }) 97 } 98 } 99 100 func TestGtm_NullFieldMap(t *testing.T) { 101 var result NullFieldMapStruct 102 103 gob.Register(map[string]NullPerObjectAttributeStruct{}) 104 105 respData, err := loadTestData("TestGtm_NullFieldMap.resp.json") 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 resultData, err := loadTestData("TestGtm_NullFieldMap.result.gob") 111 if err != nil { 112 t.Fatal(err) 113 } 114 115 if err := gob.NewDecoder(bytes.NewBuffer(resultData)).Decode(&result); err != nil { 116 t.Fatal(err) 117 } 118 119 tests := map[string]struct { 120 arg *Domain 121 responseStatus int 122 responseBody []byte 123 expectedPath string 124 expectedResponse *NullFieldMapStruct 125 withError error 126 }{ 127 "200 OK": { 128 arg: &Domain{ 129 Name: "example.akadns.net", 130 Type: "primary", 131 }, 132 responseStatus: http.StatusOK, 133 responseBody: respData, 134 expectedPath: "/config-gtm/v1/domains/example.akadns.net", 135 expectedResponse: &result, 136 }, 137 "500 internal server error": { 138 arg: &Domain{ 139 Name: "example.akadns.net", 140 Type: "primary", 141 }, 142 responseStatus: http.StatusInternalServerError, 143 responseBody: []byte(` 144 { 145 "type": "internal_error", 146 "title": "Internal Server Error", 147 "detail": "Error fetching null field map", 148 "status": 500 149 }`), 150 expectedPath: "/config-gtm/v1/domains/example.akadns.net", 151 withError: &Error{ 152 Type: "internal_error", 153 Title: "Internal Server Error", 154 Detail: "Error fetching null field map", 155 StatusCode: http.StatusInternalServerError, 156 }, 157 }, 158 } 159 160 for name, test := range tests { 161 t.Run(name, func(t *testing.T) { 162 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 163 assert.Equal(t, test.expectedPath, r.URL.String()) 164 assert.Equal(t, http.MethodGet, r.Method) 165 w.WriteHeader(test.responseStatus) 166 _, err := w.Write(test.responseBody) 167 assert.NoError(t, err) 168 })) 169 client := mockAPIClient(t, mockServer) 170 result, err := client.NullFieldMap(context.Background(), test.arg) 171 if test.withError != nil { 172 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 173 return 174 } 175 require.NoError(t, err) 176 assert.Equal(t, test.expectedResponse, result) 177 }) 178 } 179 } 180 181 // Test GetDomain 182 // GetDomain(context.Context, string) (*Domain, error) 183 func TestGtm_GetDomain(t *testing.T) { 184 var result Domain 185 186 respData, err := loadTestData("TestGtm_GetDomain.resp.json") 187 if err != nil { 188 t.Fatal(err) 189 } 190 191 if err := json.NewDecoder(bytes.NewBuffer(respData)).Decode(&result); err != nil { 192 t.Fatal(err) 193 } 194 195 tests := map[string]struct { 196 domain string 197 responseStatus int 198 responseBody []byte 199 expectedPath string 200 expectedResponse *Domain 201 withError error 202 }{ 203 "200 OK": { 204 domain: "example.akadns.net", 205 responseStatus: http.StatusOK, 206 responseBody: respData, 207 expectedPath: "/config-gtm/v1/domains/example.akadns.net", 208 expectedResponse: &result, 209 }, 210 "500 internal server error": { 211 domain: "example.akadns.net", 212 responseStatus: http.StatusInternalServerError, 213 responseBody: []byte(` 214 { 215 "type": "internal_error", 216 "title": "Internal Server Error", 217 "detail": "Error fetching domain" 218 }`), 219 expectedPath: "/config-gtm/v1/domains/example.akadns.net", 220 withError: &Error{ 221 Type: "internal_error", 222 Title: "Internal Server Error", 223 Detail: "Error fetching domain", 224 StatusCode: http.StatusInternalServerError, 225 }, 226 }, 227 } 228 229 for name, test := range tests { 230 t.Run(name, func(t *testing.T) { 231 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 232 assert.Equal(t, test.expectedPath, r.URL.String()) 233 assert.Equal(t, http.MethodGet, r.Method) 234 w.WriteHeader(test.responseStatus) 235 _, err := w.Write(test.responseBody) 236 assert.NoError(t, err) 237 })) 238 client := mockAPIClient(t, mockServer) 239 result, err := client.GetDomain(context.Background(), test.domain) 240 if test.withError != nil { 241 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 242 return 243 } 244 require.NoError(t, err) 245 assert.Equal(t, test.expectedResponse, result) 246 }) 247 } 248 } 249 250 // Test Create domain. 251 // CreateDomain(context.Context, *Domain, map[string]string) (*DomainResponse, error) 252 func TestGtm_CreateDomain(t *testing.T) { 253 var result DomainResponse 254 255 respData, err := loadTestData("TestGtm_GetDomain.resp.json") 256 if err != nil { 257 t.Fatal(err) 258 } 259 260 if err := json.NewDecoder(bytes.NewBuffer(respData)).Decode(&result); err != nil { 261 t.Fatal(err) 262 } 263 264 tests := map[string]struct { 265 domain Domain 266 query map[string]string 267 responseStatus int 268 responseBody []byte 269 expectedPath string 270 expectedResponse *DomainResponse 271 withError error 272 headers http.Header 273 }{ 274 "201 Created": { 275 domain: Domain{ 276 Name: "gtmdomtest.akadns.net", 277 Type: "basic", 278 }, 279 query: map[string]string{"contractId": "1-2ABCDE"}, 280 headers: http.Header{ 281 "Content-Type": []string{"application/vnd.config-gtm.v1.4+json;charset=UTF-8"}, 282 }, 283 responseStatus: http.StatusCreated, 284 responseBody: respData, 285 expectedResponse: &result, 286 expectedPath: "/config-gtm/v1/domains?contractId=1-2ABCDE", 287 }, 288 "500 internal server error": { 289 domain: Domain{ 290 Name: "gtmdomtest.akadns.net", 291 Type: "basic", 292 }, 293 query: map[string]string{"contractId": "1-2ABCDE"}, 294 responseStatus: http.StatusInternalServerError, 295 responseBody: []byte(` 296 { 297 "type": "internal_error", 298 "title": "Internal Server Error", 299 "detail": "Error creating domain" 300 }`), 301 expectedPath: "/config-gtm/v1/domains?contractId=1-2ABCDE", 302 withError: &Error{ 303 Type: "internal_error", 304 Title: "Internal Server Error", 305 Detail: "Error creating domain", 306 StatusCode: http.StatusInternalServerError, 307 }, 308 }, 309 } 310 311 for name, test := range tests { 312 t.Run(name, func(t *testing.T) { 313 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 314 assert.Equal(t, http.MethodPost, r.Method) 315 w.WriteHeader(test.responseStatus) 316 if len(test.responseBody) > 0 { 317 _, err := w.Write(test.responseBody) 318 assert.NoError(t, err) 319 } 320 })) 321 client := mockAPIClient(t, mockServer) 322 result, err := client.CreateDomain( 323 session.ContextWithOptions( 324 context.Background(), 325 session.WithContextHeaders(test.headers)), &test.domain, test.query) 326 if test.withError != nil { 327 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 328 return 329 } 330 require.NoError(t, err) 331 assert.Equal(t, test.expectedResponse, result) 332 }) 333 } 334 } 335 336 // Test Update domain. 337 // UpdateDomain(context.Context, *Domain, map[string]string) (*DomainResponse, error) 338 func TestGtm_UpdateDomain(t *testing.T) { 339 var result DomainResponse 340 341 respData, err := loadTestData("TestGtm_UpdateDomain.resp.json") 342 if err != nil { 343 t.Fatal(err) 344 } 345 346 if err := json.NewDecoder(bytes.NewBuffer(respData)).Decode(&result); err != nil { 347 t.Fatal(err) 348 } 349 350 tests := map[string]struct { 351 domain Domain 352 query map[string]string 353 responseStatus int 354 responseBody []byte 355 expectedPath string 356 expectedResponse *ResponseStatus 357 withError error 358 headers http.Header 359 }{ 360 "200 Success": { 361 domain: Domain{ 362 EndUserMappingEnabled: false, 363 Name: "gtmdomtest.akadns.net", 364 Type: "basic", 365 }, 366 query: map[string]string{"contractId": "1-2ABCDE"}, 367 headers: http.Header{ 368 "Content-Type": []string{"application/vnd.config-gtm.v1.4+json;charset=UTF-8"}, 369 }, 370 responseStatus: http.StatusCreated, 371 responseBody: respData, 372 expectedResponse: result.Status, 373 expectedPath: "/config-gtm/v1/domains?contractId=1-2ABCDE", 374 }, 375 "500 internal server error": { 376 domain: Domain{ 377 Name: "gtmdomtest.akadns.net", 378 Type: "basic", 379 }, 380 query: map[string]string{"contractId": "1-2ABCDE"}, 381 responseStatus: http.StatusInternalServerError, 382 responseBody: []byte(` 383 { 384 "type": "internal_error", 385 "title": "Internal Server Error", 386 "detail": "Error creating zone" 387 }`), 388 expectedPath: "/config-gtm/v1/domains?contractId=1-2ABCDE", 389 withError: &Error{ 390 Type: "internal_error", 391 Title: "Internal Server Error", 392 Detail: "Error creating zone", 393 StatusCode: http.StatusInternalServerError, 394 }, 395 }, 396 } 397 398 for name, test := range tests { 399 t.Run(name, func(t *testing.T) { 400 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 401 assert.Equal(t, http.MethodPut, r.Method) 402 w.WriteHeader(test.responseStatus) 403 if len(test.responseBody) > 0 { 404 _, err := w.Write(test.responseBody) 405 assert.NoError(t, err) 406 } 407 })) 408 client := mockAPIClient(t, mockServer) 409 result, err := client.UpdateDomain( 410 session.ContextWithOptions( 411 context.Background(), 412 session.WithContextHeaders(test.headers)), &test.domain, test.query) 413 if test.withError != nil { 414 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 415 return 416 } 417 require.NoError(t, err) 418 assert.Equal(t, test.expectedResponse, result) 419 }) 420 } 421 } 422 423 /* Future. Presently no domain Delete endpoint. 424 func TestGtm_DeleteDomain(t *testing.T) { 425 426 defer gock.Off() 427 428 mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/"+gtmTestDomain) 429 mock. 430 Delete("/config-gtm/v1/domains/"+gtmTestDomain). 431 HeaderPresent("Authorization"). 432 Reply(200). 433 SetHeader("Content-Type", "application/vnd.config-gtm.v1.4+json;charset=UTF-8"). 434 BodyString(`{ 435 "resource" : null, 436 "status" : { 437 "changeId": "40e36abd-bfb2-4635-9fca-62175cf17007", 438 "links": [ 439 { 440 "href": "https://akab-ymtebc45gco3ypzj-apz4yxpek55y7fyv.luna.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/status/current", 441 "rel": "self" 442 } 443 ], 444 "message": "Change Pending", 445 "passingValidation": true, 446 "propagationStatus": "PENDING", 447 "propagationStatusDate": "2019-04-25T14:54:00.000+00:00" 448 }, 449 }`) 450 451 Init(config) 452 453 getDomain := instantiateDomain() 454 455 _, err := getDomain.Delete() 456 assert.NoError(t, err) 457 458 } 459 */