add unit test
This commit is contained in:
@@ -112,3 +112,113 @@ func TestGetSlogCustomEchoMiddleware(t *testing.T) {
|
||||
assert.Equal(t, "middleware", l.Logs[0]["location"])
|
||||
assert.Equal(t, uri, l.Logs[1]["uri"])
|
||||
}
|
||||
|
||||
func TestGetDebugMiddleware(t *testing.T) {
|
||||
// Create a test logger
|
||||
l := &logger.TestLogger{
|
||||
T: t,
|
||||
}
|
||||
sl := slog.New(l)
|
||||
|
||||
// Get the middleware function
|
||||
handlerFunc := getDebugMiddleware(sl)
|
||||
require.NotNil(t, handlerFunc)
|
||||
|
||||
// Test with DEBUG=false (default case)
|
||||
t.Run("debug disabled", func(t *testing.T) {
|
||||
// Save original env and restore after test
|
||||
origDebug := os.Getenv("DEBUG")
|
||||
defer os.Setenv("DEBUG", origDebug)
|
||||
|
||||
os.Setenv("DEBUG", "false")
|
||||
|
||||
// Reset logs
|
||||
l.Logs = nil
|
||||
|
||||
// Create a middleware chain with our test function
|
||||
mid := func(c echo.Context) error {
|
||||
sl.Info("in middleware", "location", "middleware")
|
||||
return nil
|
||||
}
|
||||
myFunc := handlerFunc(mid)
|
||||
require.NotNil(t, myFunc)
|
||||
|
||||
// Make a test request
|
||||
uri := "/example"
|
||||
req := httptest.NewRequest(http.MethodGet, uri, nil)
|
||||
req.Header.Set("X-Test-Header", "test-value")
|
||||
req.AddCookie(&http.Cookie{Name: "test-cookie", Value: "cookie-value"})
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := echo.New().NewContext(req, rec)
|
||||
|
||||
// Execute middleware
|
||||
err := myFunc(ctx)
|
||||
require.Nil(t, err)
|
||||
|
||||
// Verify logs - should only contain middleware log, no debug info
|
||||
assert.Len(t, l.Logs, 1)
|
||||
assert.Equal(t, "middleware", l.Logs[0]["location"])
|
||||
})
|
||||
|
||||
// Test with DEBUG=true
|
||||
t.Run("debug enabled", func(t *testing.T) {
|
||||
// Save original env and restore after test
|
||||
origDebug := os.Getenv("DEBUG")
|
||||
defer os.Setenv("DEBUG", origDebug)
|
||||
|
||||
os.Setenv("DEBUG", "true")
|
||||
|
||||
// Reset logs
|
||||
l.Logs = nil
|
||||
|
||||
// Create a middleware chain with our test function
|
||||
mid := func(c echo.Context) error {
|
||||
sl.Info("in middleware", "location", "middleware")
|
||||
return nil
|
||||
}
|
||||
myFunc := handlerFunc(mid)
|
||||
require.NotNil(t, myFunc)
|
||||
|
||||
// Make a test request with headers and cookies
|
||||
uri := "/example?param=value"
|
||||
req := httptest.NewRequest(http.MethodPost, uri, strings.NewReader("hello"))
|
||||
req.Header.Set("X-Test-Header", "test-value")
|
||||
req.AddCookie(&http.Cookie{Name: "test-cookie", Value: "cookie-value"})
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := echo.New().NewContext(req, rec)
|
||||
|
||||
// Execute middleware
|
||||
err := myFunc(ctx)
|
||||
require.Nil(t, err)
|
||||
|
||||
// Verify logs - should contain debug info and middleware log
|
||||
assert.Len(t, l.Logs, 2)
|
||||
|
||||
// First log should have details attribute
|
||||
_, hasDetails := l.Logs[0]["details"]
|
||||
assert.True(t, hasDetails, "First log should have 'details' attribute")
|
||||
|
||||
// Assuming details is available, check its structure
|
||||
if details, ok := l.Logs[0]["details"].(map[string]interface{}); ok {
|
||||
// Check method was logged
|
||||
assert.Equal(t, http.MethodPost, details["method"])
|
||||
|
||||
// Check URL info was logged
|
||||
if urlInfo, ok := details["url"].(map[string]interface{}); ok {
|
||||
assert.Contains(t, urlInfo["full"].(string), "/example")
|
||||
assert.Equal(t, "/example", urlInfo["path"])
|
||||
assert.Contains(t, urlInfo["query"].(string), "param=value")
|
||||
}
|
||||
|
||||
// Check headers were logged
|
||||
if headers, ok := details["headers"].(map[string][]string); ok {
|
||||
assert.Contains(t, headers, "X-Test-Header")
|
||||
}
|
||||
}
|
||||
|
||||
// Second log should be middleware log
|
||||
assert.Equal(t, "middleware", l.Logs[1]["location"])
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user