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

Function TestIsHandledByHttpgrpcServer

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

Source from the content-addressed store, hash-verified

376}
377
378func 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}

Callers

nothing calls this directly

Calls 10

HandleMethod · 0.95
NewServerFunction · 0.85
RunMethod · 0.80
SetMethod · 0.65
GetMethod · 0.65
HeaderMethod · 0.45
ContextMethod · 0.45
WriteHeaderMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected