Repository navigation
Expand file tree
/
Copy pathhuma_logging_test.go
More file actions
110 lines (92 loc) · 3.31 KB
/
Copy pathhuma_logging_test.go
File metadata and controls
110 lines (92 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"bytes"
"net/http/httptest"
"os"
"strings"
"testing"
"golbat/config"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func TestHumaScanRequestLogging(t *testing.T) {
prevLogging := config.Config.Logging.ApiRequestLogging
prevSecret := config.Config.ApiSecret
config.Config.Logging.ApiRequestLogging = true
config.Config.ApiSecret = "" // disable auth so the request reaches the handler
defer func() {
config.Config.Logging.ApiRequestLogging = prevLogging
config.Config.ApiSecret = prevSecret
}()
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(os.Stderr)
gin.SetMode(gin.TestMode)
r := gin.New()
api := setupHumaAPI(r)
registerHumaRoutes(api)
// A deliberately bad body (lat/lon ok, but an unknown field) to confirm we log
// the rejected payload AND the 422 reason that the handler never sees.
body := `{"min":{"lat":1.25,"lon":2.5},"max":{"lat":3,"lon":4},"filters":[],"bogus":1}`
req := httptest.NewRequest("POST", "/api/pokemon/v2/scan", strings.NewReader(body))
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
logged := buf.String()
for _, want := range []string{
"[huma api]",
"/api/pokemon/v2/scan",
"1.25", // request body coordinate captured (logrus escapes quotes)
"bogus", // the offending field is visible in the request
"422", // status captured
"unexpected property", // Huma's rejection reason captured from the response
} {
if !strings.Contains(logged, want) {
t.Errorf("log missing %q.\n--- log ---\n%s", want, logged)
}
}
}
// TestHumaApiRequestLoggingCoversNonScanEndpoints confirms the logger now captures
// every Huma /api operation, not just the scan endpoints.
func TestHumaApiRequestLoggingCoversNonScanEndpoints(t *testing.T) {
prevLogging := config.Config.Logging.ApiRequestLogging
prevSecret := config.Config.ApiSecret
config.Config.Logging.ApiRequestLogging = true
config.Config.ApiSecret = ""
defer func() {
config.Config.Logging.ApiRequestLogging = prevLogging
config.Config.ApiSecret = prevSecret
}()
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(os.Stderr)
gin.SetMode(gin.TestMode)
r := gin.New()
api := setupHumaAPI(r)
registerPokemonReadRoutes(api) // includes GET /api/pokemon/available
req := httptest.NewRequest("GET", "/api/pokemon/available", nil)
r.ServeHTTP(httptest.NewRecorder(), req)
logged := buf.String()
for _, want := range []string{"[huma api]", "/api/pokemon/available"} {
if !strings.Contains(logged, want) {
t.Errorf("log missing %q for a non-scan endpoint.\n--- log ---\n%s", want, logged)
}
}
}
func TestHumaScanRequestLoggingSilentWhenDisabled(t *testing.T) {
prevLogging := config.Config.Logging.ApiRequestLogging
config.Config.Logging.ApiRequestLogging = false
defer func() { config.Config.Logging.ApiRequestLogging = prevLogging }()
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(os.Stderr)
gin.SetMode(gin.TestMode)
r := gin.New()
api := setupHumaAPI(r)
registerHumaRoutes(api)
req := httptest.NewRequest("POST", "/api/pokemon/v2/scan",
strings.NewReader(`{"min":{"lat":0,"lon":0},"max":{"lat":1,"lon":1},"filters":[]}`))
r.ServeHTTP(httptest.NewRecorder(), req)
if strings.Contains(buf.String(), "[huma api]") {
t.Errorf("expected no scan log when debug off, got: %s", buf.String())
}
}