OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [http/] [readrequest_test.go] - Blame information for rev 854

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "net/url"
13
        "reflect"
14
        "testing"
15
)
16
 
17
type reqTest struct {
18
        Raw     string
19
        Req     *Request
20
        Body    string
21
        Trailer Header
22
        Error   string
23
}
24
 
25
var noError = ""
26
var noBody = ""
27
var noTrailer Header = nil
28
 
29
var reqTests = []reqTest{
30
        // Baseline test; All Request fields included for template use
31
        {
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" +
42
                        "abcdef\n???",
43
 
44
                &Request{
45
                        Method: "GET",
46
                        URL: &url.URL{
47
                                Scheme: "http",
48
                                Host:   "www.techcrunch.com",
49
                                Path:   "/",
50
                        },
51
                        Proto:      "HTTP/1.1",
52
                        ProtoMajor: 1,
53
                        ProtoMinor: 1,
54
                        Header: Header{
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"},
63
                        },
64
                        Close:         false,
65
                        ContentLength: 7,
66
                        Host:          "www.techcrunch.com",
67
                        RequestURI:    "http://www.techcrunch.com/",
68
                },
69
 
70
                "abcdef\n",
71
 
72
                noTrailer,
73
                noError,
74
        },
75
 
76
        // GET request with no body (the normal case)
77
        {
78
                "GET / HTTP/1.1\r\n" +
79
                        "Host: foo.com\r\n\r\n",
80
 
81
                &Request{
82
                        Method: "GET",
83
                        URL: &url.URL{
84
                                Path: "/",
85
                        },
86
                        Proto:         "HTTP/1.1",
87
                        ProtoMajor:    1,
88
                        ProtoMinor:    1,
89
                        Header:        Header{},
90
                        Close:         false,
91
                        ContentLength: 0,
92
                        Host:          "foo.com",
93
                        RequestURI:    "/",
94
                },
95
 
96
                noBody,
97
                noTrailer,
98
                noError,
99
        },
100
 
101
        // Tests that we don't parse a path that looks like a
102
        // scheme-relative URI as a scheme-relative URI.
103
        {
104
                "GET //user@host/is/actually/a/path/ HTTP/1.1\r\n" +
105
                        "Host: test\r\n\r\n",
106
 
107
                &Request{
108
                        Method: "GET",
109
                        URL: &url.URL{
110
                                Path: "//user@host/is/actually/a/path/",
111
                        },
112
                        Proto:         "HTTP/1.1",
113
                        ProtoMajor:    1,
114
                        ProtoMinor:    1,
115
                        Header:        Header{},
116
                        Close:         false,
117
                        ContentLength: 0,
118
                        Host:          "test",
119
                        RequestURI:    "//user@host/is/actually/a/path/",
120
                },
121
 
122
                noBody,
123
                noTrailer,
124
                noError,
125
        },
126
 
127
        // Tests a bogus abs_path on the Request-Line (RFC 2616 section 5.1.2)
128
        {
129
                "GET ../../../../etc/passwd HTTP/1.1\r\n" +
130
                        "Host: test\r\n\r\n",
131
                nil,
132
                noBody,
133
                noTrailer,
134
                "parse ../../../../etc/passwd: invalid URI for request",
135
        },
136
 
137
        // Tests missing URL:
138
        {
139
                "GET  HTTP/1.1\r\n" +
140
                        "Host: test\r\n\r\n",
141
                nil,
142
                noBody,
143
                noTrailer,
144
                "parse : empty url",
145
        },
146
 
147
        // Tests chunked body with trailer:
148
        {
149
                "POST / HTTP/1.1\r\n" +
150
                        "Host: foo.com\r\n" +
151
                        "Transfer-Encoding: chunked\r\n\r\n" +
152
                        "3\r\nfoo\r\n" +
153
                        "3\r\nbar\r\n" +
154
                        "0\r\n" +
155
                        "Trailer-Key: Trailer-Value\r\n" +
156
                        "\r\n",
157
                &Request{
158
                        Method: "POST",
159
                        URL: &url.URL{
160
                                Path: "/",
161
                        },
162
                        TransferEncoding: []string{"chunked"},
163
                        Proto:            "HTTP/1.1",
164
                        ProtoMajor:       1,
165
                        ProtoMinor:       1,
166
                        Header:           Header{},
167
                        ContentLength:    -1,
168
                        Host:             "foo.com",
169
                        RequestURI:       "/",
170
                },
171
 
172
                "foobar",
173
                Header{
174
                        "Trailer-Key": {"Trailer-Value"},
175
                },
176
                noError,
177
        },
178
 
179
        // CONNECT request with domain name:
180
        {
181
                "CONNECT www.google.com:443 HTTP/1.1\r\n\r\n",
182
 
183
                &Request{
184
                        Method: "CONNECT",
185
                        URL: &url.URL{
186
                                Host: "www.google.com:443",
187
                        },
188
                        Proto:         "HTTP/1.1",
189
                        ProtoMajor:    1,
190
                        ProtoMinor:    1,
191
                        Header:        Header{},
192
                        Close:         false,
193
                        ContentLength: 0,
194
                        Host:          "www.google.com:443",
195
                        RequestURI:    "www.google.com:443",
196
                },
197
 
198
                noBody,
199
                noTrailer,
200
                noError,
201
        },
202
 
203
        // CONNECT request with IP address:
204
        {
205
                "CONNECT 127.0.0.1:6060 HTTP/1.1\r\n\r\n",
206
 
207
                &Request{
208
                        Method: "CONNECT",
209
                        URL: &url.URL{
210
                                Host: "127.0.0.1:6060",
211
                        },
212
                        Proto:         "HTTP/1.1",
213
                        ProtoMajor:    1,
214
                        ProtoMinor:    1,
215
                        Header:        Header{},
216
                        Close:         false,
217
                        ContentLength: 0,
218
                        Host:          "127.0.0.1:6060",
219
                        RequestURI:    "127.0.0.1:6060",
220
                },
221
 
222
                noBody,
223
                noTrailer,
224
                noError,
225
        },
226
 
227
        // CONNECT request for RPC:
228
        {
229
                "CONNECT /_goRPC_ HTTP/1.1\r\n\r\n",
230
 
231
                &Request{
232
                        Method: "CONNECT",
233
                        URL: &url.URL{
234
                                Path: "/_goRPC_",
235
                        },
236
                        Proto:         "HTTP/1.1",
237
                        ProtoMajor:    1,
238
                        ProtoMinor:    1,
239
                        Header:        Header{},
240
                        Close:         false,
241
                        ContentLength: 0,
242
                        Host:          "",
243
                        RequestURI:    "/_goRPC_",
244
                },
245
 
246
                noBody,
247
                noTrailer,
248
                noError,
249
        },
250
}
251
 
