github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/papi/clientsettings_test.go (about) 1 package papi 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestPapi_GetClientSettings(t *testing.T) { 15 tests := map[string]struct { 16 responseStatus int 17 responseBody string 18 expectedPath string 19 expectedResponse *ClientSettingsBody 20 withError func(*testing.T, error) 21 }{ 22 "200 OK": { 23 responseStatus: http.StatusOK, 24 responseBody: ` 25 { 26 "ruleFormat": "v2015-08-08", 27 "usePrefixes": true 28 } 29 `, 30 expectedPath: "/papi/v1/client-settings", 31 expectedResponse: &ClientSettingsBody{ 32 RuleFormat: "v2015-08-08", 33 UsePrefixes: true, 34 }, 35 }, 36 "500 server error": { 37 responseStatus: http.StatusInternalServerError, 38 responseBody: ` 39 { 40 "type": "internal_error", 41 "title": "Internal Server Error", 42 "detail": "Error fetching client settings", 43 "status": 500 44 } 45 `, 46 expectedPath: "/papi/v1/client-settings", 47 withError: func(t *testing.T, err error) { 48 want := &Error{ 49 Type: "internal_error", 50 Title: "Internal Server Error", 51 Detail: "Error fetching client settings", 52 StatusCode: http.StatusInternalServerError, 53 } 54 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 55 }, 56 }, 57 } 58 59 for name, test := range tests { 60 t.Run(name, func(t *testing.T) { 61 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 62 assert.Equal(t, test.expectedPath, r.URL.String()) 63 assert.Equal(t, http.MethodGet, r.Method) 64 w.WriteHeader(test.responseStatus) 65 _, err := w.Write([]byte(test.responseBody)) 66 assert.NoError(t, err) 67 })) 68 client := mockAPIClient(t, mockServer) 69 result, err := client.GetClientSettings(context.Background()) 70 if test.withError != nil { 71 test.withError(t, err) 72 return 73 } 74 require.NoError(t, err) 75 assert.Equal(t, test.expectedResponse, result) 76 }) 77 } 78 } 79 80 func TestPapi_UpdateClientSettings(t *testing.T) { 81 tests := map[string]struct { 82 params ClientSettingsBody 83 responseStatus int 84 responseBody string 85 expectedPath string 86 expectedResponse *ClientSettingsBody 87 withError func(*testing.T, error) 88 }{ 89 "200 OK": { 90 params: ClientSettingsBody{ 91 RuleFormat: "v2015-08-08", 92 UsePrefixes: true, 93 }, 94 responseStatus: http.StatusOK, 95 responseBody: ` 96 { 97 "ruleFormat": "v2015-08-08", 98 "usePrefixes": true 99 } 100 `, 101 expectedPath: "/papi/v1/client-settings", 102 expectedResponse: &ClientSettingsBody{ 103 RuleFormat: "v2015-08-08", 104 UsePrefixes: true, 105 }, 106 }, 107 "500 OK": { 108 params: ClientSettingsBody{ 109 RuleFormat: "v2015-08-08", 110 UsePrefixes: true, 111 }, 112 responseStatus: http.StatusInternalServerError, 113 responseBody: ` 114 { 115 "type": "internal_error", 116 "title": "Internal Server Error", 117 "detail": "Error fetching client settings", 118 "status": 500 119 } 120 `, 121 expectedPath: "/papi/v1/client-settings", 122 withError: func(t *testing.T, err error) { 123 want := &Error{ 124 Type: "internal_error", 125 Title: "Internal Server Error", 126 Detail: "Error fetching client settings", 127 StatusCode: http.StatusInternalServerError, 128 } 129 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 130 }, 131 }, 132 } 133 134 for name, test := range tests { 135 t.Run(name, func(t *testing.T) { 136 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 137 assert.Equal(t, test.expectedPath, r.URL.String()) 138 assert.Equal(t, http.MethodPut, r.Method) 139 w.WriteHeader(test.responseStatus) 140 _, err := w.Write([]byte(test.responseBody)) 141 assert.NoError(t, err) 142 })) 143 client := mockAPIClient(t, mockServer) 144 result, err := client.UpdateClientSettings(context.Background(), test.params) 145 if test.withError != nil { 146 test.withError(t, err) 147 return 148 } 149 require.NoError(t, err) 150 assert.Equal(t, test.expectedResponse, result) 151 }) 152 } 153 }