github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/papi/edgehostname_test.go (about) 1 package papi 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 TestPapi_GetEdgeHostnames(t *testing.T) { 15 tests := map[string]struct { 16 params GetEdgeHostnamesRequest 17 responseStatus int 18 responseBody string 19 expectedPath string 20 expectedResponse *GetEdgeHostnamesResponse 21 withError func(*testing.T, error) 22 }{ 23 "200 OK": { 24 params: GetEdgeHostnamesRequest{ 25 ContractID: "contract", 26 GroupID: "group", 27 Options: []string{"opt1", "opt2"}, 28 }, 29 responseStatus: http.StatusOK, 30 responseBody: ` 31 { 32 "accountId": "acc", 33 "contractId": "contract", 34 "groupId": "group", 35 "edgeHostnames": { 36 "items": [ 37 { 38 "edgeHostnameId": "ehID", 39 "edgeHostnameDomain": "example.com.edgekey.net", 40 "productId": "prdID", 41 "domainPrefix": "example.com", 42 "domainSuffix": "edgekey.net", 43 "status": "PENDING", 44 "secure": true, 45 "ipVersionBehavior": "IPV4" 46 } 47 ] 48 } 49 }`, 50 expectedPath: "/papi/v1/edgehostnames?contractId=contract&groupId=group&options=opt1%2Copt2", 51 expectedResponse: &GetEdgeHostnamesResponse{ 52 AccountID: "acc", 53 ContractID: "contract", 54 GroupID: "group", 55 EdgeHostnames: EdgeHostnameItems{Items: []EdgeHostnameGetItem{ 56 { 57 ID: "ehID", 58 Domain: "example.com.edgekey.net", 59 ProductID: "prdID", 60 DomainPrefix: "example.com", 61 DomainSuffix: "edgekey.net", 62 Status: "PENDING", 63 Secure: true, 64 IPVersionBehavior: "IPV4", 65 UseCases: nil, 66 }, 67 }}, 68 }, 69 }, 70 "500 internal server error": { 71 params: GetEdgeHostnamesRequest{ 72 ContractID: "contract", 73 GroupID: "group", 74 }, 75 responseStatus: http.StatusInternalServerError, 76 responseBody: ` 77 { 78 "type": "internal_error", 79 "title": "Internal Server Error", 80 "detail": "Error fetching edge hostnames", 81 "status": 500 82 }`, 83 expectedPath: "/papi/v1/edgehostnames?contractId=contract&groupId=group", 84 withError: func(t *testing.T, err error) { 85 want := &Error{ 86 Type: "internal_error", 87 Title: "Internal Server Error", 88 Detail: "Error fetching edge hostnames", 89 StatusCode: http.StatusInternalServerError, 90 } 91 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 92 }, 93 }, 94 "empty group ID": { 95 params: GetEdgeHostnamesRequest{ 96 ContractID: "contract", 97 GroupID: "", 98 }, 99 withError: func(t *testing.T, err error) { 100 want := ErrStructValidation 101 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 102 assert.Contains(t, err.Error(), "GroupID") 103 }, 104 }, 105 "empty contract ID": { 106 params: GetEdgeHostnamesRequest{ 107 ContractID: "", 108 GroupID: "group", 109 }, 110 withError: func(t *testing.T, err error) { 111 want := ErrStructValidation 112 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 113 assert.Contains(t, err.Error(), "ContractID") 114 }, 115 }, 116 } 117 118 for name, test := range tests { 119 t.Run(name, func(t *testing.T) { 120 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 121 assert.Equal(t, test.expectedPath, r.URL.String()) 122 assert.Equal(t, http.MethodGet, r.Method) 123 w.WriteHeader(test.responseStatus) 124 _, err := w.Write([]byte(test.responseBody)) 125 assert.NoError(t, err) 126 })) 127 client := mockAPIClient(t, mockServer) 128 result, err := client.GetEdgeHostnames(context.Background(), test.params) 129 if test.withError != nil { 130 test.withError(t, err) 131 return 132 } 133 require.NoError(t, err) 134 assert.Equal(t, test.expectedResponse, result) 135 }) 136 } 137 } 138 139 func TestPapi_GetEdgeHostname(t *testing.T) { 140 tests := map[string]struct { 141 params GetEdgeHostnameRequest 142 responseStatus int 143 responseBody string 144 expectedPath string 145 expectedResponse *GetEdgeHostnamesResponse 146 withError func(*testing.T, error) 147 }{ 148 "200 OK": { 149 params: GetEdgeHostnameRequest{ 150 EdgeHostnameID: "ehID", 151 ContractID: "contract", 152 GroupID: "group", 153 Options: []string{"opt1", "opt2"}, 154 }, 155 responseStatus: http.StatusOK, 156 responseBody: ` 157 { 158 "accountId": "acc", 159 "contractId": "contract", 160 "groupId": "group", 161 "edgeHostnames": { 162 "items": [ 163 { 164 "edgeHostnameId": "ehID", 165 "edgeHostnameDomain": "example.com.edgekey.net", 166 "productId": "prdID", 167 "domainPrefix": "example.com", 168 "domainSuffix": "edgekey.net", 169 "status": "PENDING", 170 "secure": true, 171 "ipVersionBehavior": "IPV4" 172 } 173 ] 174 } 175 }`, 176 expectedPath: "/papi/v1/edgehostnames/ehID?contractId=contract&groupId=group&options=opt1%2Copt2", 177 expectedResponse: &GetEdgeHostnamesResponse{ 178 AccountID: "acc", 179 ContractID: "contract", 180 GroupID: "group", 181 EdgeHostnames: EdgeHostnameItems{Items: []EdgeHostnameGetItem{ 182 { 183 ID: "ehID", 184 Domain: "example.com.edgekey.net", 185 ProductID: "prdID", 186 DomainPrefix: "example.com", 187 DomainSuffix: "edgekey.net", 188 Status: "PENDING", 189 Secure: true, 190 IPVersionBehavior: "IPV4", 191 UseCases: nil, 192 }, 193 }}, 194 EdgeHostname: EdgeHostnameGetItem{ 195 ID: "ehID", 196 Domain: "example.com.edgekey.net", 197 ProductID: "prdID", 198 DomainPrefix: "example.com", 199 DomainSuffix: "edgekey.net", 200 Status: "PENDING", 201 Secure: true, 202 IPVersionBehavior: "IPV4", 203 UseCases: nil, 204 }, 205 }, 206 }, 207 "Edge hostname not found": { 208 params: GetEdgeHostnameRequest{ 209 EdgeHostnameID: "ehID", 210 ContractID: "contract", 211 GroupID: "group", 212 Options: []string{"opt1", "opt2"}, 213 }, 214 responseStatus: http.StatusOK, 215 responseBody: ` 216 { 217 "accountId": "acc", 218 "contractId": "contract", 219 "groupId": "group", 220 "edgeHostnames": { 221 "items": [ 222 ] 223 } 224 }`, 225 expectedPath: "/papi/v1/edgehostnames/ehID?contractId=contract&groupId=group&options=opt1%2Copt2", 226 withError: func(t *testing.T, err error) { 227 assert.True(t, errors.Is(err, ErrNotFound), "want: %v; got: %v", ErrNotFound, err) 228 }, 229 }, 230 "500 internal server error": { 231 params: GetEdgeHostnameRequest{ 232 EdgeHostnameID: "ehID", 233 ContractID: "contract", 234 GroupID: "group", 235 }, 236 responseStatus: http.StatusInternalServerError, 237 responseBody: ` 238 { 239 "type": "internal_error", 240 "title": "Internal Server Error", 241 "detail": "Error fetching edge hostnames", 242 "status": 500 243 }`, 244 expectedPath: "/papi/v1/edgehostnames/ehID?contractId=contract&groupId=group", 245 withError: func(t *testing.T, err error) { 246 want := &Error{ 247 Type: "internal_error", 248 Title: "Internal Server Error", 249 Detail: "Error fetching edge hostnames", 250 StatusCode: http.StatusInternalServerError, 251 } 252 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 253 }, 254 }, 255 "empty group ID": { 256 params: GetEdgeHostnameRequest{ 257 EdgeHostnameID: "ehID", 258 ContractID: "contract", 259 GroupID: "", 260 }, 261 withError: func(t *testing.T, err error) { 262 want := ErrStructValidation 263 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 264 assert.Contains(t, err.Error(), "GroupID") 265 }, 266 }, 267 "empty contract ID": { 268 params: GetEdgeHostnameRequest{ 269 EdgeHostnameID: "ehID", 270 ContractID: "", 271 GroupID: "group", 272 }, 273 withError: func(t *testing.T, err error) { 274 want := ErrStructValidation 275 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 276 assert.Contains(t, err.Error(), "ContractID") 277 }, 278 }, 279 "empty edge hostname ID": { 280 params: GetEdgeHostnameRequest{ 281 EdgeHostnameID: "", 282 ContractID: "contract", 283 GroupID: "group", 284 }, 285 withError: func(t *testing.T, err error) { 286 want := ErrStructValidation 287 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 288 assert.Contains(t, err.Error(), "EdgeHostnameID") 289 }, 290 }, 291 } 292 293 for name, test := range tests { 294 t.Run(name, func(t *testing.T) { 295 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 296 assert.Equal(t, test.expectedPath, r.URL.String()) 297 assert.Equal(t, http.MethodGet, r.Method) 298 w.WriteHeader(test.responseStatus) 299 _, err := w.Write([]byte(test.responseBody)) 300 assert.NoError(t, err) 301 })) 302 client := mockAPIClient(t, mockServer) 303 result, err := client.GetEdgeHostname(context.Background(), test.params) 304 if test.withError != nil { 305 test.withError(t, err) 306 return 307 } 308 require.NoError(t, err) 309 assert.Equal(t, test.expectedResponse, result) 310 }) 311 } 312 } 313 314 func TestPapi_CreateEdgeHostname(t *testing.T) { 315 tests := map[string]struct { 316 params CreateEdgeHostnameRequest 317 responseStatus int 318 responseBody string 319 expectedPath string 320 expectedResponse *CreateEdgeHostnameResponse 321 withError func(*testing.T, error) 322 }{ 323 "200 OK": { 324 params: CreateEdgeHostnameRequest{ 325 ContractID: "contract", 326 GroupID: "group", 327 Options: []string{"opt1", "opt2"}, 328 EdgeHostname: EdgeHostnameCreate{ 329 ProductID: "product", 330 DomainPrefix: "example.com", 331 DomainSuffix: "edgekey.net", 332 Secure: true, 333 IPVersionBehavior: "IPV4", 334 UseCases: []UseCase{{ 335 Option: "option", 336 Type: "GLOBAL", 337 UseCase: "UseCase", 338 }}, 339 }, 340 }, 341 responseStatus: http.StatusCreated, 342 responseBody: ` 343 { 344 "edgeHostnameLink": "/papi/v1/edgehostnames/ehID?contractId=contract&group=group" 345 }`, 346 expectedPath: "/papi/v1/edgehostnames?contractId=contract&groupId=group&options=opt1%2Copt2", 347 expectedResponse: &CreateEdgeHostnameResponse{ 348 EdgeHostnameLink: "/papi/v1/edgehostnames/ehID?contractId=contract&group=group", 349 EdgeHostnameID: "ehID", 350 }, 351 }, 352 "200 OK - STANDARD_TLS": { 353 params: CreateEdgeHostnameRequest{ 354 ContractID: "contract", 355 GroupID: "group", 356 Options: []string{"opt1", "opt2"}, 357 EdgeHostname: EdgeHostnameCreate{ 358 ProductID: "product", 359 DomainPrefix: "example.com", 360 DomainSuffix: "edgesuite.net", 361 Secure: true, 362 SecureNetwork: "STANDARD_TLS", 363 IPVersionBehavior: "IPV4", 364 UseCases: nil, 365 }, 366 }, 367 responseStatus: http.StatusCreated, 368 responseBody: ` 369 { 370 "edgeHostnameLink": "/papi/v1/edgehostnames/ehID?contractId=contract&group=group" 371 }`, 372 expectedPath: "/papi/v1/edgehostnames?contractId=contract&groupId=group&options=opt1%2Copt2", 373 expectedResponse: &CreateEdgeHostnameResponse{ 374 EdgeHostnameLink: "/papi/v1/edgehostnames/ehID?contractId=contract&group=group", 375 EdgeHostnameID: "ehID", 376 }, 377 }, 378 "200 OK - SHARED_CERT": { 379 params: CreateEdgeHostnameRequest{ 380 ContractID: "contract", 381 GroupID: "group", 382 Options: []string{"opt1", "opt2"}, 383 EdgeHostname: EdgeHostnameCreate{ 384 ProductID: "product", 385 DomainPrefix: "example.com", 386 DomainSuffix: "akamaized.net", 387 Secure: true, 388 SecureNetwork: "SHARED_CERT", 389 IPVersionBehavior: "IPV4", 390 UseCases: nil, 391 }, 392 }, 393 responseStatus: http.StatusCreated, 394 responseBody: ` 395 { 396 "edgeHostnameLink": "/papi/v1/edgehostnames/ehID?contractId=contract&group=group" 397 }`, 398 expectedPath: "/papi/v1/edgehostnames?contractId=contract&groupId=group&options=opt1%2Copt2", 399 expectedResponse: &CreateEdgeHostnameResponse{ 400 EdgeHostnameLink: "/papi/v1/edgehostnames/ehID?contractId=contract&group=group", 401 EdgeHostnameID: "ehID", 402 }, 403 }, 404 "200 OK - ENHANCED_TLS": { 405 params: CreateEdgeHostnameRequest{ 406 ContractID: "contract", 407 GroupID: "group", 408 Options: []string{"opt1", "opt2"}, 409 EdgeHostname: EdgeHostnameCreate{ 410 ProductID: "product", 411 DomainPrefix: "example.com", 412 DomainSuffix: "edgekey.net", 413 CertEnrollmentID: 5, 414 Secure: true, 415 SecureNetwork: "ENHANCED_TLS", 416 IPVersionBehavior: "IPV4", 417 UseCases: nil, 418 }, 419 }, 420 responseStatus: http.StatusCreated, 421 responseBody: ` 422 { 423 "edgeHostnameLink": "/papi/v1/edgehostnames/ehID?contractId=contract&group=group" 424 }`, 425 expectedPath: "/papi/v1/edgehostnames?contractId=contract&groupId=group&options=opt1%2Copt2", 426 expectedResponse: &CreateEdgeHostnameResponse{ 427 EdgeHostnameLink: "/papi/v1/edgehostnames/ehID?contractId=contract&group=group", 428 EdgeHostnameID: "ehID", 429 }, 430 }, 431 "500 Internal Server Error": { 432 params: CreateEdgeHostnameRequest{ 433 ContractID: "contract", 434 GroupID: "group", 435 Options: []string{"opt1", "opt2"}, 436 EdgeHostname: EdgeHostnameCreate{ 437 ProductID: "product", 438 DomainPrefix: "example.com", 439 DomainSuffix: "edgekey.net", 440 Secure: true, 441 IPVersionBehavior: "IPV4", 442 UseCases: nil, 443 }, 444 }, 445 responseStatus: http.StatusInternalServerError, 446 responseBody: ` 447 { 448 "type": "internal_error", 449 "title": "Internal Server Error", 450 "detail": "Error creating edge hostname", 451 "status": 500 452 }`, 453 expectedPath: "/papi/v1/edgehostnames?contractId=contract&groupId=group&options=opt1%2Copt2", 454 withError: func(t *testing.T, err error) { 455 want := &Error{ 456 Type: "internal_error", 457 Title: "Internal Server Error", 458 Detail: "Error creating edge hostname", 459 StatusCode: http.StatusInternalServerError, 460 } 461 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 462 }, 463 }, 464 "empty group ID": { 465 params: CreateEdgeHostnameRequest{ 466 ContractID: "contract", 467 GroupID: "", 468 EdgeHostname: EdgeHostnameCreate{ 469 ProductID: "product", 470 DomainPrefix: "example.com", 471 DomainSuffix: "edgekey.net", 472 Secure: true, 473 IPVersionBehavior: "IPV4", 474 UseCases: nil, 475 }, 476 }, 477 withError: func(t *testing.T, err error) { 478 want := ErrStructValidation 479 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 480 assert.Contains(t, err.Error(), "GroupID") 481 }, 482 }, 483 "empty contract ID": { 484 params: CreateEdgeHostnameRequest{ 485 ContractID: "", 486 GroupID: "group", 487 EdgeHostname: EdgeHostnameCreate{ 488 ProductID: "product", 489 DomainPrefix: "example.com", 490 DomainSuffix: "edgekey.net", 491 Secure: true, 492 IPVersionBehavior: "IPV4", 493 UseCases: nil, 494 }, 495 }, 496 withError: func(t *testing.T, err error) { 497 want := ErrStructValidation 498 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 499 assert.Contains(t, err.Error(), "ContractID") 500 }, 501 }, 502 "empty domain prefix": { 503 params: CreateEdgeHostnameRequest{ 504 ContractID: "contract", 505 GroupID: "group", 506 EdgeHostname: EdgeHostnameCreate{ 507 ProductID: "product", 508 DomainPrefix: "", 509 DomainSuffix: "edgekey.net", 510 Secure: true, 511 IPVersionBehavior: "IPV4", 512 UseCases: nil, 513 }, 514 }, 515 withError: func(t *testing.T, err error) { 516 want := ErrStructValidation 517 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 518 assert.Contains(t, err.Error(), "DomainPrefix") 519 }, 520 }, 521 "empty domain suffix": { 522 params: CreateEdgeHostnameRequest{ 523 ContractID: "contract", 524 GroupID: "group", 525 EdgeHostname: EdgeHostnameCreate{ 526 ProductID: "product", 527 DomainPrefix: "example.com", 528 DomainSuffix: "", 529 Secure: true, 530 IPVersionBehavior: "IPV4", 531 UseCases: nil, 532 }, 533 }, 534 withError: func(t *testing.T, err error) { 535 want := ErrStructValidation 536 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 537 assert.Contains(t, err.Error(), "DomainSuffix") 538 }, 539 }, 540 "empty product id": { 541 params: CreateEdgeHostnameRequest{ 542 ContractID: "contract", 543 GroupID: "group", 544 EdgeHostname: EdgeHostnameCreate{ 545 ProductID: "", 546 DomainPrefix: "example.com", 547 DomainSuffix: "edgekey.net", 548 Secure: true, 549 IPVersionBehavior: "IPV4", 550 UseCases: nil, 551 }, 552 }, 553 withError: func(t *testing.T, err error) { 554 want := ErrStructValidation 555 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 556 assert.Contains(t, err.Error(), "ProductID") 557 }, 558 }, 559 "CertEnrollmentID is required for SecureNetwork == ENHANCED_TLS": { 560 params: CreateEdgeHostnameRequest{ 561 ContractID: "contract", 562 GroupID: "group", 563 EdgeHostname: EdgeHostnameCreate{ 564 ProductID: "product", 565 DomainPrefix: "example.com", 566 DomainSuffix: "edgekey.net", 567 Secure: true, 568 SecureNetwork: "ENHANCED_TLS", 569 IPVersionBehavior: "IPV4", 570 UseCases: nil, 571 }, 572 }, 573 withError: func(t *testing.T, err error) { 574 want := ErrStructValidation 575 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 576 assert.Contains(t, err.Error(), "CertEnrollmentID") 577 }, 578 }, 579 "SecureNetwork has invalid value": { 580 params: CreateEdgeHostnameRequest{ 581 ContractID: "contract", 582 GroupID: "group", 583 EdgeHostname: EdgeHostnameCreate{ 584 ProductID: "product", 585 DomainPrefix: "example.com", 586 DomainSuffix: "edgekey.net", 587 Secure: true, 588 SecureNetwork: "test", 589 IPVersionBehavior: "IPV4", 590 UseCases: nil, 591 }, 592 }, 593 withError: func(t *testing.T, err error) { 594 want := ErrStructValidation 595 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 596 assert.Contains(t, err.Error(), "SecureNetwork") 597 }, 598 }, 599 "DomainSuffix has invalid value for SecureNetwork == STANDARD_TLS": { 600 params: CreateEdgeHostnameRequest{ 601 ContractID: "contract", 602 GroupID: "group", 603 EdgeHostname: EdgeHostnameCreate{ 604 ProductID: "product", 605 DomainPrefix: "example.com", 606 DomainSuffix: "edgekey.net", 607 Secure: true, 608 SecureNetwork: "STANDARD_TLS", 609 IPVersionBehavior: "IPV4", 610 UseCases: nil, 611 }, 612 }, 613 withError: func(t *testing.T, err error) { 614 want := ErrStructValidation 615 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 616 assert.Contains(t, err.Error(), "DomainSuffix") 617 }, 618 }, 619 "DomainSuffix has invalid value for SecureNetwork == SHARED_CERT": { 620 params: CreateEdgeHostnameRequest{ 621 ContractID: "contract", 622 GroupID: "group", 623 EdgeHostname: EdgeHostnameCreate{ 624 ProductID: "product", 625 DomainPrefix: "example.com", 626 DomainSuffix: "edgekey.net", 627 Secure: true, 628 SecureNetwork: "SHARED_CERT", 629 IPVersionBehavior: "IPV4", 630 UseCases: nil, 631 }, 632 }, 633 withError: func(t *testing.T, err error) { 634 want := ErrStructValidation 635 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 636 assert.Contains(t, err.Error(), "DomainSuffix") 637 }, 638 }, 639 "DomainSuffix has invalid value for SecureNetwork == ENHANCED_TLS": { 640 params: CreateEdgeHostnameRequest{ 641 ContractID: "contract", 642 GroupID: "group", 643 EdgeHostname: EdgeHostnameCreate{ 644 ProductID: "product", 645 DomainPrefix: "example.com", 646 DomainSuffix: "akamized.net", 647 Secure: true, 648 SecureNetwork: "STANDARD_TLS", 649 IPVersionBehavior: "IPV4", 650 UseCases: nil, 651 }, 652 }, 653 withError: func(t *testing.T, err error) { 654 want := ErrStructValidation 655 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 656 assert.Contains(t, err.Error(), "DomainSuffix") 657 }, 658 }, 659 "IPVersionBehavior has invalid value": { 660 params: CreateEdgeHostnameRequest{ 661 ContractID: "contract", 662 GroupID: "group", 663 EdgeHostname: EdgeHostnameCreate{ 664 ProductID: "product", 665 DomainPrefix: "example.com", 666 DomainSuffix: "akamized.net", 667 Secure: true, 668 IPVersionBehavior: "test", 669 UseCases: nil, 670 }, 671 }, 672 withError: func(t *testing.T, err error) { 673 want := ErrStructValidation 674 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 675 assert.Contains(t, err.Error(), "IPVersionBehavior") 676 }, 677 }, 678 "UseCase has empty UseCase value": { 679 params: CreateEdgeHostnameRequest{ 680 ContractID: "contract", 681 GroupID: "group", 682 EdgeHostname: EdgeHostnameCreate{ 683 ProductID: "product", 684 DomainPrefix: "example.com", 685 DomainSuffix: "akamized.net", 686 Secure: true, 687 IPVersionBehavior: "IPV4", 688 UseCases: []UseCase{{ 689 Option: "option", 690 Type: "GLOBAL", 691 UseCase: "", 692 }}, 693 }, 694 }, 695 withError: func(t *testing.T, err error) { 696 want := ErrStructValidation 697 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 698 assert.Contains(t, err.Error(), "UseCase") 699 }, 700 }, 701 "UseCase has empty Option value": { 702 params: CreateEdgeHostnameRequest{ 703 ContractID: "contract", 704 GroupID: "group", 705 EdgeHostname: EdgeHostnameCreate{ 706 ProductID: "product", 707 DomainPrefix: "example.com", 708 DomainSuffix: "akamized.net", 709 Secure: true, 710 IPVersionBehavior: "IPV4", 711 UseCases: []UseCase{{ 712 Option: "", 713 Type: "GLOBAL", 714 UseCase: "useCase", 715 }}, 716 }, 717 }, 718 withError: func(t *testing.T, err error) { 719 want := ErrStructValidation 720 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 721 assert.Contains(t, err.Error(), "Option") 722 }, 723 }, 724 "UseCase has invalid Type value": { 725 params: CreateEdgeHostnameRequest{ 726 ContractID: "contract", 727 GroupID: "group", 728 EdgeHostname: EdgeHostnameCreate{ 729 ProductID: "product", 730 DomainPrefix: "example.com", 731 DomainSuffix: "akamized.net", 732 Secure: true, 733 IPVersionBehavior: "IPV4", 734 UseCases: []UseCase{{ 735 Option: "option", 736 Type: "test", 737 UseCase: "UseCase", 738 }}, 739 }, 740 }, 741 withError: func(t *testing.T, err error) { 742 want := ErrStructValidation 743 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 744 assert.Contains(t, err.Error(), "Type") 745 }, 746 }, 747 "invalid location": { 748 params: CreateEdgeHostnameRequest{ 749 ContractID: "contract", 750 GroupID: "group", 751 Options: []string{"opt1", "opt2"}, 752 EdgeHostname: EdgeHostnameCreate{ 753 ProductID: "product", 754 DomainPrefix: "example.com", 755 DomainSuffix: "edgekey.net", 756 Secure: true, 757 IPVersionBehavior: "IPV4", 758 UseCases: []UseCase{{ 759 Option: "option", 760 Type: "GLOBAL", 761 UseCase: "UseCase", 762 }}, 763 }, 764 }, 765 responseStatus: http.StatusCreated, 766 responseBody: ` 767 { 768 "edgeHostnameLink": ":" 769 }`, 770 expectedPath: "/papi/v1/edgehostnames?contractId=contract&groupId=group&options=opt1%2Copt2", 771 withError: func(t *testing.T, err error) { 772 want := ErrInvalidResponseLink 773 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 774 }, 775 }, 776 } 777 778 for name, test := range tests { 779 t.Run(name, func(t *testing.T) { 780 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 781 assert.Equal(t, test.expectedPath, r.URL.String()) 782 assert.Equal(t, http.MethodPost, r.Method) 783 w.WriteHeader(test.responseStatus) 784 _, err := w.Write([]byte(test.responseBody)) 785 assert.NoError(t, err) 786 })) 787 client := mockAPIClient(t, mockServer) 788 result, err := client.CreateEdgeHostname(context.Background(), test.params) 789 if test.withError != nil { 790 test.withError(t, err) 791 return 792 } 793 require.NoError(t, err) 794 assert.Equal(t, test.expectedResponse, result) 795 }) 796 } 797 }