MCPcopy
hub / github.com/caddyserver/caddy / TestServer_LogRequest_WithTrace

Function TestServer_LogRequest_WithTrace

modules/caddyhttp/server_test.go:76–104  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

74}
75
76func TestServer_LogRequest_WithTrace(t *testing.T) {
77 s := &Server{}
78
79 extra := new(ExtraLogFields)
80 ctx := context.WithValue(context.Background(), ExtraLogFieldsCtxKey, extra)
81 extra.Add(zap.String("traceID", "1234567890abcdef"))
82 extra.Add(zap.String("spanID", "12345678"))
83
84 req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
85 rec := httptest.NewRecorder()
86 wrec := NewResponseRecorder(rec, nil, nil)
87
88 duration := 50 * time.Millisecond
89 repl := NewTestReplacer(req)
90 bodyReader := &lengthReader{Source: req.Body}
91 shouldLogCredentials := false
92
93 buf := bytes.Buffer{}
94 accLog := testLogger(buf.Write)
95 s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, shouldLogCredentials)
96
97 assert.JSONEq(t, `{
98 "msg":"handled request", "level":"info", "bytes_read":0,
99 "duration":"50ms", "resp_headers": {}, "size":0,
100 "status":0, "user_id":"",
101 "traceID":"1234567890abcdef",
102 "spanID":"12345678"
103 }`, buf.String())
104}
105
106func BenchmarkServer_LogRequest(b *testing.B) {
107 s := &Server{}

Callers

nothing calls this directly

Calls 7

logRequestMethod · 0.95
NewResponseRecorderFunction · 0.85
NewTestReplacerFunction · 0.85
testLoggerFunction · 0.85
WithValueMethod · 0.80
AddMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected