1
// Copyright 2010 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
27
var noTrailer Header = nil
29
var reqTests = []reqTest{
30
// Baseline test; All Request fields included for template use
32
"GET http://www.techcrunch.com/ HTTP/1.1\r\n" +
33
"Host: www.techcrunch.com\r\n" +
34
"User-Agent: Fake\r\n" +
35
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
36
"Accept-Language: en-us,en;q=0.5\r\n" +
37
"Accept-Encoding: gzip,deflate\r\n" +
38
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
39
"Keep-Alive: 300\r\n" +
40
"Content-Length: 7\r\n" +
41
"Proxy-Connection: keep-alive\r\n\r\n" +
48
Host: "www.techcrunch.com",
55
"Accept": {"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
56
"Accept-Language": {"en-us,en;q=0.5"},
57
"Accept-Encoding": {"gzip,deflate"},
58
"Accept-Charset": {"ISO-8859-1,utf-8;q=0.7,*;q=0.7"},
59
"Keep-Alive": {"300"},
60
"Proxy-Connection": {"keep-alive"},
61
"Content-Length": {"7"},
62
"User-Agent": {"Fake"},
66
Host: "www.techcrunch.com",
67
RequestURI: "http://www.techcrunch.com/",
76
// GET request with no body (the normal case)
78
"GET / HTTP/1.1\r\n" +
79
"Host: foo.com\r\n\r\n",
101
// Tests that we don't parse a path that looks like a
102
// scheme-relative URI as a scheme-relative URI.
104
"GET //user@host/is/actually/a/path/ HTTP/1.1\r\n" +
105
"Host: test\r\n\r\n",
110
Path: "//user@host/is/actually/a/path/",
119
RequestURI: "//user@host/is/actually/a/path/",
127
// Tests a bogus abs_path on the Request-Line (RFC 2616 section 5.1.2)
129
"GET ../../../../etc/passwd HTTP/1.1\r\n" +
130
"Host: test\r\n\r\n",
134
"parse ../../../../etc/passwd: invalid URI for request",
137
// Tests missing URL:
140
"Host: test\r\n\r\n",
147
// Tests chunked body with trailer:
149
"POST / HTTP/1.1\r\n" +
150
"Host: foo.com\r\n" +
151
"Transfer-Encoding: chunked\r\n\r\n" +
155
"Trailer-Key: Trailer-Value\r\n" +
162
TransferEncoding: []string{"chunked"},
174
"Trailer-Key": {"Trailer-Value"},
179
// CONNECT request with domain name:
181
"CONNECT www.google.com:443 HTTP/1.1\r\n\r\n",
186
Host: "www.google.com:443",
194
Host: "www.google.com:443",
195
RequestURI: "www.google.com:443",
203
// CONNECT request with IP address:
205
"CONNECT 127.0.0.1:6060 HTTP/1.1\r\n\r\n",
210
Host: "127.0.0.1:6060",
218
Host: "127.0.0.1:6060",
219
RequestURI: "127.0.0.1:6060",
227
// CONNECT request for RPC:
229
"CONNECT /_goRPC_ HTTP/1.1\r\n\r\n",
243
RequestURI: "/_goRPC_",
251
// SSDP Notify request. golang.org/issue/3692
253
"NOTIFY * HTTP/1.1\r\nServer: foo\r\n\r\n",
263
"Server": []string{"foo"},
275
// OPTIONS request. Similar to golang.org/issue/3692
277
"OPTIONS * HTTP/1.1\r\nServer: foo\r\n\r\n",
287
"Server": []string{"foo"},
300
func TestReadRequest(t *testing.T) {
301
for i := range reqTests {
303
var braw bytes.Buffer
304
braw.WriteString(tt.Raw)
305
req, err := ReadRequest(bufio.NewReader(&braw))
307
if err.Error() != tt.Error {
308
t.Errorf("#%d: error %q, want error %q", i, err.Error(), tt.Error)
314
diff(t, fmt.Sprintf("#%d Request", i), req, tt.Req)
315
var bout bytes.Buffer
317
_, err := io.Copy(&bout, rbody)
319
t.Fatalf("#%d. copying body: %v", i, err)
323
body := bout.String()
325
t.Errorf("#%d: Body = %q want %q", i, body, tt.Body)
327
if !reflect.DeepEqual(tt.Trailer, req.Trailer) {
328
t.Errorf("#%d. Trailers differ.\n got: %v\nwant: %v", i, req.Trailer, tt.Trailer)