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