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