-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
context_test.go
175 lines (129 loc) · 3.87 KB
/
context_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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package atreugo
import (
"context"
"reflect"
"testing"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
func Test_AcquireRequestCtx(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := AcquireRequestCtx(ctx) // nolint:ifshort
if !isEqual(actx.jsonMarshalFunc, defaultJSONMarshalFunc) {
t.Errorf("jsonMarshalFunc = %p, want %p", actx.jsonMarshalFunc, defaultJSONMarshalFunc)
}
if actx.RequestCtx != ctx {
t.Errorf("RequestCtx = %p, want %p", actx.RequestCtx, ctx)
}
}
func Test_ReleaseRequestCtx(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := AcquireRequestCtx(ctx)
if err := actx.Next(); err != nil {
t.Fatalf("Error calling next. %+v", err)
}
actx.SkipView()
ReleaseRequestCtx(actx)
if actx.next {
t.Errorf("next is not 'false'")
}
if actx.skipView {
t.Errorf("skipView is not 'false'")
}
if actx.jsonMarshalFunc == nil {
t.Errorf("jsonMarshalFunc is nil")
}
if actx.RequestCtx != nil {
t.Errorf("*fasthttp.RequestCtx = %p, want %v", actx.RequestCtx, nil)
}
}
func TestRequestCtx_RequestID(t *testing.T) {
value := "123bnj3r2j3rj23"
ctx := new(RequestCtx)
ctx.RequestCtx = new(fasthttp.RequestCtx)
ctx.Request.Header.Set(XRequestIDHeader, value)
currentValue := string(ctx.RequestID())
if currentValue != value {
t.Errorf("ctx.RequestID() = '%s', want '%s'", currentValue, value)
}
}
func Test_Next(t *testing.T) {
ctx := AcquireRequestCtx(new(fasthttp.RequestCtx))
if err := ctx.Next(); err != nil {
t.Errorf("ctx.Next() unexpected error: %v", err)
}
}
func Test_SkipView(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := AcquireRequestCtx(ctx)
actx.SkipView()
if !actx.skipView {
t.Error("ctx.SkipView() is not true")
}
}
func Test_AttachContext(t *testing.T) {
type key struct{}
ctx := new(fasthttp.RequestCtx)
actx := AcquireRequestCtx(ctx)
actx.AttachContext(context.WithValue(ctx, key{}, "value"))
if actx.UserValue(attachedCtxKey) == nil {
t.Error("ctx.AttachContext() the context is not attached")
}
err := catchPanic(func() {
actx.AttachContext(actx)
})
if err == nil {
t.Error("Panic expected when attachs to itself")
}
}
func Test_AttachedContext(t *testing.T) {
type key struct{}
ctx := new(fasthttp.RequestCtx)
actx := AcquireRequestCtx(ctx)
otherCtx := context.WithValue(ctx, key{}, "value")
attachedCtx := actx.AttachedContext()
if attachedCtx != nil {
t.Errorf("ctx.AttachedContext() == %p, want %v", attachedCtx, nil)
}
actx.AttachContext(otherCtx)
attachedCtx = actx.AttachedContext()
if reflect.ValueOf(attachedCtx).Pointer() != reflect.ValueOf(otherCtx).Pointer() {
t.Errorf("ctx.AttachedContext() == %p, want %p", attachedCtx, otherCtx)
}
}
func Test_MatchedRoutePath(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := AcquireRequestCtx(ctx)
result := actx.MatchedRoutePath()
if result != nil {
t.Errorf("ctx.MatchedRoutePathParam() == %s, want %v", result, nil)
}
matchedRoutePath := "/user/{id}"
actx.SetUserValue(router.MatchedRoutePathParam, matchedRoutePath)
result = actx.MatchedRoutePath()
if string(result) != matchedRoutePath {
t.Errorf("ctx.MatchedRoutePathParam() == %s, want %s", result, matchedRoutePath)
}
}
func Test_Value(t *testing.T) {
type key struct{}
structKey := key{}
stringKey := "stringKey"
value := "value"
ctx := new(fasthttp.RequestCtx)
actx := AcquireRequestCtx(ctx)
if v := actx.Value("fake"); v != nil {
t.Errorf("Value() of key '%v' == %s, want %v", "fake", v, nil)
}
actx.AttachContext(context.WithValue(actx, structKey, value))
actx.SetUserValue(stringKey, value)
if v := actx.Value(structKey); v != value {
t.Errorf("Value() of key '%v' == %s, want %s", structKey, v, value)
}
if v := actx.Value(stringKey); v != value {
t.Errorf("Value() of key '%s' == %s, want %s", stringKey, v, value)
}
if v := actx.Value("fake"); v != nil {
t.Errorf("Value() key '%s' == %v, want %v", "fake", v, nil)
}
}