~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/http/readrequest_test.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
)
14
14
 
15
15
type reqTest struct {
16
 
        Raw  string
17
 
        Req  Request
18
 
        Body string
 
16
        Raw   string
 
17
        Req   *Request
 
18
        Body  string
 
19
        Error string
19
20
}
20
21
 
 
22
var noError = ""
 
23
var noBody = ""
 
24
 
21
25
var reqTests = []reqTest{
22
26
        // Baseline test; All Request fields included for template use
23
27
        {
33
37
                        "Proxy-Connection: keep-alive\r\n\r\n" +
34
38
                        "abcdef\n???",
35
39
 
36
 
                Request{
 
40
                &Request{
37
41
                        Method: "GET",
38
42
                        RawURL: "http://www.techcrunch.com/",
39
43
                        URL: &URL{
58
62
                                "Keep-Alive":       {"300"},
59
63
                                "Proxy-Connection": {"keep-alive"},
60
64
                                "Content-Length":   {"7"},
 
65
                                "User-Agent":       {"Fake"},
61
66
                        },
62
67
                        Close:         false,
63
68
                        ContentLength: 7,
64
69
                        Host:          "www.techcrunch.com",
65
 
                        Referer:       "",
66
 
                        UserAgent:     "Fake",
67
70
                        Form:          Values{},
68
71
                },
69
72
 
70
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
                        RawURL: "/",
 
86
                        URL: &URL{
 
87
                                Raw:     "/",
 
88
                                Path:    "/",
 
89
                                RawPath: "/",
 
90
                        },
 
91
                        Proto:         "HTTP/1.1",
 
92
                        ProtoMajor:    1,
 
93
                        ProtoMinor:    1,
 
94
                        Close:         false,
 
95
                        ContentLength: 0,
 
96
                        Host:          "foo.com",
 
97
                        Form:          Values{},
 
98
                },
 
99
 
 
100
                noBody,
 
101
                noError,
71
102
        },
72
103
 
73
104
        // Tests that we don't parse a path that looks like a
76
107
                "GET //user@host/is/actually/a/path/ HTTP/1.1\r\n" +
77
108
                        "Host: test\r\n\r\n",
78
109
 
79
 
                Request{
 
110
                &Request{
80
111
                        Method: "GET",
81
112
                        RawURL: "//user@host/is/actually/a/path/",
82
113
                        URL: &URL{
95
126
                        ProtoMinor:    1,
96
127
                        Header:        Header{},
97
128
                        Close:         false,
98
 
                        ContentLength: -1,
 
129
                        ContentLength: 0,
99
130
                        Host:          "test",
100
 
                        Referer:       "",
101
 
                        UserAgent:     "",
102
131
                        Form:          Values{},
103
132
                },
104
133
 
105
 
                "",
 
134
                noBody,
 
135
                noError,
 
136
        },
 
137
 
 
138
        // Tests a bogus abs_path on the Request-Line (RFC 2616 section 5.1.2)
 
139
        {
 
140
                "GET ../../../../etc/passwd HTTP/1.1\r\n" +
 
141
                        "Host: test\r\n\r\n",
 
142
                nil,
 
143
                noBody,
 
144
                "parse ../../../../etc/passwd: invalid URI for request",
 
145
        },
 
146
 
 
147
        // Tests missing URL:
 
148
        {
 
149
                "GET  HTTP/1.1\r\n" +
 
150
                        "Host: test\r\n\r\n",
 
151
                nil,
 
152
                noBody,
 
153
                "parse : empty url",
106
154
        },
107
155
}
108
156
 
113
161
                braw.WriteString(tt.Raw)
114
162
                req, err := ReadRequest(bufio.NewReader(&braw))
115
163
                if err != nil {
116
 
                        t.Errorf("#%d: %s", i, err)
 
164
                        if err.String() != tt.Error {
 
165
                                t.Errorf("#%d: error %q, want error %q", i, err.String(), tt.Error)
 
166
                        }
117
167
                        continue
118
168
                }
119
169
                rbody := req.Body
120
170
                req.Body = nil
121
 
                diff(t, fmt.Sprintf("#%d Request", i), req, &tt.Req)
 
171
                diff(t, fmt.Sprintf("#%d Request", i), req, tt.Req)
122
172
                var bout bytes.Buffer
123
173
                if rbody != nil {
124
174
                        io.Copy(&bout, rbody)