forked from justinas/nosurf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
101 lines (75 loc) · 1.98 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
package nosurf
import (
"errors"
"testing"
)
func TestSetsReasonCorrectly(t *testing.T) {
req := dummyGet()
// set token first, as it's required for ctxSetReason
ctxSetToken(req, "abcdef")
err := errors.New("universe imploded")
ctxSetReason(req, err)
got := contextMap[req].reason
if got != err {
t.Errorf("Reason set incorrectly: expected %v, got %v", err, got)
}
}
func TestSettingReasonFailsWithoutContext(t *testing.T) {
req := dummyGet()
err := errors.New("universe imploded")
defer func() {
r := recover()
if r == nil {
t.Error("ctxSetReason() didn't panic on no context")
}
}()
ctxSetReason(req, err)
}
func TestSetsTokenCorrectly(t *testing.T) {
req := dummyGet()
token := "abcdef"
ctxSetToken(req, token)
got := contextMap[req].token
if got != token {
t.Errorf("Token set incorrectly: expected %v, got %v", token, got)
}
}
func TestGetsTokenCorrectly(t *testing.T) {
req := dummyGet()
token := Token(req)
if token != "" {
t.Errorf("Token hasn't been set yet, but it's not an empty string, it's %v", token)
}
intended := "abcdef"
ctxSetToken(req, intended)
token = Token(req)
if token != "abcdef" {
t.Errorf("Token has been set to %v, but it's %v", intended, token)
}
}
func TestGetsReasonCorrectly(t *testing.T) {
req := dummyGet()
reason := Reason(req)
if reason != nil {
t.Errorf("Reason hasn't been set yet, but it's not nil, it's %v", reason)
}
// again, needed for ctxSetReason() to work
ctxSetToken(req, "dummy")
intended := errors.New("universe imploded")
ctxSetReason(req, intended)
reason = Reason(req)
if reason != intended {
t.Errorf("Reason has been set to %v, but it's %v", intended, reason)
}
}
func TestClearsContextEntry(t *testing.T) {
req := dummyGet()
ctxSetToken(req, "dummy")
ctxSetReason(req, errors.New("some error"))
ctxClear(req)
entry, found := contextMap[req]
if found {
t.Errorf("Context entry %v found for the request %v, even though"+
" it should have been cleared.", entry, req)
}
}