github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/api_hostname_coverage_overlapping_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/v8/pkg/session" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestAppSec_ListApiHostnameCoverageOverlapping(t *testing.T) { 17 18 result := GetApiHostnameCoverageOverlappingResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestApiHostnameCoverageOverlapping/ApiHostnameCoverageOverlapping.json")) 21 err := json.Unmarshal([]byte(respData), &result) 22 require.NoError(t, err) 23 24 tests := map[string]struct { 25 params GetApiHostnameCoverageOverlappingRequest 26 responseStatus int 27 responseBody string 28 expectedPath string 29 expectedResponse *GetApiHostnameCoverageOverlappingResponse 30 withError error 31 headers http.Header 32 }{ 33 "200 OK": { 34 params: GetApiHostnameCoverageOverlappingRequest{ 35 ConfigID: 43253, 36 Version: 15, 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/hostname-coverage/overlapping?hostname=", 44 expectedResponse: &result, 45 }, 46 "500 internal server error": { 47 params: GetApiHostnameCoverageOverlappingRequest{ 48 ConfigID: 43253, 49 Version: 15, 50 }, 51 headers: http.Header{}, 52 responseStatus: http.StatusInternalServerError, 53 responseBody: ` 54 { 55 "type": "internal_error", 56 "title": "Internal Server Error", 57 "detail": "Error fetching ApiHostnameCoverageOverlapping", 58 "status": 500 59 }`, 60 expectedPath: "/appsec/v1/configs/43253/versions/15/hostname-coverage/overlapping?hostname=", 61 withError: &Error{ 62 Type: "internal_error", 63 Title: "Internal Server Error", 64 Detail: "Error fetching ApiHostnameCoverageOverlapping", 65 StatusCode: http.StatusInternalServerError, 66 }, 67 }, 68 } 69 70 for name, test := range tests { 71 t.Run(name, func(t *testing.T) { 72 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 73 assert.Equal(t, test.expectedPath, r.URL.String()) 74 assert.Equal(t, http.MethodGet, r.Method) 75 w.WriteHeader(test.responseStatus) 76 _, err := w.Write([]byte(test.responseBody)) 77 assert.NoError(t, err) 78 })) 79 client := mockAPIClient(t, mockServer) 80 result, err := client.GetApiHostnameCoverageOverlapping( 81 session.ContextWithOptions( 82 context.Background(), 83 session.WithContextHeaders(test.headers), 84 ), 85 test.params) 86 if test.withError != nil { 87 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 88 return 89 } 90 require.NoError(t, err) 91 assert.Equal(t, test.expectedResponse, result) 92 }) 93 } 94 } 95 96 // Test ApiHostnameCoverageOverlapping 97 func TestAppSec_GetApiHostnameCoverageOverlapping(t *testing.T) { 98 99 result := GetApiHostnameCoverageOverlappingResponse{} 100 101 respData := compactJSON(loadFixtureBytes("testdata/TestApiHostnameCoverageOverlapping/ApiHostnameCoverageOverlapping.json")) 102 err := json.Unmarshal([]byte(respData), &result) 103 require.NoError(t, err) 104 105 tests := map[string]struct { 106 params GetApiHostnameCoverageOverlappingRequest 107 responseStatus int 108 responseBody string 109 expectedPath string 110 expectedResponse *GetApiHostnameCoverageOverlappingResponse 111 withError error 112 }{ 113 "200 OK": { 114 params: GetApiHostnameCoverageOverlappingRequest{ 115 ConfigID: 43253, 116 Version: 15, 117 Hostname: "www.example.com", 118 }, 119 responseStatus: http.StatusOK, 120 responseBody: respData, 121 expectedPath: "/appsec/v1/configs/43253/versions/15/hostname-coverage/overlapping?hostname=www.example.com", 122 expectedResponse: &result, 123 }, 124 "500 internal server error": { 125 params: GetApiHostnameCoverageOverlappingRequest{ 126 ConfigID: 43253, 127 Version: 15, 128 Hostname: "www.example.com", 129 }, 130 responseStatus: http.StatusInternalServerError, 131 responseBody: ` 132 { 133 "type": "internal_error", 134 "title": "Internal Server Error", 135 "detail": "Error fetching ApiHostnameCoverageOverlapping" 136 }`, 137 expectedPath: "/appsec/v1/configs/43253/versions/15/hostname-coverage/overlapping?hostname=www.example.com", 138 withError: &Error{ 139 Type: "internal_error", 140 Title: "Internal Server Error", 141 Detail: "Error fetching ApiHostnameCoverageOverlapping", 142 StatusCode: http.StatusInternalServerError, 143 }, 144 }, 145 } 146 147 for name, test := range tests { 148 t.Run(name, func(t *testing.T) { 149 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 150 assert.Equal(t, test.expectedPath, r.URL.String()) 151 assert.Equal(t, http.MethodGet, r.Method) 152 w.WriteHeader(test.responseStatus) 153 _, err := w.Write([]byte(test.responseBody)) 154 assert.NoError(t, err) 155 })) 156 client := mockAPIClient(t, mockServer) 157 result, err := client.GetApiHostnameCoverageOverlapping(context.Background(), test.params) 158 if test.withError != nil { 159 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 160 return 161 } 162 require.NoError(t, err) 163 assert.Equal(t, test.expectedResponse, result) 164 }) 165 } 166 }