github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/wap_selected_hostnames_test.go (about) 1 package appsec 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/session" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestAppSec_GetWAPSelectedHostnames(t *testing.T) { 17 18 result := GetWAPSelectedHostnamesResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestWAPSelectedHostnames/WAPSelectedHostnames.json")) 21 json.Unmarshal([]byte(respData), &result) 22 23 tests := map[string]struct { 24 params GetWAPSelectedHostnamesRequest 25 responseStatus int 26 responseBody string 27 expectedPath string 28 expectedResponse *GetWAPSelectedHostnamesResponse 29 withError error 30 headers http.Header 31 }{ 32 "200 OK": { 33 params: GetWAPSelectedHostnamesRequest{ 34 ConfigID: 43253, 35 Version: 15, 36 SecurityPolicyID: "AAAA_81230", 37 }, 38 headers: http.Header{ 39 "Content-Type": []string{"application/json"}, 40 }, 41 responseStatus: http.StatusOK, 42 responseBody: string(respData), 43 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/wap-selected-hostnames", 44 expectedResponse: &result, 45 }, 46 "500 internal server error": { 47 params: GetWAPSelectedHostnamesRequest{ 48 ConfigID: 43253, 49 Version: 15, 50 SecurityPolicyID: "AAAA_81230", 51 }, 52 headers: http.Header{}, 53 responseStatus: http.StatusInternalServerError, 54 responseBody: ` 55 { 56 "type": "internal_error", 57 "title": "Internal Server Error", 58 "detail": "Error fetching WAP selected hostnames", 59 "status": 500 60 }`, 61 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/wap-selected-hostnames", 62 withError: &Error{ 63 Type: "internal_error", 64 Title: "Internal Server Error", 65 Detail: "Error fetching WAP selected hostnames", 66 StatusCode: http.StatusInternalServerError, 67 }, 68 }, 69 } 70 71 for name, test := range tests { 72 t.Run(name, func(t *testing.T) { 73 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 74 assert.Equal(t, test.expectedPath, r.URL.String()) 75 assert.Equal(t, http.MethodGet, r.Method) 76 w.WriteHeader(test.responseStatus) 77 _, err := w.Write([]byte(test.responseBody)) 78 assert.NoError(t, err) 79 })) 80 client := mockAPIClient(t, mockServer) 81 result, err := client.GetWAPSelectedHostnames( 82 session.ContextWithOptions( 83 context.Background(), 84 session.WithContextHeaders(test.headers), 85 ), 86 test.params) 87 if test.withError != nil { 88 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 89 return 90 } 91 require.NoError(t, err) 92 assert.Equal(t, test.expectedResponse, result) 93 }) 94 } 95 }