-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathrequest_id_test.go
More file actions
144 lines (123 loc) · 3.04 KB
/
Copy pathrequest_id_test.go
File metadata and controls
144 lines (123 loc) · 3.04 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package middleware
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
)
func maintainDefaultRequestID() func() {
original := RequestIDHeader
return func() {
RequestIDHeader = original
}
}
func TestRequestID(t *testing.T) {
tests := map[string]struct {
requestIDHeader string
request func() *http.Request
expectedResponse string
}{
"Retrieves Request Id from default header": {
"X-Request-Id",
func() *http.Request {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("X-Request-Id", "req-123456")
return req
},
"RequestID: req-123456",
},
"Retrieves Request Id from custom header": {
"X-Trace-Id",
func() *http.Request {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("X-Trace-Id", "trace:abc123")
return req
},
"RequestID: trace:abc123",
},
}
defer maintainDefaultRequestID()()
for _, test := range tests {
w := httptest.NewRecorder()
r := chi.NewRouter()
RequestIDHeader = test.requestIDHeader
r.Use(RequestID)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
requestID := GetReqID(r.Context())
response := fmt.Sprintf("RequestID: %s", requestID)
w.Write([]byte(response))
})
r.ServeHTTP(w, test.request())
if w.Body.String() != test.expectedResponse {
t.Fatalf("RequestID was not the expected value")
}
}
}
func TestRequestIDWithCustomKey(t *testing.T) {
tests := map[string]struct {
customKey string
request func() *http.Request
expectPanic bool
}{
"Sets request ID under custom key": {
"x-custom-id",
func() *http.Request {
req, _ := http.NewRequest("GET", "/", nil)
return req
},
false,
},
"Custom key value matches GetReqID": {
"x-trace-id",
func() *http.Request {
req, _ := http.NewRequest("GET", "/", nil)
return req
},
false,
},
"Panics on empty key": {
"",
func() *http.Request {
req, _ := http.NewRequest("GET", "/", nil)
return req
},
true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if test.expectPanic {
defer func() {
if r := recover(); r == nil {
t.Fatalf("[%s] expected panic but did not get one", name)
}
}()
RequestIDWithCustomKey(test.customKey)
return
}
var gotDefault string
var gotCustom string
w := httptest.NewRecorder()
r := chi.NewRouter()
r.Use(RequestIDWithCustomKey(test.customKey))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
gotDefault = GetReqID(r.Context())
if v, ok := r.Context().Value(test.customKey).(string); ok {
gotCustom = v
}
w.Write([]byte(gotDefault))
})
r.ServeHTTP(w, test.request())
if gotDefault == "" {
t.Fatalf("[%s] expected default RequestIDKey to be set in context", name)
}
if gotCustom == "" {
t.Fatalf("[%s] expected custom key %q to be set in context", name, test.customKey)
}
if gotDefault != gotCustom {
t.Fatalf("[%s] default id %q and custom id %q should be equal", name, gotDefault, gotCustom)
}
})
}
}