This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrfc7230_test.go
210 lines (202 loc) · 4.84 KB
/
rfc7230_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package httpheader
import (
"fmt"
"net/http"
"os"
"testing"
)
func ExampleVia() {
header := http.Header{"Via": {
"1.1 proxy2.example.com:8080 (corporate)",
"2 edge3.example.net",
}}
fmt.Print(Via(header))
// Output: [{HTTP/1.1 proxy2.example.com:8080 corporate} {HTTP/2.0 edge3.example.net }]
}
func ExampleAddVia() {
header := http.Header{}
AddVia(header, ViaElem{ReceivedProto: "HTTP/1.1", ReceivedBy: "api-gw1"})
header.Write(os.Stdout)
// Output: Via: 1.1 api-gw1
}
func TestVia(t *testing.T) {
tests := []struct {
header http.Header
result []ViaElem
}{
// Valid headers.
{
http.Header{"Via": {"1.0 foo"}},
[]ViaElem{{"HTTP/1.0", "foo", ""}},
},
{
http.Header{"Via": {"1.0 \tfoo"}},
[]ViaElem{{"HTTP/1.0", "foo", ""}},
},
{
http.Header{"Via": {"1.0 foo "}},
[]ViaElem{{"HTTP/1.0", "foo", ""}},
},
{
http.Header{"Via": {"1.0 foo ,"}},
[]ViaElem{{"HTTP/1.0", "foo", ""}},
},
{
http.Header{"Via": {"1.0 foo\t (comment)"}},
[]ViaElem{{"HTTP/1.0", "foo", "comment"}},
},
{
http.Header{"Via": {
"1.0 foo,1.0 bar\t, \t 1.0 baz,,",
"1.1 qux",
}},
[]ViaElem{
{"HTTP/1.0", "foo", ""},
{"HTTP/1.0", "bar", ""},
{"HTTP/1.0", "baz", ""},
{"HTTP/1.1", "qux", ""},
},
},
{
http.Header{"Via": {"FSTR/3 bar (some new protocol)"}},
[]ViaElem{{"FSTR/3", "bar", "some new protocol"}},
},
{
http.Header{"Via": {"1.1 foo (comment (with) nesting)"}},
[]ViaElem{{"HTTP/1.1", "foo", "comment (with) nesting"}},
},
{
http.Header{"Via": {"1.1 foo (comment (with nesting))"}},
[]ViaElem{{"HTTP/1.1", "foo", "comment (with nesting)"}},
},
{
http.Header{"Via": {`1.1 foo (comment with \) quoting)`}},
[]ViaElem{{"HTTP/1.1", "foo", "comment with ) quoting"}},
},
{
http.Header{"Via": {
`1.1 foo (comment (with \) quoting) and nesting)`,
}},
[]ViaElem{
{"HTTP/1.1", "foo", "comment (with ) quoting) and nesting"},
},
},
{
http.Header{"Via": {`1.1 foo (\strange quoting)`}},
[]ViaElem{{"HTTP/1.1", "foo", "strange quoting"}},
},
{
// This is a valid received-by, per uri-host -> IPvFuture.
http.Header{"Via": {
`1.1 [v9.a51c00de,route=51]:8080 (IPv9 Powered), 1.1 example.net`,
}},
[]ViaElem{
{"HTTP/1.1", "[v9.a51c00de,route=51]:8080", "IPv9 Powered"},
{"HTTP/1.1", "example.net", ""},
},
},
{
// This is a valid received-by, per uri-host -> reg-name -> sub-delims,
// but we currently don't parse it. This is a documented bug.
http.Header{"Via": {
`1.1 funky,reg-name, 1.1 example.net`,
}},
[]ViaElem{
{"HTTP/1.1", "funky", ""},
{"HTTP/reg-name", "", ""},
{"HTTP/1.1", "example.net", ""},
},
},
{
http.Header{"Via": {"2 example.com, HTTP/2 example.net"}},
[]ViaElem{
{"HTTP/2.0", "example.com", ""},
{"HTTP/2.0", "example.net", ""},
},
},
// Invalid headers.
// Precise outputs on them are not a guaranteed part of the API.
// They may change as convenient for the parsing code.
{
http.Header{"Via": {"1.0"}},
[]ViaElem{{"HTTP/1.0", "", ""}},
},
{
http.Header{"Via": {"1.0, 1.1 foo, 1.2, 1.3 bar"}},
[]ViaElem{
{"HTTP/1.0", "", ""},
{"HTTP/1.1", "foo", ""},
{"HTTP/1.2", "", ""},
{"HTTP/1.3", "bar", ""},
},
},
{
http.Header{"Via": {
"1.1 foo (unterminated",
"1.1 bar",
}},
[]ViaElem{
{"HTTP/1.1", "foo", "unterminated"},
{"HTTP/1.1", "bar", ""},
},
},
{
http.Header{"Via": {"1.1 foo (unterminated (with nesting)"}},
[]ViaElem{
{"HTTP/1.1", "foo", "unterminated (with nesting)"},
},
},
{
http.Header{"Via": {
`1.1 foo (unterminated with \quoting (and nesting`,
"1.1 bar",
}},
[]ViaElem{
{"HTTP/1.1", "foo", "unterminated with quoting (and nesting"},
{"HTTP/1.1", "bar", ""},
},
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
checkParse(t, test.header, test.result, Via(test.header))
})
}
}
func TestSetVia(t *testing.T) {
tests := []struct {
input []ViaElem
result http.Header
}{
{
[]ViaElem{{"HTTP/1.1", "foo", ""}},
http.Header{"Via": {`1.1 foo`}},
},
{
[]ViaElem{{"FSTR/3.0", "bar.example.net:8080", "(baz)"}},
http.Header{"Via": {`FSTR/3.0 bar.example.net:8080 (\(baz\))`}},
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
header := http.Header{}
SetVia(header, test.input)
checkGenerate(t, test.input, test.result, header)
})
}
}
func TestViaRoundTrip(t *testing.T) {
checkRoundTrip(t, SetVia, Via,
[]ViaElem{{
ReceivedProto: "token/token",
ReceivedBy: "token",
Comment: "quotable | empty",
}},
)
}
func BenchmarkVia(b *testing.B) {
header := http.Header{"Via": {"1.1 proxy2.example.net (CWA (corporate Web accelerator))", "2 api-front.example.com:443 (trace: 97G9Hcio), 2 gw1-3.svc.example.com"}}
for i := 0; i < b.N; i++ {
Via(header)
}
}