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