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

Function TestTestRequestLogger

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

Source from the content-addressed store, hash-verified

467}
468
469func TestTestRequestLogger(t *testing.T) {
470 var testCases = []struct {
471 name string
472 whenStatus int
473 whenError error
474 expectStatus string
475 expectError string
476 }{
477 {
478 name: "ok",
479 whenStatus: http.StatusTeapot,
480 expectStatus: "418",
481 },
482 {
483 name: "error",
484 whenError: echo.NewHTTPError(http.StatusBadGateway, "bad gw"),
485 expectStatus: "502",
486 expectError: `"error":"code=502, message=bad gw"`,
487 },
488 }
489 for _, tc := range testCases {
490 t.Run(tc.name, func(t *testing.T) {
491 e := echo.New()
492 buf := new(bytes.Buffer)
493 e.Logger = slog.New(slog.NewJSONHandler(buf, nil))
494
495 e.Use(RequestLogger())
496 e.POST("/test", func(c *echo.Context) error {
497 if tc.whenError != nil {
498 return tc.whenError
499 }
500 return c.String(tc.whenStatus, "OK")
501 })
502
503 f := make(url.Values)
504 f.Set("csrf", "token")
505 f.Set("multiple", "1")
506 f.Add("multiple", "2")
507 reader := strings.NewReader(f.Encode())
508 req := httptest.NewRequest(http.MethodPost, "/test?lang=en&checked=1&checked=2", reader)
509 req.Header.Set("Referer", "https://echo.labstack.com/")
510 req.Header.Set("User-Agent", "curl/7.68.0")
511 req.Header.Set(echo.HeaderContentLength, strconv.Itoa(int(reader.Size())))
512 req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm)
513 req.Header.Set(echo.HeaderXRealIP, "8.8.8.8")
514 req.Header.Set(echo.HeaderXRequestID, "MY_ID")
515
516 rec := httptest.NewRecorder()
517
518 e.ServeHTTP(rec, req)
519
520 rawlog := buf.Bytes()
521 if tc.expectError != "" {
522 assert.Contains(t, string(rawlog), `"level":"ERROR"`)
523 assert.Contains(t, string(rawlog), `"msg":"REQUEST_ERROR"`)
524 assert.Contains(t, string(rawlog), tc.expectError)
525 } else {
526 assert.Contains(t, string(rawlog), `"level":"INFO"`)

Callers

nothing calls this directly

Calls 7

RequestLoggerFunction · 0.85
SetMethod · 0.80
ServeHTTPMethod · 0.80
AddMethod · 0.65
UseMethod · 0.45
POSTMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…