-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_headers_test.go
More file actions
104 lines (97 loc) · 2.72 KB
/
Copy pathrequest_headers_test.go
File metadata and controls
104 lines (97 loc) · 2.72 KB
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
package routeit
import "testing"
func TestHeadersFromRaw(t *testing.T) {
t.Run("valid inputs", func(t *testing.T) {
tests := []struct {
name string
raw [][]byte
want map[string][]string
wantIndex int
}{
{
name: "multi header",
raw: [][]byte{[]byte("Host: localhost"), []byte("Content-Type: application/json"), {}},
want: map[string][]string{
"Host": {"localhost"},
"Content-Type": {"application/json"},
},
wantIndex: 2,
},
{
name: "excessive leading and trailing whitespace",
raw: [][]byte{[]byte("X-My-Header: value "), {}},
want: map[string][]string{"X-My-Header": {"value"}},
wantIndex: 1,
},
{
name: "exits after empty lines",
raw: [][]byte{[]byte(""), []byte("Host: localhost")},
wantIndex: 0,
},
{
name: `allows multiple ":" characters`,
raw: [][]byte{[]byte("Host: localhost:433"), {}},
want: map[string][]string{"Host": {"localhost:433"}},
wantIndex: 1,
},
{
name: "stores multiple header entries with the same key",
raw: [][]byte{[]byte("Accept: application/json"), []byte("Accept: application/javascript"), []byte("Host: localhost"), []byte("Accept: text/html"), {}},
want: map[string][]string{
"Accept": {"application/json", "application/javascript", "text/html"},
"Host": {"localhost"},
},
wantIndex: 4,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
h, i, err := headersFromRaw(tc.raw)
if err != nil {
t.Errorf("expected error to be nil: %v", err)
}
if len(h.headers) != len(tc.want) {
t.Errorf(`headers from raw len(h) = %d, want %d`, len(h.headers), len(tc.want))
}
for k, vals := range tc.want {
verifyHeaderPresentAndMatches(t, h.headers, k, vals)
}
if i != tc.wantIndex {
t.Errorf(`last valid header index = %d, wanted %d`, i, tc.wantIndex)
}
})
}
})
t.Run("errors", func(t *testing.T) {
tests := []struct {
name string
raw [][]byte
}{
{
name: "does not allow leading whitespace in keys",
raw: [][]byte{[]byte(" My-Header: Value")},
},
{
name: "does not allow trailing whitespace in keys",
raw: [][]byte{[]byte("My-Header : Value")},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
h, i, err := headersFromRaw(tc.raw)
if err == nil {
t.Fatal("expected error but got nil")
}
if h != nil {
t.Fatal("expected headers to be nil")
}
if err.Error() != "400: Bad Request" {
t.Errorf(`error = %q, wanted "400: Bad Request"`, err.Error())
}
if i != -1 {
t.Errorf(`last valid header index = %d, wanted -1`, i)
}
})
}
})
}