github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/bot_analytics_cookie_values_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 BotAnalyticsCookieValues
    16  func TestBotman_GetBotAnalyticsCookieValues(t *testing.T) {
    17  
    18  	tests := map[string]struct {
    19  		responseStatus   int
    20  		responseBody     string
    21  		expectedPath     string
    22  		expectedResponse map[string]interface{}
    23  		withError        func(*testing.T, error)
    24  	}{
    25  		"200 OK": {
    26  			responseStatus: http.StatusOK,
    27  			responseBody: `
    28  {
    29  	"values": [
    30  		{"testKey":"testValue1"},
    31  		{"testKey":"testValue2"},
    32  		{"testKey":"testValue3"},
    33  		{"testKey":"testValue4"},
    34  		{"testKey":"testValue5"}
    35  	]
    36  }`,
    37  			expectedPath: "/appsec/v1/bot-analytics-cookie/values",
    38  			expectedResponse: map[string]interface{}{
    39  				"values": []interface{}{
    40  					map[string]interface{}{"testKey": "testValue1"},
    41  					map[string]interface{}{"testKey": "testValue2"},
    42  					map[string]interface{}{"testKey": "testValue3"},
    43  					map[string]interface{}{"testKey": "testValue4"},
    44  					map[string]interface{}{"testKey": "testValue5"},
    45  				},
    46  			},
    47  		},
    48  		"500 internal server error": {
    49  			responseStatus: http.StatusInternalServerError,
    50  			responseBody: `
    51  {
    52      "type": "internal_error",
    53      "title": "Internal Server Error",
    54      "detail": "Error fetching data",
    55      "status": 500
    56  }`,
    57  			expectedPath: "/appsec/v1/bot-analytics-cookie/values",
    58  			withError: func(t *testing.T, err error) {
    59  				want := &Error{
    60  					Type:       "internal_error",
    61  					Title:      "Internal Server Error",
    62  					Detail:     "Error fetching data",
    63  					StatusCode: http.StatusInternalServerError,
    64  				}
    65  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    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.GetBotAnalyticsCookieValues(
    81  				session.ContextWithOptions(
    82  					context.Background(),
    83  				))
    84  			if test.withError != nil {
    85  				test.withError(t, err)
    86  				return
    87  			}
    88  			require.NoError(t, err)
    89  			assert.Equal(t, test.expectedResponse, result)
    90  		})
    91  	}
    92  }