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

Function TestGrpcErrorsHaveCorrectMessage

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

Source from the content-addressed store, hash-verified

329}
330
331func TestGrpcErrorsHaveCorrectMessage(t *testing.T) {
332 testCases := map[string]struct {
333 responseBody string
334 errorMessageInHeader string
335
336 expectedErrorMessage string
337 }{
338 "error response with string body": {
339 responseBody: "hello world",
340 expectedErrorMessage: "rpc error: code = Code(500) desc = hello world",
341 },
342 "error response with binary body": {
343 responseBody: "\x08\x08\x12\xc7\x03the request has been rejected",
344 expectedErrorMessage: "rpc error: code = Code(500) desc = \x08\x08\x12\xc7\x03the request has been rejected",
345 },
346 "error response with binary body and provided message via header": {
347 responseBody: "\x08\x08\x12\xc7\x03the request has been rejected",
348 errorMessageInHeader: "hello world",
349 expectedErrorMessage: "rpc error: code = Code(500) desc = hello world",
350 },
351 }
352 for testName, testData := range testCases {
353 t.Run(testName, func(t *testing.T) {
354 h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
355 if testData.errorMessageInHeader != "" {
356 w.Header().Set(ErrorMessageHeaderKey, testData.errorMessageInHeader)
357 }
358 w.WriteHeader(http.StatusInternalServerError)
359 _, _ = w.Write([]byte(testData.responseBody))
360 })
361
362 s := NewServer(h)
363 req := &httpgrpc.HTTPRequest{Method: "GET", Url: "/test"}
364 resp, err := s.Handle(context.Background(), req)
365 require.Error(t, err)
366 require.Nil(t, resp)
367
368 require.Equal(t, testData.expectedErrorMessage, err.Error())
369
370 httpResp, ok := httpgrpc.HTTPResponseFromError(err)
371 require.True(t, ok)
372 // Verify that header was removed
373 require.Empty(t, httpResp.Headers)
374 })
375 }
376}
377
378func TestIsHandledByHttpgrpcServer(t *testing.T) {
379 t.Run("false by default", func(t *testing.T) {

Callers

nothing calls this directly

Calls 10

HandleMethod · 0.95
HTTPResponseFromErrorFunction · 0.92
NewServerFunction · 0.85
RunMethod · 0.80
SetMethod · 0.65
HeaderMethod · 0.45
WriteHeaderMethod · 0.45
WriteMethod · 0.45
ErrorMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected