OSDN Git Service

Update Go library to last weekly.
[pf3gnuchains/gcc-fork.git] / libgo / go / http / readrequest_test.go
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.
4
5 package http
6
7 import (
8         "bufio"
9         "bytes"
10         "fmt"
11         "io"
12         "testing"
13         "url"
14 )
15
16 type reqTest struct {
17         Raw   string
18         Req   *Request
19         Body  string
20         Error string
21 }
22
23 var noError = ""
24 var noBody = ""
25
26 var reqTests = []reqTest{
27         // Baseline test; All Request fields included for template use
28         {
29                 "GET http://www.techcrunch.com/ HTTP/1.1\r\n" +
30                         "Host: www.techcrunch.com\r\n" +
31                         "User-Agent: Fake\r\n" +
32                         "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
33                         "Accept-Language: en-us,en;q=0.5\r\n" +
34                         "Accept-Encoding: gzip,deflate\r\n" +
35                         "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
36                         "Keep-Alive: 300\r\n" +
37                         "Content-Length: 7\r\n" +
38                         "Proxy-Connection: keep-alive\r\n\r\n" +
39                         "abcdef\n???",
40
41                 &Request{
42                         Method: "GET",
43                         URL: &url.URL{
44                                 Raw:          "http://www.techcrunch.com/",
45                                 Scheme:       "http",
46                                 RawPath:      "/",
47                                 RawAuthority: "www.techcrunch.com",
48                                 RawUserinfo:  "",
49                                 Host:         "www.techcrunch.com",
50                                 Path:         "/",
51                                 RawQuery:     "",
52                                 Fragment:     "",
53                         },
54                         Proto:      "HTTP/1.1",
55                         ProtoMajor: 1,
56                         ProtoMinor: 1,
57                         Header: Header{
58                                 "Accept":           {"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
59                                 "Accept-Language":  {"en-us,en;q=0.5"},
60                                 "Accept-Encoding":  {"gzip,deflate"},
61                                 "Accept-Charset":   {"ISO-8859-1,utf-8;q=0.7,*;q=0.7"},
62                                 "Keep-Alive":       {"300"},
63                                 "Proxy-Connection": {"keep-alive"},
64                                 "Content-Length":   {"7"},
65                                 "User-Agent":       {"Fake"},
66                         },
67                         Close:         false,
68                         ContentLength: 7,
69                         Host:          "www.techcrunch.com",
70                         Form:          url.Values{},
71                 },
72
73                 "abcdef\n",
74
75                 noError,
76         },
77
78         // GET request with no body (the normal case)
79         {
80                 "GET / HTTP/1.1\r\n" +
81                         "Host: foo.com\r\n\r\n",
82
83                 &Request{
84                         Method: "GET",
85                         URL: &url.URL{
86                                 Raw:     "/",
87                                 Path:    "/",
88                                 RawPath: "/",
89                         },
90                         Proto:         "HTTP/1.1",
91                         ProtoMajor:    1,
92                         ProtoMinor:    1,
93                         Close:         false,
94                         ContentLength: 0,
95                         Host:          "foo.com",
96                         Form:          url.Values{},
97                 },
98
99                 noBody,
100                 noError,
101         },
102
103         // Tests that we don't parse a path that looks like a
104         // scheme-relative URI as a scheme-relative URI.
105         {
106                 "GET //user@host/is/actually/a/path/ HTTP/1.1\r\n" +
107                         "Host: test\r\n\r\n",
108
109                 &Request{
110                         Method: "GET",
111                         URL: &url.URL{
112                                 Raw:          "//user@host/is/actually/a/path/",
113                                 Scheme:       "",
114                                 RawPath:      "//user@host/is/actually/a/path/",
115                                 RawAuthority: "",
116                                 RawUserinfo:  "",
117                                 Host:         "",
118                                 Path:         "//user@host/is/actually/a/path/",
119                                 RawQuery:     "",
120                                 Fragment:     "",
121                         },
122                         Proto:         "HTTP/1.1",
123                         ProtoMajor:    1,
124                         ProtoMinor:    1,
125                         Header:        Header{},
126                         Close:         false,
127                         ContentLength: 0,
128                         Host:          "test",
129                         Form:          url.Values{},
130                 },
131
132                 noBody,
133                 noError,
134         },
135
136         // Tests a bogus abs_path on the Request-Line (RFC 2616 section 5.1.2)
137         {
138                 "GET ../../../../etc/passwd HTTP/1.1\r\n" +
139                         "Host: test\r\n\r\n",
140                 nil,
141                 noBody,
142                 "parse ../../../../etc/passwd: invalid URI for request",
143         },
144
145         // Tests missing URL:
146         {
147                 "GET  HTTP/1.1\r\n" +
148                         "Host: test\r\n\r\n",
149                 nil,
150                 noBody,
151                 "parse : empty url",
152         },
153 }
154
155 func TestReadRequest(t *testing.T) {
156         for i := range reqTests {
157                 tt := &reqTests[i]
158                 var braw bytes.Buffer
159                 braw.WriteString(tt.Raw)
160                 req, err := ReadRequest(bufio.NewReader(&braw))
161                 if err != nil {
162                         if err.String() != tt.Error {
163                                 t.Errorf("#%d: error %q, want error %q", i, err.String(), tt.Error)
164                         }
165                         continue
166                 }
167                 rbody := req.Body
168                 req.Body = nil
169                 diff(t, fmt.Sprintf("#%d Request", i), req, tt.Req)
170                 var bout bytes.Buffer
171                 if rbody != nil {
172                         io.Copy(&bout, rbody)
173                         rbody.Close()
174                 }
175                 body := bout.String()
176                 if body != tt.Body {
177                         t.Errorf("#%d: Body = %q want %q", i, body, tt.Body)
178                 }
179         }
180 }