github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/response_action_test.go (about) 1 package botman 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 // Test Get ResponseAction List 16 func TestBotman_GetResponseActionList(t *testing.T) { 17 18 tests := map[string]struct { 19 params GetResponseActionListRequest 20 responseStatus int 21 responseBody string 22 expectedPath string 23 expectedResponse *GetResponseActionListResponse 24 withError func(*testing.T, error) 25 }{ 26 "200 OK": { 27 params: GetResponseActionListRequest{ 28 ConfigID: 43253, 29 Version: 15, 30 }, 31 responseStatus: http.StatusOK, 32 responseBody: ` 33 { 34 "responseActions": [ 35 {"actionId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"}, 36 {"actionId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"}, 37 {"actionId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}, 38 {"actionId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"}, 39 {"actionId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"} 40 ] 41 }`, 42 expectedPath: "/appsec/v1/configs/43253/versions/15/response-actions", 43 expectedResponse: &GetResponseActionListResponse{ 44 ResponseActions: []map[string]interface{}{ 45 {"actionId": "b85e3eaa-d334-466d-857e-33308ce416be", "testKey": "testValue1"}, 46 {"actionId": "69acad64-7459-4c1d-9bad-672600150127", "testKey": "testValue2"}, 47 {"actionId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 48 {"actionId": "10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey": "testValue4"}, 49 {"actionId": "4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey": "testValue5"}, 50 }, 51 }, 52 }, 53 "200 OK One Record": { 54 params: GetResponseActionListRequest{ 55 ConfigID: 43253, 56 Version: 15, 57 ActionID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 58 }, 59 responseStatus: http.StatusOK, 60 responseBody: ` 61 { 62 "responseActions":[ 63 {"actionId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"}, 64 {"actionId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"}, 65 {"actionId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}, 66 {"actionId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"}, 67 {"actionId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"} 68 ] 69 }`, 70 expectedPath: "/appsec/v1/configs/43253/versions/15/response-actions", 71 expectedResponse: &GetResponseActionListResponse{ 72 ResponseActions: []map[string]interface{}{ 73 {"actionId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 74 }, 75 }, 76 }, 77 "500 internal server error": { 78 params: GetResponseActionListRequest{ 79 ConfigID: 43253, 80 Version: 15, 81 }, 82 responseStatus: http.StatusInternalServerError, 83 responseBody: ` 84 { 85 "type": "internal_error", 86 "title": "Internal Server Error", 87 "detail": "Error fetching data", 88 "status": 500 89 }`, 90 expectedPath: "/appsec/v1/configs/43253/versions/15/response-actions", 91 withError: func(t *testing.T, err error) { 92 want := &Error{ 93 Type: "internal_error", 94 Title: "Internal Server Error", 95 Detail: "Error fetching data", 96 StatusCode: http.StatusInternalServerError, 97 } 98 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 99 }, 100 }, 101 "Missing ConfigID": { 102 params: GetResponseActionListRequest{ 103 Version: 15, 104 }, 105 withError: func(t *testing.T, err error) { 106 want := ErrStructValidation 107 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 108 assert.Contains(t, err.Error(), "ConfigID") 109 }, 110 }, 111 "Missing Version": { 112 params: GetResponseActionListRequest{ 113 ConfigID: 43253, 114 }, 115 withError: func(t *testing.T, err error) { 116 want := ErrStructValidation 117 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 118 assert.Contains(t, err.Error(), "Version") 119 }, 120 }, 121 } 122 123 for name, test := range tests { 124 t.Run(name, func(t *testing.T) { 125 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 126 assert.Equal(t, test.expectedPath, r.URL.String()) 127 assert.Equal(t, http.MethodGet, r.Method) 128 w.WriteHeader(test.responseStatus) 129 _, err := w.Write([]byte(test.responseBody)) 130 assert.NoError(t, err) 131 })) 132 client := mockAPIClient(t, mockServer) 133 result, err := client.GetResponseActionList( 134 session.ContextWithOptions( 135 context.Background(), 136 ), 137 test.params) 138 if test.withError != nil { 139 test.withError(t, err) 140 return 141 } 142 require.NoError(t, err) 143 assert.Equal(t, test.expectedResponse, result) 144 }) 145 } 146 }