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

Function TestError

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

Source from the content-addressed store, hash-verified

91}
92
93func TestError(t *testing.T) {
94 for _, doNotLog := range []bool{true, false} {
95 var stat string
96 if !doNotLog {
97 stat = "not "
98 }
99 t.Run(fmt.Sprintf("test header when DoNotLogErrorHeaderKey is %spresent", stat), func(t *testing.T) {
100 server, err := newTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
101 if doNotLog {
102 w.Header().Set(DoNotLogErrorHeaderKey, "true")
103 }
104 // Does a Fprintln, injecting a newline.
105 http.Error(w, "foo", http.StatusInternalServerError)
106 }))
107 require.NoError(t, err)
108 defer server.grpcServer.GracefulStop()
109
110 client, err := NewClient(server.URL)
111 require.NoError(t, err)
112
113 req, err := http.NewRequest("GET", "/hello", &bytes.Buffer{})
114 require.NoError(t, err)
115
116 req = req.WithContext(user.InjectOrgID(context.Background(), "1"))
117 recorder := httptest.NewRecorder()
118 client.ServeHTTP(recorder, req)
119
120 assert.Equal(t, "foo\n", recorder.Body.String())
121 assert.Equal(t, 500, recorder.Code)
122 assert.NotContains(t, recorder.Header(), DoNotLogErrorHeaderKey)
123 })
124 }
125}
126
127func TestServerHandleDoNotLogError(t *testing.T) {
128 testCases := map[string]struct {

Callers

nothing calls this directly

Calls 10

ServeHTTPMethod · 0.95
InjectOrgIDFunction · 0.92
newTestServerFunction · 0.85
RunMethod · 0.80
NewClientFunction · 0.70
SetMethod · 0.65
StringMethod · 0.65
HeaderMethod · 0.45
ErrorMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected