forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_test.go
197 lines (178 loc) · 6.17 KB
/
request_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
package http3
import (
"net/http"
"net/url"
"github.com/marten-seemann/qpack"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Request", func() {
It("populates request", func() {
headers := []qpack.HeaderField{
{Name: ":path", Value: "/foo"},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: "GET"},
{Name: "content-length", Value: "42"},
}
req, err := requestFromHeaders(headers)
Expect(err).NotTo(HaveOccurred())
Expect(req.Method).To(Equal("GET"))
Expect(req.URL.Path).To(Equal("/foo"))
Expect(req.URL.Host).To(BeEmpty())
Expect(req.Proto).To(Equal("HTTP/3.0"))
Expect(req.ProtoMajor).To(Equal(3))
Expect(req.ProtoMinor).To(BeZero())
Expect(req.ContentLength).To(Equal(int64(42)))
Expect(req.Header).To(BeEmpty())
Expect(req.Body).To(BeNil())
Expect(req.Host).To(Equal("quic.clemente.io"))
Expect(req.RequestURI).To(Equal("/foo"))
Expect(req.TLS).ToNot(BeNil())
})
It("parses path with leading double slashes", func() {
headers := []qpack.HeaderField{
{Name: ":path", Value: "//foo"},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: "GET"},
}
req, err := requestFromHeaders(headers)
Expect(err).NotTo(HaveOccurred())
Expect(req.Header).To(BeEmpty())
Expect(req.Body).To(BeNil())
Expect(req.URL.Path).To(Equal("//foo"))
Expect(req.URL.Host).To(BeEmpty())
Expect(req.Host).To(Equal("quic.clemente.io"))
Expect(req.RequestURI).To(Equal("//foo"))
})
It("concatenates the cookie headers", func() {
headers := []qpack.HeaderField{
{Name: ":path", Value: "/foo"},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: "GET"},
{Name: "cookie", Value: "cookie1=foobar1"},
{Name: "cookie", Value: "cookie2=foobar2"},
}
req, err := requestFromHeaders(headers)
Expect(err).NotTo(HaveOccurred())
Expect(req.Header).To(Equal(http.Header{
"Cookie": []string{"cookie1=foobar1; cookie2=foobar2"},
}))
})
It("handles Other headers", func() {
headers := []qpack.HeaderField{
{Name: ":path", Value: "/foo"},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: "GET"},
{Name: "cache-control", Value: "max-age=0"},
{Name: "duplicate-header", Value: "1"},
{Name: "duplicate-header", Value: "2"},
}
req, err := requestFromHeaders(headers)
Expect(err).NotTo(HaveOccurred())
Expect(req.Header).To(Equal(http.Header{
"Cache-Control": []string{"max-age=0"},
"Duplicate-Header": []string{"1", "2"},
}))
})
It("errors with missing path", func() {
headers := []qpack.HeaderField{
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: "GET"},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError(":path, :authority and :method must not be empty"))
})
It("errors with missing method", func() {
headers := []qpack.HeaderField{
{Name: ":path", Value: "/foo"},
{Name: ":authority", Value: "quic.clemente.io"},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError(":path, :authority and :method must not be empty"))
})
It("errors with missing authority", func() {
headers := []qpack.HeaderField{
{Name: ":path", Value: "/foo"},
{Name: ":method", Value: "GET"},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError(":path, :authority and :method must not be empty"))
})
Context("regular HTTP CONNECT", func() {
It("handles CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: http.MethodConnect},
}
req, err := requestFromHeaders(headers)
Expect(err).NotTo(HaveOccurred())
Expect(req.Method).To(Equal(http.MethodConnect))
Expect(req.RequestURI).To(Equal("quic.clemente.io"))
})
It("errors with missing authority in CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":method", Value: http.MethodConnect},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError(":path must be empty and :authority must not be empty"))
})
It("errors with extra path in CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":path", Value: "/foo"},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: http.MethodConnect},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError(":path must be empty and :authority must not be empty"))
})
})
Context("Extended CONNECT", func() {
It("handles Extended CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":protocol", Value: "webtransport"},
{Name: ":scheme", Value: "ftp"},
{Name: ":method", Value: http.MethodConnect},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":path", Value: "/foo?val=1337"},
}
req, err := requestFromHeaders(headers)
Expect(err).NotTo(HaveOccurred())
Expect(req.Method).To(Equal(http.MethodConnect))
Expect(req.Proto).To(Equal("webtransport"))
Expect(req.URL.String()).To(Equal("ftp://quic.clemente.io/foo?val=1337"))
Expect(req.URL.Query().Get("val")).To(Equal("1337"))
})
It("errors with missing scheme", func() {
headers := []qpack.HeaderField{
{Name: ":protocol", Value: "webtransport"},
{Name: ":method", Value: http.MethodConnect},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":path", Value: "/foo"},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError("extended CONNECT: :scheme, :path and :authority must not be empty"))
})
})
Context("extracting the hostname from a request", func() {
var url *url.URL
BeforeEach(func() {
var err error
url, err = url.Parse("https://quic.clemente.io:1337")
Expect(err).ToNot(HaveOccurred())
})
It("uses req.URL.Host", func() {
req := &http.Request{URL: url}
Expect(hostnameFromRequest(req)).To(Equal("quic.clemente.io:1337"))
})
It("uses req.URL.Host even if req.Host is available", func() {
req := &http.Request{
Host: "www.example.org",
URL: url,
}
Expect(hostnameFromRequest(req)).To(Equal("quic.clemente.io:1337"))
})
It("returns an empty hostname if nothing is set", func() {
Expect(hostnameFromRequest(&http.Request{})).To(BeEmpty())
})
})
})