mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
🧪 fix: Logger Middleware tests to use regex for time validation (#3392)
* test: rewrite logger format tests to use regex for time validation * test: rewrite logger format tests to use regex for time validation
This commit is contained in:
parent
e7681ee402
commit
62b099879a
@ -11,6 +11,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
@ -25,6 +26,11 @@ import (
|
|||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
pathFooBar = "/?foo=bar"
|
||||||
|
httpProto = "HTTP/1.1"
|
||||||
|
)
|
||||||
|
|
||||||
func benchmarkSetup(b *testing.B, app *fiber.App, uri string) {
|
func benchmarkSetup(b *testing.B, app *fiber.App, uri string) {
|
||||||
b.Helper()
|
b.Helper()
|
||||||
|
|
||||||
@ -459,7 +465,7 @@ func Test_Logger_All(t *testing.T) {
|
|||||||
// Alias colors
|
// Alias colors
|
||||||
colors := app.Config().ColorScheme
|
colors := app.Config().ColorScheme
|
||||||
|
|
||||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil))
|
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, pathFooBar, nil))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
||||||
|
|
||||||
@ -473,23 +479,21 @@ func Test_Logger_CLF_Format(t *testing.T) {
|
|||||||
defer bytebufferpool.Put(buf)
|
defer bytebufferpool.Put(buf)
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
|
||||||
app.Use(New(Config{
|
app.Use(New(Config{
|
||||||
Format: CommonFormat,
|
Format: CommonFormat,
|
||||||
Stream: buf,
|
Stream: buf,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil))
|
method := fiber.MethodGet
|
||||||
require.NoError(t, err)
|
status := fiber.StatusNotFound
|
||||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
bytesSent := 0
|
||||||
|
|
||||||
expected := fmt.Sprintf("0.0.0.0 - - [%s] \"%s %s %s\" %d %d\n",
|
resp, err := app.Test(httptest.NewRequest(method, pathFooBar, nil))
|
||||||
time.Now().Format("15:04:05"),
|
require.NoError(t, err)
|
||||||
fiber.MethodGet, "/?foo=bar", "HTTP/1.1",
|
require.Equal(t, status, resp.StatusCode)
|
||||||
fiber.StatusNotFound,
|
|
||||||
0)
|
pattern := fmt.Sprintf(`0\.0\.0\.0 - - \[\d{2}:\d{2}:\d{2}\] "%s %s %s" %d %d`, method, regexp.QuoteMeta(pathFooBar), httpProto, status, bytesSent)
|
||||||
logResponse := buf.String()
|
require.Regexp(t, pattern, buf.String())
|
||||||
require.Equal(t, expected, logResponse)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Logger_Combined_CLF_Format(t *testing.T) {
|
func Test_Logger_Combined_CLF_Format(t *testing.T) {
|
||||||
@ -498,29 +502,27 @@ func Test_Logger_Combined_CLF_Format(t *testing.T) {
|
|||||||
defer bytebufferpool.Put(buf)
|
defer bytebufferpool.Put(buf)
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
|
||||||
app.Use(New(Config{
|
app.Use(New(Config{
|
||||||
Format: CombinedFormat,
|
Format: CombinedFormat,
|
||||||
Stream: buf,
|
Stream: buf,
|
||||||
}))
|
}))
|
||||||
const expectedUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
|
|
||||||
const expectedReferer = "http://example.com"
|
method := fiber.MethodGet
|
||||||
req := httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil)
|
status := fiber.StatusNotFound
|
||||||
req.Header.Set("Referer", expectedReferer)
|
bytesSent := 0
|
||||||
req.Header.Set("User-Agent", expectedUA)
|
referer := "http://example.com"
|
||||||
|
ua := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
|
||||||
|
|
||||||
|
req := httptest.NewRequest(method, pathFooBar, nil)
|
||||||
|
req.Header.Set("Referer", referer)
|
||||||
|
req.Header.Set("User-Agent", ua)
|
||||||
|
|
||||||
resp, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
require.Equal(t, status, resp.StatusCode)
|
||||||
|
|
||||||
expected := fmt.Sprintf("0.0.0.0 - - [%s] %q %d %d %q %q\n",
|
pattern := fmt.Sprintf(`0\.0\.0\.0 - - \[\d{2}:\d{2}:\d{2}\] "%s %s %s" %d %d "%s" "%s"`, method, regexp.QuoteMeta(pathFooBar), httpProto, status, bytesSent, regexp.QuoteMeta(referer), regexp.QuoteMeta(ua)) //nolint:gocritic // double quoting for regex and string is not needed
|
||||||
time.Now().Format("15:04:05"),
|
require.Regexp(t, pattern, buf.String())
|
||||||
fmt.Sprintf("%s %s %s", fiber.MethodGet, "/?foo=bar", "HTTP/1.1"),
|
|
||||||
fiber.StatusNotFound,
|
|
||||||
0,
|
|
||||||
expectedReferer,
|
|
||||||
expectedUA)
|
|
||||||
logResponse := buf.String()
|
|
||||||
require.Equal(t, expected, logResponse)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Logger_Json_Format(t *testing.T) {
|
func Test_Logger_Json_Format(t *testing.T) {
|
||||||
@ -529,28 +531,23 @@ func Test_Logger_Json_Format(t *testing.T) {
|
|||||||
defer bytebufferpool.Put(buf)
|
defer bytebufferpool.Put(buf)
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
|
||||||
app.Use(New(Config{
|
app.Use(New(Config{
|
||||||
Format: JSONFormat,
|
Format: JSONFormat,
|
||||||
Stream: buf,
|
Stream: buf,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
req := httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil)
|
method := fiber.MethodGet
|
||||||
|
status := fiber.StatusNotFound
|
||||||
|
ip := "0.0.0.0"
|
||||||
|
bytesSent := 0
|
||||||
|
|
||||||
|
req := httptest.NewRequest(method, pathFooBar, nil)
|
||||||
resp, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
require.Equal(t, status, resp.StatusCode)
|
||||||
|
|
||||||
expected := fmt.Sprintf(
|
pattern := fmt.Sprintf(`\{"time":"\d{2}:\d{2}:\d{2}","ip":"%s","method":%q,"url":"%s","status":%d,"bytesSent":%d\}`, regexp.QuoteMeta(ip), method, regexp.QuoteMeta(pathFooBar), status, bytesSent) //nolint:gocritic // double quoting for regex and string is not needed
|
||||||
"{\"time\":%q,\"ip\":%q,\"method\":%q,\"url\":%q,\"status\":%d,\"bytesSent\":%d}\n",
|
require.Regexp(t, pattern, buf.String())
|
||||||
time.Now().Format("15:04:05"),
|
|
||||||
"0.0.0.0",
|
|
||||||
fiber.MethodGet,
|
|
||||||
"/?foo=bar",
|
|
||||||
fiber.StatusNotFound,
|
|
||||||
0,
|
|
||||||
)
|
|
||||||
logResponse := buf.String()
|
|
||||||
require.Equal(t, expected, logResponse)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Logger_ECS_Format(t *testing.T) {
|
func Test_Logger_ECS_Format(t *testing.T) {
|
||||||
@ -559,30 +556,24 @@ func Test_Logger_ECS_Format(t *testing.T) {
|
|||||||
defer bytebufferpool.Put(buf)
|
defer bytebufferpool.Put(buf)
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
|
||||||
app.Use(New(Config{
|
app.Use(New(Config{
|
||||||
Format: ECSFormat,
|
Format: ECSFormat,
|
||||||
Stream: buf,
|
Stream: buf,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
req := httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil)
|
method := fiber.MethodGet
|
||||||
|
status := fiber.StatusNotFound
|
||||||
|
ip := "0.0.0.0"
|
||||||
|
bytesSent := 0
|
||||||
|
msg := fmt.Sprintf("%s %s responded with %d", method, pathFooBar, status)
|
||||||
|
|
||||||
|
req := httptest.NewRequest(method, pathFooBar, nil)
|
||||||
resp, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
require.Equal(t, status, resp.StatusCode)
|
||||||
|
|
||||||
expected := fmt.Sprintf(
|
pattern := fmt.Sprintf(`\{"@timestamp":"\d{2}:\d{2}:\d{2}","ecs":\{"version":"1.6.0"\},"client":\{"ip":"%s"\},"http":\{"request":\{"method":%q,"url":"%s","protocol":%q\},"response":\{"status_code":%d,"body":\{"bytes":%d\}\}\},"log":\{"level":"INFO","logger":"fiber"\},"message":"%s"\}`, regexp.QuoteMeta(ip), method, regexp.QuoteMeta(pathFooBar), httpProto, status, bytesSent, regexp.QuoteMeta(msg)) //nolint:gocritic // double quoting for regex and string is not needed
|
||||||
"{\"@timestamp\":%q,\"ecs\":{\"version\":\"1.6.0\"},\"client\":{\"ip\":%q},\"http\":{\"request\":{\"method\":%q,\"url\":%q,\"protocol\":%q},\"response\":{\"status_code\":%d,\"body\":{\"bytes\":%d}}},\"log\":{\"level\":\"INFO\",\"logger\":\"fiber\"},\"message\":%q}\n",
|
require.Regexp(t, pattern, buf.String())
|
||||||
time.Now().Format("15:04:05"),
|
|
||||||
"0.0.0.0",
|
|
||||||
fiber.MethodGet,
|
|
||||||
"/?foo=bar",
|
|
||||||
"HTTP/1.1",
|
|
||||||
fiber.StatusNotFound,
|
|
||||||
0,
|
|
||||||
fmt.Sprintf("%s %s responded with %d", fiber.MethodGet, "/?foo=bar", fiber.StatusNotFound),
|
|
||||||
)
|
|
||||||
logResponse := buf.String()
|
|
||||||
require.Equal(t, expected, logResponse)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLatencyTimeUnits() []struct {
|
func getLatencyTimeUnits() []struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user