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