MCPcopy
hub / github.com/labstack/echo / TestRequestLoggerError

Function TestRequestLoggerError

middleware/request_logger_test.go:73–114  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

71}
72
73func TestRequestLoggerError(t *testing.T) {
74 old := slog.Default()
75 t.Cleanup(func() {
76 slog.SetDefault(old)
77 })
78
79 e := echo.New()
80 buf := new(bytes.Buffer)
81 e.Logger = slog.New(slog.NewJSONHandler(buf, nil))
82 e.Use(RequestLogger())
83
84 e.GET("/test", func(c *echo.Context) error {
85 return errors.New("nope")
86 })
87 req := httptest.NewRequest(http.MethodGet, "/test", nil)
88 rec := httptest.NewRecorder()
89 e.ServeHTTP(rec, req)
90
91 logAttrs := map[string]any{}
92 assert.NoError(t, json.Unmarshal(buf.Bytes(), &logAttrs))
93 logAttrs["latency"] = 123
94 logAttrs["time"] = "x"
95
96 expect := map[string]any{
97 "level": "ERROR",
98 "msg": "REQUEST_ERROR",
99 "method": "GET",
100 "uri": "/test",
101 "status": float64(500),
102 "bytes_in": "",
103 "host": "example.com",
104 "bytes_out": float64(36.0),
105 "user_agent": "",
106 "remote_ip": "192.0.2.1",
107 "request_id": "",
108 "error": "nope",
109
110 "latency": 123,
111 "time": "x",
112 }
113 assert.Equal(t, expect, logAttrs)
114}
115
116func TestRequestLoggerWithConfig(t *testing.T) {
117 e := echo.New()

Callers

nothing calls this directly

Calls 4

RequestLoggerFunction · 0.85
ServeHTTPMethod · 0.80
UseMethod · 0.45
GETMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…