252
func TestReadRequest(t *testing.T) {
253
        for i := range reqTests {
254
                tt := &reqTests[i]
255
                var braw bytes.Buffer
256
                braw.WriteString(tt.Raw)
257
                req, err := ReadRequest(bufio.NewReader(&braw))
258
                if err != nil {
259
                        if err.Error() != tt.Error {
260
                                t.Errorf("#%d: error %q, want error %q", i, err.Error(), tt.Error)
261
                        }
262
                        continue
263
                }
264
                rbody := req.Body
265
                req.Body = nil
266
                diff(t, fmt.Sprintf("#%d Request", i), req, tt.Req)
267
                var bout bytes.Buffer
268
                if rbody != nil {
269
                        _, err := io.Copy(&bout, rbody)
270
                        if err != nil {
271
                                t.Fatalf("#%d. copying body: %v", i, err)
272
                        }
273
                        rbody.Close()
274
                }
275
                body := bout.String()
276
                if body != tt.Body {
277
                        t.Errorf("#%d: Body = %q want %q", i, body, tt.Body)
278
                }
279
                if !reflect.DeepEqual(tt.Trailer, req.Trailer) {
280
                        t.Errorf("#%d. Trailers differ.\n got: %v\nwant: %v", i, req.Trailer, tt.Trailer)
281
                }
282
        }
283
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.