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_",
252 func TestReadRequest(t *testing.T) {
253 for i := range reqTests {
255 var braw bytes.Buffer
256 braw.WriteString(tt.Raw)
257 req, err := ReadRequest(bufio.NewReader(&braw))
259 if err.Error() != tt.Error {
260 t.Errorf("#%d: error %q, want error %q", i, err.Error(), tt.Error)
266 diff(t, fmt.Sprintf("#%d Request", i), req, tt.Req)
267 var bout bytes.Buffer
269 _, err := io.Copy(&bout, rbody)
271 t.Fatalf("#%d. copying body: %v", i, err)
275 body := bout.String()
277 t.Errorf("#%d: Body = %q want %q", i, body, tt.Body)
279 if !reflect.DeepEqual(tt.Trailer, req.Trailer) {
280 t.Errorf("#%d. Trailers differ.\n got: %v\nwant: %v", i, req.Trailer, tt.Trailer)