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