MCPcopy
hub / github.com/gin-gonic/gin / TestLoggerWithConfigFormatting

Function TestLoggerWithConfigFormatting

logger_test.go:182–232  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

180}
181
182func TestLoggerWithConfigFormatting(t *testing.T) {
183 var gotParam LogFormatterParams
184 var gotKeys map[any]any
185 buffer := new(strings.Builder)
186
187 router := New()
188 router.engine.trustedCIDRs, _ = router.engine.prepareTrustedCIDRs()
189
190 router.Use(LoggerWithConfig(LoggerConfig{
191 Output: buffer,
192 Formatter: func(param LogFormatterParams) string {
193 // for assert test
194 gotParam = param
195
196 return fmt.Sprintf("[FORMATTER TEST] %v | %3d | %13v | %15s | %-7s %s\n%s",
197 param.TimeStamp.Format("2006/01/02 - 15:04:05"),
198 param.StatusCode,
199 param.Latency,
200 param.ClientIP,
201 param.Method,
202 param.Path,
203 param.ErrorMessage,
204 )
205 },
206 }))
207 router.GET("/example", func(c *Context) {
208 // set dummy ClientIP
209 c.Request.Header.Set("X-Forwarded-For", "20.20.20.20")
210 gotKeys = c.Keys
211 time.Sleep(time.Millisecond)
212 })
213 PerformRequest(router, http.MethodGet, "/example?a=100")
214
215 // output test
216 assert.Contains(t, buffer.String(), "[FORMATTER TEST]")
217 assert.Contains(t, buffer.String(), "200")
218 assert.Contains(t, buffer.String(), http.MethodGet)
219 assert.Contains(t, buffer.String(), "/example")
220 assert.Contains(t, buffer.String(), "a=100")
221
222 // LogFormatterParams test
223 assert.NotNil(t, gotParam.Request)
224 assert.NotEmpty(t, gotParam.TimeStamp)
225 assert.Equal(t, 200, gotParam.StatusCode)
226 assert.NotEmpty(t, gotParam.Latency)
227 assert.Equal(t, "20.20.20.20", gotParam.ClientIP)
228 assert.Equal(t, http.MethodGet, gotParam.Method)
229 assert.Equal(t, "/example?a=100", gotParam.Path)
230 assert.Empty(t, gotParam.ErrorMessage)
231 assert.Equal(t, gotKeys, gotParam.Keys)
232}
233
234func TestDefaultLogFormatter(t *testing.T) {
235 timeStamp := time.Unix(1544173902, 0).UTC()

Callers

nothing calls this directly

Calls 8

NewFunction · 0.85
LoggerWithConfigFunction · 0.85
PerformRequestFunction · 0.85
prepareTrustedCIDRsMethod · 0.80
SetMethod · 0.80
UseMethod · 0.65
GETMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected