github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/akamai_bot_category_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 AkamaiBotCategory List 16 func TestBotman_GetAkamaiBotCategoryList(t *testing.T) { 17 18 tests := map[string]struct { 19 params GetAkamaiBotCategoryListRequest 20 responseStatus int 21 responseBody string 22 expectedPath string 23 expectedResponse *GetAkamaiBotCategoryListResponse 24 withError func(*testing.T, error) 25 }{ 26 "200 OK": { 27 responseStatus: http.StatusOK, 28 responseBody: ` 29 { 30 "categories": [ 31 {"categoryId":"b85e3eaa-d334-466d-857e-33308ce416be", "categoryName":"Test Name 1", "testKey":"testValue1"}, 32 {"categoryId":"69acad64-7459-4c1d-9bad-672600150127", "categoryName":"Test Name 2", "testKey":"testValue2"}, 33 {"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "categoryName":"Test Name 3", "testKey":"testValue3"}, 34 {"categoryId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "categoryName":"Test Name 4", "testKey":"testValue4"}, 35 {"categoryId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "categoryName":"Test Name 5", "testKey":"testValue5"} 36 ] 37 }`, 38 expectedPath: "/appsec/v1/akamai-bot-categories", 39 expectedResponse: &GetAkamaiBotCategoryListResponse{ 40 Categories: []map[string]interface{}{ 41 {"categoryId": "b85e3eaa-d334-466d-857e-33308ce416be", "categoryName": "Test Name 1", "testKey": "testValue1"}, 42 {"categoryId": "69acad64-7459-4c1d-9bad-672600150127", "categoryName": "Test Name 2", "testKey": "testValue2"}, 43 {"categoryId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "categoryName": "Test Name 3", "testKey": "testValue3"}, 44 {"categoryId": "10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "categoryName": "Test Name 4", "testKey": "testValue4"}, 45 {"categoryId": "4d64d85a-a07f-485a-bbac-24c60658a1b8", "categoryName": "Test Name 5", "testKey": "testValue5"}, 46 }, 47 }, 48 }, 49 "200 OK One Record": { 50 params: GetAkamaiBotCategoryListRequest{ 51 CategoryName: "Test Name 3", 52 }, 53 responseStatus: http.StatusOK, 54 responseBody: ` 55 { 56 "categories":[ 57 {"categoryId":"b85e3eaa-d334-466d-857e-33308ce416be", "categoryName":"Test Name 1", "testKey":"testValue1"}, 58 {"categoryId":"69acad64-7459-4c1d-9bad-672600150127", "categoryName":"Test Name 2", "testKey":"testValue2"}, 59 {"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "categoryName":"Test Name 3", "testKey":"testValue3"}, 60 {"categoryId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "categoryName":"Test Name 4", "testKey":"testValue4"}, 61 {"categoryId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "categoryName":"Test Name 5", "testKey":"testValue5"} 62 ] 63 }`, 64 expectedPath: "/appsec/v1/akamai-bot-categories", 65 expectedResponse: &GetAkamaiBotCategoryListResponse{ 66 Categories: []map[string]interface{}{ 67 {"categoryId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "categoryName": "Test Name 3", "testKey": "testValue3"}, 68 }, 69 }, 70 }, 71 "500 internal server error": { 72 responseStatus: http.StatusInternalServerError, 73 responseBody: ` 74 { 75 "type": "internal_error", 76 "title": "Internal Server Error", 77 "detail": "Error fetching data", 78 "status": 500 79 }`, 80 expectedPath: "/appsec/v1/akamai-bot-categories", 81 withError: func(t *testing.T, err error) { 82 want := &Error{ 83 Type: "internal_error", 84 Title: "Internal Server Error", 85 Detail: "Error fetching data", 86 StatusCode: http.StatusInternalServerError, 87 } 88 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 89 }, 90 }, 91 } 92 93 for name, test := range tests { 94 t.Run(name, func(t *testing.T) { 95 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 96 assert.Equal(t, test.expectedPath, r.URL.String()) 97 assert.Equal(t, http.MethodGet, r.Method) 98 w.WriteHeader(test.responseStatus) 99 _, err := w.Write([]byte(test.responseBody)) 100 assert.NoError(t, err) 101 })) 102 client := mockAPIClient(t, mockServer) 103 result, err := client.GetAkamaiBotCategoryList( 104 session.ContextWithOptions( 105 context.Background(), 106 ), 107 test.params) 108 if test.withError != nil { 109 test.withError(t, err) 110 return 111 } 112 require.NoError(t, err) 113 assert.Equal(t, test.expectedResponse, result) 114 }) 115 } 116 }