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