github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/configdns/authorities_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/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/session" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestDns_GetAuthorities(t *testing.T) { 16 tests := map[string]struct { 17 contractID string 18 responseStatus int 19 responseBody string 20 expectedPath string 21 expectedResponse *AuthorityResponse 22 withError error 23 }{ 24 "200 OK": { 25 contractID: "9-9XXXXX", 26 responseStatus: http.StatusOK, 27 responseBody: ` 28 { 29 "contracts": [ 30 { 31 "contractId": "9-9XXXXX", 32 "authorities": [ 33 "a1-118.akam.net.", 34 "a2-64.akam.net.", 35 "a6-66.akam.net.", 36 "a18-67.akam.net.", 37 "a7-64.akam.net.", 38 "a11-64.akam.net." 39 ] 40 } 41 ] 42 }`, 43 expectedPath: "/config-dns/v2/data/authorities?contractIds=9-9XXXXX", 44 expectedResponse: &AuthorityResponse{ 45 Contracts: []Contract{ 46 { 47 ContractID: "9-9XXXXX", 48 Authorities: []string{ 49 "a1-118.akam.net.", 50 "a2-64.akam.net.", 51 "a6-66.akam.net.", 52 "a18-67.akam.net.", 53 "a7-64.akam.net.", 54 "a11-64.akam.net.", 55 }, 56 }, 57 }, 58 }, 59 }, 60 "Missing arguments": { 61 contractID: "", 62 responseStatus: http.StatusOK, 63 responseBody: "", 64 expectedPath: "/config-dns/v2/data/authorities?contractIds=9-9XXXXX", 65 withError: ErrBadRequest, 66 }, 67 "500 internal server error": { 68 contractID: "9-9XXXXX", 69 responseStatus: http.StatusInternalServerError, 70 responseBody: ` 71 { 72 "type": "internal_error", 73 "title": "Internal Server Error", 74 "detail": "Error fetching authorities", 75 "status": 500 76 }`, 77 expectedPath: "/config-dns/v2/data/authorities?contractIds=9-9XXXXX", 78 withError: &Error{ 79 Type: "internal_error", 80 Title: "Internal Server Error", 81 Detail: "Error fetching authorities", 82 StatusCode: http.StatusInternalServerError, 83 }, 84 }, 85 } 86 87 for name, test := range tests { 88 t.Run(name, func(t *testing.T) { 89 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 90 assert.Equal(t, test.expectedPath, r.URL.String()) 91 assert.Equal(t, http.MethodGet, r.Method) 92 w.WriteHeader(test.responseStatus) 93 _, err := w.Write([]byte(test.responseBody)) 94 assert.NoError(t, err) 95 })) 96 client := mockAPIClient(t, mockServer) 97 result, err := client.GetAuthorities(context.Background(), test.contractID) 98 if test.withError != nil { 99 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 100 return 101 } 102 require.NoError(t, err) 103 assert.Equal(t, test.expectedResponse, result) 104 }) 105 } 106 } 107 108 func TestDns_GetNameServerRecordList(t *testing.T) { 109 tests := map[string]struct { 110 contractID string 111 responseStatus int 112 responseBody string 113 expectedPath string 114 expectedResponse []string 115 withError error 116 }{ 117 "test with valid arguments": { 118 contractID: "9-9XXXXX", 119 responseStatus: http.StatusOK, 120 responseBody: ` 121 { 122 "contracts": [ 123 { 124 "contractId": "9-9XXXXX", 125 "authorities": [ 126 "a1-118.akam.net.", 127 "a2-64.akam.net.", 128 "a6-66.akam.net.", 129 "a18-67.akam.net.", 130 "a7-64.akam.net.", 131 "a11-64.akam.net." 132 ] 133 } 134 ] 135 }`, 136 expectedResponse: []string{"a1-118.akam.net.", "a2-64.akam.net.", "a6-66.akam.net.", "a18-67.akam.net.", "a7-64.akam.net.", "a11-64.akam.net."}, 137 expectedPath: "/config-dns/v2/data/authorities?contractIds=9-9XXXXX", 138 }, 139 "test with missing arguments": { 140 contractID: "", 141 expectedPath: "/config-dns/v2/data/authorities?contractIds=9-9XXXXX", 142 withError: ErrBadRequest, 143 }, 144 } 145 146 for name, test := range tests { 147 t.Run(name, func(t *testing.T) { 148 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 149 assert.Equal(t, test.expectedPath, r.URL.String()) 150 assert.Equal(t, http.MethodGet, r.Method) 151 w.WriteHeader(test.responseStatus) 152 _, err := w.Write([]byte(test.responseBody)) 153 assert.NoError(t, err) 154 })) 155 client := mockAPIClient(t, mockServer) 156 result, err := client.GetNameServerRecordList(context.Background(), test.contractID) 157 if test.withError != nil { 158 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 159 return 160 } 161 require.NoError(t, err) 162 assert.Equal(t, test.expectedResponse, result) 163 }) 164 } 165 } 166 167 func TestDns_NewAuthorityResponse(t *testing.T) { 168 client := Client(session.Must(session.New())) 169 170 resp := client.NewAuthorityResponse(context.Background(), "empty") 171 172 assert.NotNil(t, resp) 173 }