forked from castle/castle-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
69 lines (62 loc) · 1.54 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
package castle_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/utilitywarehouse/castle-go"
)
func TestToCtxFromRequest(t *testing.T) {
tests := map[string]struct {
input http.Request
expected castle.Context
}{
"castle token on header": {
input: func() http.Request {
req := httptest.NewRequest(http.MethodPost, "http://example.com", nil)
req.Header.Set("X-Castle-Request-Token", "foo")
req.RemoteAddr = "2.2.2.2"
return *req
}(),
expected: castle.Context{
IP: "2.2.2.2",
Headers: map[string]string{"X-Castle-Request-Token": "foo"},
RequestToken: "foo",
},
},
"castle token in form": {
input: func() http.Request {
req := httptest.NewRequest(http.MethodPost, "http://example.com/bar?castle_request_token=bar", nil)
req.RemoteAddr = "2.2.2.2"
return *req
}(),
expected: castle.Context{
IP: "2.2.2.2",
Headers: map[string]string{},
RequestToken: "bar",
},
},
"no castle token": {
input: func() http.Request {
req := http.Request{}
req.RemoteAddr = "2.2.2.2"
return req
}(),
expected: castle.Context{
IP: "2.2.2.2",
Headers: map[string]string{},
RequestToken: "",
},
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
ctx := context.Background()
gotCtx := castle.ToCtxFromHTTPRequest(ctx, &test.input)
got := castle.FromCtx(gotCtx)
assert.Equal(t, test.expected, *got)
})
}
}