-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_test.go
45 lines (41 loc) · 916 Bytes
/
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
package clientip
import (
"context"
"net"
"reflect"
"testing"
)
func TestFromContext(t *testing.T) {
t.Parallel()
tests := []struct {
name string
ctx context.Context
expectedIP net.IP
}{
{
name: "returns the correct ip when set",
ctx: toContext(context.Background(), net.ParseIP("45.0.0.40")),
expectedIP: net.ParseIP("45.0.0.40"),
},
{
name: "returns nil ip when value is not net.IP",
ctx: context.WithValue(context.Background(), ipCtxKey{}, "45.0.0.40"),
expectedIP: nil,
},
{
name: "returns nil ip when not set",
ctx: context.Background(),
expectedIP: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ip := FromContext(tt.ctx)
if !reflect.DeepEqual(tt.expectedIP, ip) {
t.Errorf("expected %s to equal %s", tt.expectedIP, ip)
}
})
}
}