-
Notifications
You must be signed in to change notification settings - Fork 208
/
html_data_test.go
83 lines (67 loc) · 1.6 KB
/
html_data_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
package authboss
import (
"context"
"net/http/httptest"
"testing"
)
func TestHTMLData(t *testing.T) {
t.Parallel()
data := NewHTMLData("a", "b").MergeKV("c", "d").Merge(NewHTMLData("e", "f"))
if data["a"].(string) != "b" {
t.Error("A was wrong:", data["a"])
}
if data["c"].(string) != "d" {
t.Error("C was wrong:", data["c"])
}
if data["e"].(string) != "f" {
t.Error("E was wrong:", data["e"])
}
}
func TestHTMLData_Panics(t *testing.T) {
t.Parallel()
nPanics := 0
panicCount := func() {
if r := recover(); r != nil {
nPanics++
}
}
func() {
defer panicCount()
NewHTMLData("hello")
}()
func() {
defer panicCount()
NewHTMLData().MergeKV("hello")
}()
func() {
defer panicCount()
NewHTMLData(5, 6)
}()
func() {
defer panicCount()
NewHTMLData().MergeKV(7, 8)
}()
if nPanics != 4 {
t.Error("They all should have paniced.")
}
}
func TestHTMLDataMergeDataInRequest(t *testing.T) {
t.Parallel()
r := httptest.NewRequest("GET", "/", nil)
MergeDataInRequest(&r, HTMLData{"hello": "world"})
val := r.Context().Value(CTXKeyData).(HTMLData)["hello"].(string)
if val != "world" {
t.Error("expected world, got:", val)
}
r = httptest.NewRequest("GET", "/", nil)
r = r.WithContext(context.WithValue(context.Background(), CTXKeyData, HTMLData{"first": "here"}))
MergeDataInRequest(&r, HTMLData{"hello": "world"})
val = r.Context().Value(CTXKeyData).(HTMLData)["hello"].(string)
if val != "world" {
t.Error("expected world, got:", val)
}
val = r.Context().Value(CTXKeyData).(HTMLData)["first"].(string)
if val != "here" {
t.Error("expected world, got:", val)
}
}