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