-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathip_test.go
45 lines (41 loc) · 897 Bytes
/
ip_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 client
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/metadata"
)
func TestGetIPAddr(t *testing.T) {
tt := map[string]struct {
ctx context.Context
hasIP bool
expectedIP string
}{
"emptyContext": {
ctx: context.Background(),
hasIP: false,
},
"contextWithoutClientIP": {
ctx: metadata.NewIncomingContext(
context.Background(),
metadata.MD{"key": []string{"value"}},
),
hasIP: false,
},
"contextWithClientIP": {
ctx: metadata.NewIncomingContext(
context.Background(),
metadata.MD{clientIPHeader: []string{"127.0.0.1"}},
),
hasIP: true,
expectedIP: "127.0.0.1",
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
actual, err := GetIPAddr(tc.ctx)
assert.Equal(t, tc.hasIP, err == nil)
assert.Equal(t, tc.expectedIP, actual)
})
}
}