(t *testing.T)
| 376 | } |
| 377 | |
| 378 | func TestIsHandledByHttpgrpcServer(t *testing.T) { |
| 379 | t.Run("false by default", func(t *testing.T) { |
| 380 | require.False(t, IsHandledByHttpgrpcServer(context.Background())) |
| 381 | }) |
| 382 | |
| 383 | const testHeader = "X-HandledByHttpgrpcServer" |
| 384 | |
| 385 | // Handler will return value returned by IsHandledByHttpgrpcServer in test header. |
| 386 | handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 387 | w.Header().Set(testHeader, strconv.FormatBool(IsHandledByHttpgrpcServer(r.Context()))) |
| 388 | w.WriteHeader(200) |
| 389 | }) |
| 390 | |
| 391 | t.Run("handler runs outside of httpgrpc.Server", func(t *testing.T) { |
| 392 | rec := httptest.NewRecorder() |
| 393 | handler(rec, &http.Request{}) |
| 394 | require.Equal(t, "false", rec.Header().Get(testHeader)) |
| 395 | }) |
| 396 | |
| 397 | t.Run("handler runs from httpgrpc.Server", func(t *testing.T) { |
| 398 | s := NewServer(handler) |
| 399 | resp, err := s.Handle(context.Background(), &httpgrpc.HTTPRequest{Method: "GET", Url: "/test"}) |
| 400 | require.NoError(t, err) |
| 401 | require.NotNil(t, resp) |
| 402 | |
| 403 | require.Equal(t, []*httpgrpc.Header{{Key: http.CanonicalHeaderKey(testHeader), Values: []string{"true"}}}, resp.Headers) |
| 404 | }) |
| 405 | } |
nothing calls this directly
no test coverage detected