github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/configuration_version_clone_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_ListConfigurationVersionClone(t *testing.T) { 17 18 result := GetConfigurationVersionCloneResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestConfigurationVersionClone/ConfigurationVersionClone.json")) 21 err := json.Unmarshal([]byte(respData), &result) 22 require.NoError(t, err) 23 24 tests := map[string]struct { 25 params GetConfigurationVersionCloneRequest 26 responseStatus int 27 responseBody string 28 expectedPath string 29 expectedResponse *GetConfigurationVersionCloneResponse 30 withError error 31 headers http.Header 32 }{ 33 "200 OK": { 34 params: GetConfigurationVersionCloneRequest{ 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/configs/43253/versions/15", 44 expectedResponse: &result, 45 }, 46 "500 internal server error": { 47 params: GetConfigurationVersionCloneRequest{ 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/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.GetConfigurationVersionClone( 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 // Test ConfigurationClone 97 func TestAppSec_GetConfigurationVersionClone(t *testing.T) { 98 99 result := GetConfigurationVersionCloneResponse{} 100 101 respData := compactJSON(loadFixtureBytes("testdata/TestConfigurationVersionClone/ConfigurationVersionClone.json")) 102 err := json.Unmarshal([]byte(respData), &result) 103 require.NoError(t, err) 104 105 tests := map[string]struct { 106 params GetConfigurationVersionCloneRequest 107 responseStatus int 108 responseBody string 109 expectedPath string 110 expectedResponse *GetConfigurationVersionCloneResponse 111 withError error 112 }{ 113 "200 OK": { 114 params: GetConfigurationVersionCloneRequest{ 115 ConfigID: 43253, 116 Version: 15, 117 }, 118 responseStatus: http.StatusOK, 119 responseBody: respData, 120 expectedPath: "/appsec/v1/configs/43253/versions/15", 121 expectedResponse: &result, 122 }, 123 "500 internal server error": { 124 params: GetConfigurationVersionCloneRequest{ 125 ConfigID: 43253, 126 Version: 15, 127 }, 128 responseStatus: http.StatusInternalServerError, 129 responseBody: ` 130 { 131 "type": "internal_error", 132 "title": "Internal Server Error", 133 "detail": "Error fetching match target" 134 }`, 135 expectedPath: "/appsec/v1/configs/43253/versions/15", 136 withError: &Error{ 137 Type: "internal_error", 138 Title: "Internal Server Error", 139 Detail: "Error fetching match target", 140 StatusCode: http.StatusInternalServerError, 141 }, 142 }, 143 } 144 145 for name, test := range tests { 146 t.Run(name, func(t *testing.T) { 147 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 148 assert.Equal(t, test.expectedPath, r.URL.String()) 149 assert.Equal(t, http.MethodGet, r.Method) 150 w.WriteHeader(test.responseStatus) 151 _, err := w.Write([]byte(test.responseBody)) 152 assert.NoError(t, err) 153 })) 154 client := mockAPIClient(t, mockServer) 155 result, err := client.GetConfigurationVersionClone(context.Background(), test.params) 156 if test.withError != nil { 157 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 158 return 159 } 160 require.NoError(t, err) 161 assert.Equal(t, test.expectedResponse, result) 162 }) 163 } 164 } 165 166 // Test Create ConfigurationClone 167 // Test Create ConfigurationClone 168 func TestAppSec_CreateConfigurationVersionClone(t *testing.T) { 169 170 result := CreateConfigurationVersionCloneResponse{} 171 172 respData := compactJSON(loadFixtureBytes("testdata/TestConfigurationVersionClone/ConfigurationVersionClone.json")) 173 err := json.Unmarshal([]byte(respData), &result) 174 require.NoError(t, err) 175 176 req := CreateConfigurationVersionCloneRequest{} 177 178 reqData := compactJSON(loadFixtureBytes("testdata/TestConfigurationVersionClone/ConfigurationVersionClone.json")) 179 err = json.Unmarshal([]byte(reqData), &req) 180 require.NoError(t, err) 181 182 tests := map[string]struct { 183 params CreateConfigurationVersionCloneRequest 184 prop *CreateConfigurationVersionCloneRequest 185 responseStatus int 186 responseBody string 187 expectedPath string 188 expectedResponse *CreateConfigurationVersionCloneResponse 189 withError error 190 headers http.Header 191 }{ 192 "201 Created": { 193 params: CreateConfigurationVersionCloneRequest{ 194 ConfigID: 43253, 195 CreateFromVersion: 3, 196 }, 197 headers: http.Header{ 198 "Content-Type": []string{"application/json;charset=UTF-8"}, 199 }, 200 responseStatus: http.StatusCreated, 201 responseBody: respData, 202 expectedResponse: &result, 203 expectedPath: "/appsec/v1/configs/43253/versions", 204 }, 205 "500 internal server error": { 206 params: CreateConfigurationVersionCloneRequest{ 207 ConfigID: 43253, 208 CreateFromVersion: 3, 209 }, 210 responseStatus: http.StatusInternalServerError, 211 responseBody: ` 212 { 213 "type": "internal_error", 214 "title": "Internal Server Error", 215 "detail": "Error creating ConfigurationVersionClone" 216 }`, 217 expectedPath: "/appsec/v1/configs/43253/versions", 218 withError: &Error{ 219 Type: "internal_error", 220 Title: "Internal Server Error", 221 Detail: "Error creating ConfigurationVersionClone", 222 StatusCode: http.StatusInternalServerError, 223 }, 224 }, 225 } 226 227 for name, test := range tests { 228 t.Run(name, func(t *testing.T) { 229 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 230 assert.Equal(t, http.MethodPost, r.Method) 231 w.WriteHeader(test.responseStatus) 232 if len(test.responseBody) > 0 { 233 _, err := w.Write([]byte(test.responseBody)) 234 assert.NoError(t, err) 235 } 236 })) 237 client := mockAPIClient(t, mockServer) 238 result, err := client.CreateConfigurationVersionClone( 239 session.ContextWithOptions( 240 context.Background(), 241 session.WithContextHeaders(test.headers)), test.params) 242 if test.withError != nil { 243 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 244 return 245 } 246 require.NoError(t, err) 247 assert.Equal(t, test.expectedResponse, result) 248 }) 249 } 250 }