github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/export_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_ListExportConfiguration(t *testing.T) {
    17  
    18  	result := GetExportConfigurationResponse{}
    19  
    20  	respData := compactJSON(loadFixtureBytes("testdata/TestExportConfiguration/ExportConfiguration.json"))
    21  	json.Unmarshal([]byte(respData), &result)
    22  
    23  	tests := map[string]struct {
    24  		params           GetExportConfigurationRequest
    25  		responseStatus   int
    26  		responseBody     string
    27  		expectedPath     string
    28  		expectedResponse *GetExportConfigurationResponse
    29  		withError        error
    30  		headers          http.Header
    31  	}{
    32  		"200 OK": {
    33  			params: GetExportConfigurationRequest{
    34  				ConfigID: 43253,
    35  				Version:  15,
    36  			},
    37  			headers: http.Header{
    38  				"Content-Type": []string{"application/json"},
    39  			},
    40  			responseStatus:   http.StatusOK,
    41  			responseBody:     string(respData),
    42  			expectedPath:     "/appsec/v1/export/configs/43253/versions/15",
    43  			expectedResponse: &result,
    44  		},
    45  		"500 internal server error": {
    46  			params: GetExportConfigurationRequest{
    47  				ConfigID: 43253,
    48  				Version:  15,
    49  			},
    50  			headers:        http.Header{},
    51  			responseStatus: http.StatusInternalServerError,
    52  			responseBody: `
    53  {
    54      "type": "internal_error",
    55      "title": "Internal Server Error",
    56      "detail": "Error fetching propertys",
    57      "status": 500
    58  }`,
    59  			expectedPath: "/appsec/v1/export/configs/43253/versions/15",
    60  			withError: &Error{
    61  				Type:       "internal_error",
    62  				Title:      "Internal Server Error",
    63  				Detail:     "Error fetching propertys",
    64  				StatusCode: http.StatusInternalServerError,
    65  			},
    66  		},
    67  	}
    68  
    69  	for name, test := range tests {
    70  		t.Run(name, func(t *testing.T) {
    71  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    72  				assert.Equal(t, test.expectedPath, r.URL.String())
    73  				assert.Equal(t, http.MethodGet, r.Method)
    74  				w.WriteHeader(test.responseStatus)
    75  				_, err := w.Write([]byte(test.responseBody))
    76  				assert.NoError(t, err)
    77  			}))
    78  			client := mockAPIClient(t, mockServer)
    79  			result, err := client.GetExportConfiguration(
    80  				session.ContextWithOptions(
    81  					context.Background(),
    82  					session.WithContextHeaders(test.headers),
    83  				),
    84  				test.params)
    85  			if test.withError != nil {
    86  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
    87  				return
    88  			}
    89  			require.NoError(t, err)
    90  			assert.Equal(t, test.expectedResponse, result)
    91  		})
    92  	}
    93  }
    94  
    95  func TestAppSec_ListExportConfigurations(t *testing.T) {
    96  
    97  	result := GetExportConfigurationsResponse{}
    98  
    99  	respData := compactJSON(loadFixtureBytes("testdata/TestExportConfiguration/ExportConfiguration.json"))
   100  	json.Unmarshal([]byte(respData), &result)
   101  
   102  	tests := map[string]struct {
   103  		params           GetExportConfigurationsRequest
   104  		responseStatus   int
   105  		responseBody     string
   106  		expectedPath     string
   107  		expectedResponse *GetExportConfigurationsResponse
   108  		withError        error
   109  		headers          http.Header
   110  	}{
   111  		"200 OK": {
   112  			params: GetExportConfigurationsRequest{
   113  				ConfigID: 43253,
   114  				Version:  15,
   115  			},
   116  			headers: http.Header{
   117  				"Content-Type": []string{"application/json"},
   118  			},
   119  			responseStatus:   http.StatusOK,
   120  			responseBody:     string(respData),
   121  			expectedPath:     "/appsec/v1/export/configs/43253/versions/15",
   122  			expectedResponse: &result,
   123  		},
   124  		"500 internal server error": {
   125  			params: GetExportConfigurationsRequest{
   126  				ConfigID: 43253,
   127  				Version:  15,
   128  			},
   129  			headers:        http.Header{},
   130  			responseStatus: http.StatusInternalServerError,
   131  			responseBody: `
   132  {
   133      "type": "internal_error",
   134      "title": "Internal Server Error",
   135      "detail": "Error fetching propertys",
   136      "status": 500
   137  }`,
   138  			expectedPath: "/appsec/v1/export/configs/43253/versions/15",
   139  			withError: &Error{
   140  				Type:       "internal_error",
   141  				Title:      "Internal Server Error",
   142  				Detail:     "Error fetching propertys",
   143  				StatusCode: http.StatusInternalServerError,
   144  			},
   145  		},
   146  	}
   147  
   148  	for name, test := range tests {
   149  		t.Run(name, func(t *testing.T) {
   150  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   151  				assert.Equal(t, test.expectedPath, r.URL.String())
   152  				assert.Equal(t, http.MethodGet, r.Method)
   153  				w.WriteHeader(test.responseStatus)
   154  				_, err := w.Write([]byte(test.responseBody))
   155  				assert.NoError(t, err)
   156  			}))
   157  			client := mockAPIClient(t, mockServer)
   158  			result, err := client.GetExportConfigurations(
   159  				session.ContextWithOptions(
   160  					context.Background(),
   161  					session.WithContextHeaders(test.headers),
   162  				),
   163  				test.params)
   164  			if test.withError != nil {
   165  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   166  				return
   167  			}
   168  			require.NoError(t, err)
   169  			assert.Equal(t, test.expectedResponse, result)
   170  		})
   171  	}
   172  }