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