-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestid_test.go
76 lines (63 loc) · 1.44 KB
/
requestid_test.go
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
package requestid
import (
"testing"
"github.com/savsgio/atreugo/v11"
"github.com/valyala/fasthttp"
)
func Test_New(t *testing.T) { //nolint:funlen
type args struct {
predefinedRequestID []byte
}
type want struct {
newValue bool
err bool
}
tests := []struct {
name string
args args
want want
}{
{
name: "Create",
args: args{
predefinedRequestID: nil,
},
want: want{
newValue: true,
err: false,
},
},
{
name: "Predefined",
args: args{
predefinedRequestID: []byte("1342nwjdviwer3c2e32e"),
},
want: want{
newValue: false,
err: false,
},
},
}
for _, test := range tests {
tt := test
t.Run(tt.name, func(t *testing.T) {
t.Helper()
ctx := new(atreugo.RequestCtx)
ctx.RequestCtx = new(fasthttp.RequestCtx)
if tt.args.predefinedRequestID != nil {
ctx.Request.Header.SetBytesV(atreugo.XRequestIDHeader, tt.args.predefinedRequestID)
}
err := New(Config{})(ctx)
if (err != nil) != tt.want.err {
t.Errorf("RequestIDMiddleware() unexpected error: %v", err)
return
}
value := ctx.Request.Header.Peek(atreugo.XRequestIDHeader)
if tt.want.newValue && (string(tt.args.predefinedRequestID) == string(value)) {
t.Error("RequestIDMiddleware() not set a new value")
} else if !tt.want.newValue && (string(tt.args.predefinedRequestID) != string(value)) {
t.Error("RequestIDMiddleware() changed value unexpectly")
}
})
}
}