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

Function TestServerHandleDoNotLogError

httpgrpc/server/server_test.go:127–188  ·  httpgrpc/server/server_test.go::TestServerHandleDoNotLogError
(t *testing.T)

Source from the content-addressed store, hash-verified

125}
126
127func TestServerHandleDoNotLogError(t *testing.T) {
128 testCases := map[string]struct {
129 errorCode int
130 doNotLogError bool
131 expectedError bool
132 }{
133 "HTTPResponse with code 5xx and with DoNotLogError header should return a non-loggable error": {
134 errorCode: http.StatusInternalServerError,
135 doNotLogError: true,
136 expectedError: true,
137 },
138 "HTTPResponse with code 5xx and without DoNotLogError header should return a loggable error": {
139 errorCode: http.StatusInternalServerError,
140 expectedError: true,
141 },
142 "HTTPResponse with code different from 5xx and with DoNotLogError header should not return an error": {
143 errorCode: http.StatusBadRequest,
144 doNotLogError: true,
145 expectedError: false,
146 },
147 "HTTPResponse with code different from 5xx and without DoNotLogError header should not return an error": {
148 errorCode: http.StatusBadRequest,
149 expectedError: false,
150 },
151 }
152 errMsg := "this is an error"
153 for testName, testData := range testCases {
154 t.Run(testName, func(t *testing.T) {
155 h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
156 if testData.doNotLogError {
157 w.Header().Set(DoNotLogErrorHeaderKey, "true")
158 }
159 http.Error(w, errMsg, testData.errorCode)
160 })
161
162 s := NewServer(h)
163 req := &httpgrpc.HTTPRequest{
164 Method: "GET",
165 Url: "/test",
166 }
167 resp, err := s.Handle(context.Background(), req)
168 if testData.expectedError {
169 require.Error(t, err)
170 require.Nil(t, resp)
171 var optional middleware.OptionalLogging
172 if testData.doNotLogError {
173 require.ErrorAs(t, err, &optional)
174
175 actual, _ := optional.ShouldLog(context.Background())
176 require.False(t, actual)
177 } else {
178 require.False(t, errors.As(err, &optional))
179 }
180 checkError(t, err, testData.errorCode, errMsg)
181 } else {
182 require.NoError(t, err)
183 require.NotNil(t, resp)
184 checkHTTPResponse(t, resp, testData.errorCode, errMsg)

Callers

nothing calls this directly

Calls 9

HandleMethod · 0.95
ShouldLogMethod · 0.95
NewServerFunction · 0.85
checkErrorFunction · 0.85
checkHTTPResponseFunction · 0.85
RunMethod · 0.80
SetMethod · 0.65
HeaderMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected