github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/configuration_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_ListConfigurations(t *testing.T) { 17 18 result := GetConfigurationsResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestConfiguration/Configuration.json")) 21 err := json.Unmarshal([]byte(respData), &result) 22 require.NoError(t, err) 23 24 tests := map[string]struct { 25 params GetConfigurationsRequest 26 responseStatus int 27 responseBody string 28 expectedPath string 29 expectedResponse *GetConfigurationsResponse 30 withError error 31 headers http.Header 32 }{ 33 "200 OK": { 34 params: GetConfigurationsRequest{}, 35 headers: http.Header{ 36 "Content-Type": []string{"application/json"}, 37 }, 38 responseStatus: http.StatusOK, 39 responseBody: string(respData), 40 expectedPath: "/appsec/v1/configs", 41 expectedResponse: &result, 42 }, 43 "500 internal server error": { 44 params: GetConfigurationsRequest{}, 45 headers: http.Header{}, 46 responseStatus: http.StatusInternalServerError, 47 responseBody: ` 48 { 49 "type": "internal_error", 50 "title": "Internal Server Error", 51 "detail": "Error fetching propertys", 52 "status": 500 53 }`, 54 expectedPath: "/appsec/v1/configs", 55 withError: &Error{ 56 Type: "internal_error", 57 Title: "Internal Server Error", 58 Detail: "Error fetching propertys", 59 StatusCode: http.StatusInternalServerError, 60 }, 61 }, 62 } 63 64 for name, test := range tests { 65 t.Run(name, func(t *testing.T) { 66 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 67 assert.Equal(t, test.expectedPath, r.URL.String()) 68 assert.Equal(t, http.MethodGet, r.Method) 69 w.WriteHeader(test.responseStatus) 70 _, err := w.Write([]byte(test.responseBody)) 71 assert.NoError(t, err) 72 })) 73 client := mockAPIClient(t, mockServer) 74 result, err := client.GetConfigurations( 75 session.ContextWithOptions( 76 context.Background(), 77 session.WithContextHeaders(test.headers), 78 ), 79 test.params) 80 if test.withError != nil { 81 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 82 return 83 } 84 require.NoError(t, err) 85 assert.Equal(t, test.expectedResponse, result) 86 }) 87 } 88 }