-
Notifications
You must be signed in to change notification settings - Fork 0
/
header_test.go
184 lines (172 loc) · 4.98 KB
/
header_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package lowerkeys_test
import (
"net/http"
"reflect"
"testing"
"github.com/Zyl9393/lowerkeys"
)
func TestLowerKeysFrom(t *testing.T) {
tests := []struct {
header http.Header
expectedHeader lowerkeys.Header
}{
{nil, lowerkeys.Header{}},
{http.Header{}, lowerkeys.Header{}},
{http.Header{"content-type": {"foo"}}, lowerkeys.Header{"content-type": {"foo"}}},
{http.Header{"Content-Type": {"foo"}}, lowerkeys.Header{"content-type": {"foo"}}},
{http.Header{"Content-Type": {"foo"}, "Content-Length": {"3"}}, lowerkeys.Header{"content-type": {"foo"}, "content-length": {"3"}}},
}
for i, test := range tests {
header := lowerkeys.From(test.header)
if !reflect.DeepEqual(header, test.expectedHeader) {
t.Errorf("Test #%d: header = %v. Expected %v.", i+1, header, test.expectedHeader)
}
}
}
func TestLowerKeysFromIsCopy(t *testing.T) {
header := http.Header{
"Content-Type": {"foo"},
"Content-Length": {"3"},
}
lowerKeysHeader := lowerkeys.From(header)
header["Mark"] = []string{"Mutation"}
if _, ok := lowerKeysHeader["Mark"]; ok {
t.Fatalf("change in http.Header caused change in lowerkeys.Header created with lowerkeys.From()")
}
}
func TestLowerKeysUsingIsSameMap(t *testing.T) {
header := http.Header{
"Content-Type": {"foo"},
"Content-Length": {"3"},
}
lowerKeysHeader := lowerkeys.Using(header)
header["Mark"] = []string{"Mutation"}
if _, ok := lowerKeysHeader["Mark"]; !ok {
t.Fatalf("change in http.Header did not cause change in lowerkeys.Header created with lowerkeys.Using()")
}
}
func TestLowerKeysHeaderAdd(t *testing.T) {
header := lowerkeys.From(http.Header{
"Content-Type": {"foo"},
"Content-Length": {"3"},
})
header.Add("Stop-Writing-Like-This", "Please")
if _, ok := header["Stop-Writing-Like-This"]; ok {
t.Errorf("key was added in non-lower original case")
}
if _, ok := header["stop-writing-like-this"]; !ok {
t.Errorf("key was not added in lower-case")
}
if header.Get("sToP-wRiTiNg-LikE-tHiS") != "Please" {
t.Errorf("added key not found")
}
}
func TestLowerKeysHeaderDel(t *testing.T) {
header := lowerkeys.From(http.Header{
"Content-Type": {"foo"},
"Content-Length": {"3"},
})
header.Del("conTENT-TYpe")
if _, ok := header["Content-Type"]; ok {
t.Errorf("key was not deleted")
}
if header.Get("ConTent-tyPE") != "" {
t.Errorf("deleted key is still found")
}
}
func TestLowerKeysHeaderGet(t *testing.T) {
header := lowerkeys.From(http.Header{
"Content-Type": {"foo"},
"Content-Length": {"3"},
})
if header.Get("Content-Type") != "foo" {
t.Errorf(`header.Get("Content-Type") != "foo"`)
}
if header.Get("Content-Length") != "3" {
t.Errorf(`header.Get("Content-Length") != "3"`)
}
if header.Get("content-type") != "foo" {
t.Errorf(`header.Get("content-type") != "foo"`)
}
if header.Get("content-length") != "3" {
t.Errorf(`header.Get("content-length") != "3"`)
}
}
func TestLowerKeysHeaderSet(t *testing.T) {
header := lowerkeys.From(http.Header{
"Content-Type": {"foo"},
"Content-Length": {"3"},
})
header.Set("contenT-TYPE", "BAR")
header.Set("oTHerKEY", "value")
if _, ok := header["content-type"]; !ok {
t.Errorf("key replaced but not found")
}
val := header.Values("conTENT-type")
if len(val) != 1 {
t.Errorf("replaced one value but length was not 1")
}
if val[0] != "BAR" {
t.Errorf("key found but wrong value")
}
val = header.Values("othErkey")
if len(val) != 1 {
t.Errorf("set one value but length was not 1")
}
if val[0] != "value" {
t.Errorf("key found but wrong value")
}
if len(header) != 3 {
t.Errorf("unexpected entry count in map")
}
}
func TestLowerKeysHeaderValues(t *testing.T) {
header := lowerkeys.From(http.Header{
"Content-Type": {"foo", "bar"},
"Nonsense": {"3", "4"},
})
tests := []struct {
headerName string
expectedValues map[string]int
}{
{"cOnTeNt-TyPe", map[string]int{"foo": 1, "bar": 1}},
{"NonSenSe", map[string]int{"3": 1, "4": 1}},
}
for _, test := range tests {
for _, value := range header.Values(test.headerName) {
_, ok := test.expectedValues[value]
if !ok {
t.Errorf("unexpected or duplicate value '%s' for header name '%s'", value, test.headerName)
} else {
delete(test.expectedValues, value)
}
}
if len(test.expectedValues) > 0 {
t.Errorf("missing values for header name '%s': %v", test.headerName, test.expectedValues)
}
}
}
func TestLowerKeysFromPanic(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatalf("expected this test to panic, but it didn't.")
}
}()
lowerkeys.From(http.Header{
"Content-Type": {"foo"},
"cOnTenT-tYpE": {"foo"},
})
}
func TestLowerKeysUsingPanic(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatalf("expected this test to panic, but it didn't.")
}
}()
lowerkeys.Using(http.Header{
"Content-Type": {"foo"},
"cOnTenT-tYpE": {"foo"},
})
}