(t *testing.T)
| 329 | } |
| 330 | |
| 331 | func 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 | |
| 378 | func TestIsHandledByHttpgrpcServer(t *testing.T) { |
| 379 | t.Run("false by default", func(t *testing.T) { |
nothing calls this directly
no test coverage detected