-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_id_middleware_test.go
More file actions
56 lines (51 loc) · 1.27 KB
/
Copy pathrequest_id_middleware_test.go
File metadata and controls
56 lines (51 loc) · 1.27 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
package routeit
import "testing"
func TestRequestIdMiddleware(t *testing.T) {
tests := []struct {
name string
header string
provider RequestIdProvider
wantId string
wantHeader string
}{
{
name: "no header provided, uses default",
provider: func(r *Request) string { return "id" },
wantId: "id",
wantHeader: "X-Request-Id",
},
{
name: "uses custom header",
header: "X-My-Request-Id",
provider: func(r *Request) string { return "id" },
wantId: "id",
wantHeader: "X-My-Request-Id",
},
{
name: "uses provider correctly",
header: "With-Provider",
provider: func(r *Request) string {
return r.Path()
},
wantId: "/foo",
wantHeader: "With-Provider",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mware := requestIdMiddleware(tc.provider, tc.header)
req := NewTestRequest(t, "/foo", GET, TestRequestOptions{})
res, proceeded, err := TestMiddleware(mware, req)
if err != nil {
t.Errorf(`err = %v, wanted nil`, err)
}
if !proceeded {
t.Error("did not proceed")
}
if req.req.id != tc.wantId {
t.Errorf(`id = %+q, wanted %+q`, req.req.id, tc.wantId)
}
res.AssertHeaderMatchesString(t, tc.wantHeader, tc.wantId)
})
}
}