OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package http | 5 package http |
6 | 6 |
7 import ( | 7 import ( |
8 "bufio" | 8 "bufio" |
9 "bytes" | 9 "bytes" |
10 "fmt" | 10 "fmt" |
11 "io" | 11 "io" |
12 "net/url" | 12 "net/url" |
13 "reflect" | 13 "reflect" |
| 14 "strings" |
14 "testing" | 15 "testing" |
15 ) | 16 ) |
16 | 17 |
17 type reqTest struct { | 18 type reqTest struct { |
18 Raw string | 19 Raw string |
19 Req *Request | 20 Req *Request |
20 Body string | 21 Body string |
21 Trailer Header | 22 Trailer Header |
22 Error string | 23 Error string |
23 } | 24 } |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 }, | 289 }, |
289 Close: false, | 290 Close: false, |
290 ContentLength: 0, | 291 ContentLength: 0, |
291 RequestURI: "*", | 292 RequestURI: "*", |
292 }, | 293 }, |
293 | 294 |
294 noBody, | 295 noBody, |
295 noTrailer, | 296 noTrailer, |
296 noError, | 297 noError, |
297 }, | 298 }, |
| 299 |
| 300 // Connection: close. golang.org/issue/8261 |
| 301 { |
| 302 "GET / HTTP/1.1\r\nHost: issue8261.com\r\nConnection: close\r\n\
r\n", |
| 303 &Request{ |
| 304 Method: "GET", |
| 305 URL: &url.URL{ |
| 306 Path: "/", |
| 307 }, |
| 308 Header: Header{ |
| 309 // This wasn't removed from Go 1.0 to |
| 310 // Go 1.3, so locking it in that we |
| 311 // keep this: |
| 312 "Connection": []string{"close"}, |
| 313 }, |
| 314 Host: "issue8261.com", |
| 315 Proto: "HTTP/1.1", |
| 316 ProtoMajor: 1, |
| 317 ProtoMinor: 1, |
| 318 Close: true, |
| 319 RequestURI: "/", |
| 320 }, |
| 321 |
| 322 noBody, |
| 323 noTrailer, |
| 324 noError, |
| 325 }, |
298 } | 326 } |
299 | 327 |
300 func TestReadRequest(t *testing.T) { | 328 func TestReadRequest(t *testing.T) { |
301 for i := range reqTests { | 329 for i := range reqTests { |
302 tt := &reqTests[i] | 330 tt := &reqTests[i] |
303 » » var braw bytes.Buffer | 331 » » req, err := ReadRequest(bufio.NewReader(strings.NewReader(tt.Raw
))) |
304 » » braw.WriteString(tt.Raw) | |
305 » » req, err := ReadRequest(bufio.NewReader(&braw)) | |
306 if err != nil { | 332 if err != nil { |
307 if err.Error() != tt.Error { | 333 if err.Error() != tt.Error { |
308 t.Errorf("#%d: error %q, want error %q", i, err.
Error(), tt.Error) | 334 t.Errorf("#%d: error %q, want error %q", i, err.
Error(), tt.Error) |
309 } | 335 } |
310 continue | 336 continue |
311 } | 337 } |
312 rbody := req.Body | 338 rbody := req.Body |
313 req.Body = nil | 339 req.Body = nil |
314 » » diff(t, fmt.Sprintf("#%d Request", i), req, tt.Req) | 340 » » testName := fmt.Sprintf("Test %d (%q)", i, tt.Raw) |
| 341 » » diff(t, testName, req, tt.Req) |
315 var bout bytes.Buffer | 342 var bout bytes.Buffer |
316 if rbody != nil { | 343 if rbody != nil { |
317 _, err := io.Copy(&bout, rbody) | 344 _, err := io.Copy(&bout, rbody) |
318 if err != nil { | 345 if err != nil { |
319 » » » » t.Fatalf("#%d. copying body: %v", i, err) | 346 » » » » t.Fatalf("%s: copying body: %v", testName, err) |
320 } | 347 } |
321 rbody.Close() | 348 rbody.Close() |
322 } | 349 } |
323 body := bout.String() | 350 body := bout.String() |
324 if body != tt.Body { | 351 if body != tt.Body { |
325 » » » t.Errorf("#%d: Body = %q want %q", i, body, tt.Body) | 352 » » » t.Errorf("%s: Body = %q want %q", testName, body, tt.Bod
y) |
326 } | 353 } |
327 if !reflect.DeepEqual(tt.Trailer, req.Trailer) { | 354 if !reflect.DeepEqual(tt.Trailer, req.Trailer) { |
328 » » » t.Errorf("#%d. Trailers differ.\n got: %v\nwant: %v", i,
req.Trailer, tt.Trailer) | 355 » » » t.Errorf("%s: Trailers differ.\n got: %v\nwant: %v", tes
tName, req.Trailer, tt.Trailer) |
329 } | 356 } |
330 } | 357 } |
331 } | 358 } |
OLD | NEW |