MCPcopy
hub / github.com/rs/zerolog / TestRequestIDHandler

Function TestRequestIDHandler

hlog/hlog_test.go:193–216  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

191}
192
193func TestRequestIDHandler(t *testing.T) {
194 out := &bytes.Buffer{}
195 r := &http.Request{
196 Header: http.Header{
197 "Referer": []string{"http://foo.com/bar"},
198 },
199 }
200 h := RequestIDHandler("id", "Request-Id")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
201 id, ok := IDFromRequest(r)
202 if !ok {
203 t.Fatal("Missing id in request")
204 }
205 if want, got := id.String(), w.Header().Get("Request-Id"); got != want {
206 t.Errorf("Invalid Request-Id header, got: %s, want: %s", got, want)
207 }
208 l := FromRequest(r)
209 l.Log().Msg("")
210 if want, got := fmt.Sprintf(`{"id":"%s"}`+"\n", id), decodeIfBinary(out); want != got {
211 t.Errorf("Invalid log output, got: %s, want: %s", got, want)
212 }
213 }))
214 h = NewHandler(zerolog.New(out))(h)
215 h.ServeHTTP(httptest.NewRecorder(), r)
216}
217
218func TestCustomHeaderHandler(t *testing.T) {
219 out := &bytes.Buffer{}

Callers

nothing calls this directly

Calls 10

NewFunction · 0.92
RequestIDHandlerFunction · 0.85
IDFromRequestFunction · 0.85
FromRequestFunction · 0.85
decodeIfBinaryFunction · 0.85
NewHandlerFunction · 0.85
FatalMethod · 0.80
MsgMethod · 0.80
LogMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected