-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathmain_test.go
More file actions
58 lines (49 loc) · 1.34 KB
/
main_test.go
File metadata and controls
58 lines (49 loc) · 1.34 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
package main
import (
"bytes"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func TestLoggerMiddlewareRedactsSensitiveContext(t *testing.T) {
gin.SetMode(gin.TestMode)
var logs bytes.Buffer
previousOutput := log.Writer()
previousFlags := log.Flags()
log.SetOutput(&logs)
log.SetFlags(0)
t.Cleanup(func() {
log.SetOutput(previousOutput)
log.SetFlags(previousFlags)
})
r := gin.New()
r.Use(loggerMiddleware())
r.GET("/probe", func(c *gin.Context) {
c.Set("x-account-email", "alice@example.com")
c.Set("x-account-proxy", "http://user:secret@proxy.example:8080")
c.Set("x-model", "gpt-5.5")
c.Set("x-reasoning-effort", "medium")
c.Set("x-service-tier", "fast")
c.Status(http.StatusAccepted)
})
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/probe", nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got := logs.String()
for _, forbidden := range []string{"alice@example.com", "secret"} {
if strings.Contains(got, forbidden) {
t.Fatalf("log output leaked %q: %s", forbidden, got)
}
}
for _, expected := range []string{"GET /probe 202", "gpt-5.5", "effort=medium", "fast"} {
if !strings.Contains(got, expected) {
t.Fatalf("log output missing %q: %s", expected, got)
}
}
}