MCPcopy
hub / github.com/grafana/dskit / TestServerHandleReturn4XXErrors

Function TestServerHandleReturn4XXErrors

httpgrpc/server/server_test.go:190–256  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

188}
189
190func TestServerHandleReturn4XXErrors(t *testing.T) {
191 testCases := map[string]struct {
192 errorCode int
193 return4xxErrors bool
194 expectedError bool
195 }{
196 "HTTPResponse with code 5xx should return an error when server creates with Return4XXErrorsOption": {
197 errorCode: http.StatusInternalServerError,
198 return4xxErrors: true,
199 expectedError: true,
200 },
201 "HTTPResponse with code 5xx should return an error when server creates without Return4XXErrorsOption": {
202 errorCode: http.StatusInternalServerError,
203 return4xxErrors: false,
204 expectedError: true,
205 },
206 "HTTPResponse with code 4xx should return an error when server creates with Return4XXErrorsOption": {
207 errorCode: http.StatusBadRequest,
208 return4xxErrors: true,
209 expectedError: true,
210 },
211 "HTTPResponse with code 4xx should not return an error when server creates without Return4XXErrorsOption": {
212 errorCode: http.StatusBadRequest,
213 return4xxErrors: false,
214 expectedError: false,
215 },
216 "HTTPResponse with code different from 5xx and 4xx should not return an error when server creates with Return4XXErrorsOption": {
217 errorCode: http.StatusNoContent,
218 return4xxErrors: true,
219 expectedError: false,
220 },
221 "HTTPResponse with code different from 5xx and 4xx should not return an error when server creates without Return4XXErrorsOption": {
222 errorCode: http.StatusNoContent,
223 return4xxErrors: false,
224 expectedError: false,
225 },
226 }
227 errMsg := "this is an error"
228 for testName, testData := range testCases {
229 t.Run(testName, func(t *testing.T) {
230 h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
231 http.Error(w, errMsg, testData.errorCode)
232 })
233
234 var serverOptions []Option
235 if testData.return4xxErrors {
236 serverOptions = []Option{WithReturn4XXErrors}
237 }
238 s := NewServer(h, serverOptions...)
239
240 req := &httpgrpc.HTTPRequest{
241 Method: "GET",
242 Url: "/test",
243 }
244 resp, err := s.Handle(context.Background(), req)
245 if testData.expectedError {
246 require.Error(t, err)
247 require.Nil(t, resp)

Callers

nothing calls this directly

Calls 6

HandleMethod · 0.95
NewServerFunction · 0.85
checkErrorFunction · 0.85
checkHTTPResponseFunction · 0.85
RunMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected