-
Notifications
You must be signed in to change notification settings - Fork 126
/
context_legacy_test.go
115 lines (87 loc) · 2.38 KB
/
context_legacy_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// +build !go1.7
package nosurf
import (
"bytes"
"errors"
"testing"
)
func TestSetsReasonCorrectly(t *testing.T) {
req := dummyGet()
// set token first, as it's required for ctxSetReason
ctxSetToken(req, []byte("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 := []byte("12345678901234567890123456789012")
ctxSetToken(req, token)
got := contextMap[req].token
if !bytes.Equal(token, unmaskToken(b64decode(got))) {
t.Errorf("Token set incorrectly: expected %v, got %v", token, got)
}
}
func TestGetsTokenCorrectly(t *testing.T) {
req := dummyGet()
token := Token(req)
if len(token) != 0 {
t.Errorf("Token hasn't been set yet, but it's not an empty slice, it's %v", token)
}
intended := []byte("12345678901234567890123456789012")
ctxSetToken(req, intended)
token = Token(req)
decToken := unmaskToken(b64decode(token))
if !bytes.Equal(intended, decToken) {
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, []byte("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, []byte("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)
}
}
func TestClearsContextEntryEvenIfNotSet(t *testing.T) {
r := dummyGet()
defer func() {
if r := recover(); r != nil {
t.Errorf("ctxClear(r) panicked with %v", r)
}
}()
ctxClear(r)
}