-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
321 lines (293 loc) · 7.28 KB
/
context_test.go
File metadata and controls
321 lines (293 loc) · 7.28 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package keratin
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestKContext_RealIP(t *testing.T) {
tests := []struct {
name string
ip string
}{
{"IPv4 address", "192.168.1.1"},
{"IPv6 address", "2001:0db8:0000:0000:0000:0000:0000:0001"},
{"localhost", "127.0.0.1"},
{"empty IP", ""},
{"private IP", "10.0.0.1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := &kContext{realIP: tt.ip}
got := ctx.RealIP()
require.Equal(t, tt.ip, got)
})
}
}
func TestKContext_Pattern(t *testing.T) {
tests := []struct {
name string
pattern string
}{
{"simple path", "/users"},
{"path with parameter", "/users/:id"},
{"root path", "/"},
{"wildcard path", "/*"},
{"nested path", "/api/v1/users"},
{"complex pattern", "/api/:version/:resource/:id"},
{"empty pattern", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := &kContext{pattern: tt.pattern}
got := ctx.Pattern()
require.Equal(t, tt.pattern, got)
})
}
}
func TestKContext_Methods(t *testing.T) {
tests := []struct {
name string
methods string
}{
{"single method", "GET"},
{"multiple methods", "GET,POST,PUT,DELETE"},
{"empty methods", ""},
{"all methods", "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"},
{"two methods", "GET,POST"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := &kContext{methods: tt.methods}
got := ctx.Methods()
require.Equal(t, tt.methods, got)
})
}
}
func TestKContext_AnyMethods(t *testing.T) {
tests := []struct {
name string
anyMethods bool
}{
{"any methods true", true},
{"any methods false", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := &kContext{anyMethods: tt.anyMethods}
got := ctx.AnyMethods()
require.Equal(t, tt.anyMethods, got)
})
}
}
func TestKContext_reset(t *testing.T) {
tests := []struct {
name string
input *kContext
want *kContext
}{
{
name: "reset populated context",
input: &kContext{
scheme: "https",
realIP: "192.168.1.1",
pattern: "/users/:id",
methods: "GET,POST",
anyMethods: true,
err: nil,
},
want: &kContext{
scheme: "",
realIP: "",
pattern: "",
methods: "",
anyMethods: false,
err: nil,
},
},
{
name: "reset empty context",
input: &kContext{},
want: &kContext{
scheme: "",
realIP: "",
pattern: "",
methods: "",
anyMethods: false,
err: nil,
},
},
{
name: "reset with error",
input: &kContext{
scheme: "https",
realIP: "10.0.0.1",
pattern: "/",
methods: "GET",
err: context.Canceled,
},
want: &kContext{
scheme: "",
realIP: "",
pattern: "",
methods: "",
anyMethods: false,
err: nil,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.input.reset()
require.Equal(t, tt.want.realIP, tt.input.realIP)
require.Equal(t, tt.want.pattern, tt.input.pattern)
require.Equal(t, tt.want.methods, tt.input.methods)
require.Equal(t, tt.want.anyMethods, tt.input.anyMethods)
require.Equal(t, tt.want.err, tt.input.err)
})
}
}
func TestFromContext(t *testing.T) {
type difKey struct{}
tests := []struct {
name string
ctx context.Context
wantReal string
wantNil bool
}{
{
name: "context with kContext returns it",
ctx: context.WithValue(context.Background(), ctxKey{}, &kContext{realIP: "192.168.1.1"}),
wantReal: "192.168.1.1",
wantNil: false,
},
{
name: "context without kContext returns nilKCtx",
ctx: context.Background(),
wantReal: "",
wantNil: false,
},
{
name: "context with different key type returns nilKCtx",
ctx: context.WithValue(context.Background(), difKey{}, &kContext{realIP: "10.0.0.1"}),
wantReal: "",
wantNil: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FromContext(tt.ctx)
require.NotNil(t, got)
require.Equal(t, tt.wantReal, got.RealIP())
})
}
}
func TestKContext_ImplementsContext(t *testing.T) {
ctx := &kContext{
realIP: "192.168.1.1",
pattern: "/users/:id",
methods: "GET,POST",
anyMethods: false,
}
var _ Context = ctx
require.Equal(t, "192.168.1.1", ctx.RealIP())
require.Equal(t, "/users/:id", ctx.Pattern())
require.Equal(t, "GET,POST", ctx.Methods())
require.Equal(t, false, ctx.AnyMethods())
}
func TestKContext_FullCycle(t *testing.T) {
ctx := &kContext{
scheme: "https",
realIP: "2001:0db8:0000:0000:0000:0000:0000:0001",
pattern: "/api/v1/resources/:id",
methods: "GET,POST,PUT,PATCH,DELETE",
anyMethods: false,
}
require.Equal(t, "https", ctx.Scheme())
require.Equal(t, "2001:0db8:0000:0000:0000:0000:0000:0001", ctx.RealIP())
require.Equal(t, "/api/v1/resources/:id", ctx.Pattern())
require.Equal(t, "GET,POST,PUT,PATCH,DELETE", ctx.Methods())
require.Equal(t, false, ctx.AnyMethods())
ctx.reset()
require.Equal(t, "", ctx.Scheme())
require.Equal(t, "", ctx.RealIP())
require.Equal(t, "", ctx.Pattern())
require.Equal(t, "", ctx.Methods())
require.Equal(t, false, ctx.AnyMethods())
}
func TestNilKCtx(t *testing.T) {
tests := []struct {
name string
method func() any
wantNil bool
wantString string
}{
{
name: "RealIP returns empty string",
method: func() any { return nilKCtx.RealIP() },
wantNil: false,
wantString: "",
},
{
name: "Pattern returns empty string",
method: func() any { return nilKCtx.Pattern() },
wantNil: false,
wantString: "",
},
{
name: "Methods returns empty string",
method: func() any { return nilKCtx.Methods() },
wantNil: false,
wantString: "",
},
{
name: "AnyMethods returns false",
method: func() any { return nilKCtx.AnyMethods() },
wantNil: false,
wantString: "",
},
{
name: "Scheme returns empty string",
method: func() any { return nilKCtx.Scheme() },
wantNil: false,
wantString: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.method()
if tt.wantString != "" {
require.Equal(t, tt.wantString, got)
}
if tt.wantNil {
require.Nil(t, got)
} else {
require.NotNil(t, got)
}
})
}
}
func TestKContext_ContextKeyUniqueness(t *testing.T) {
ctx := context.Background()
ctx1 := context.WithValue(ctx, ctxKey{}, &kContext{realIP: "192.168.1.1"})
ctx2 := context.WithValue(ctx, ctxKey{}, &kContext{realIP: "10.0.0.1"})
got1 := FromContext(ctx1)
got2 := FromContext(ctx2)
require.NotEqual(t, got1.RealIP(), got2.RealIP())
require.Equal(t, "192.168.1.1", got1.RealIP())
require.Equal(t, "10.0.0.1", got2.RealIP())
}
func TestKContext_WithAllFields(t *testing.T) {
ctx := &kContext{
scheme: "https",
realIP: "203.0.113.1",
pattern: "/api/v2/organizations/:org/users/:user",
methods: "GET,HEAD,OPTIONS",
anyMethods: true,
err: context.DeadlineExceeded,
}
require.Equal(t, "https", ctx.Scheme())
require.Equal(t, "203.0.113.1", ctx.RealIP())
require.Equal(t, "/api/v2/organizations/:org/users/:user", ctx.Pattern())
require.Equal(t, "GET,HEAD,OPTIONS", ctx.Methods())
require.Equal(t, true, ctx.AnyMethods())
}