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.
20 type respTest struct {
26 func dummyReq(method string) *Request {
27 return &Request{Method: method}
30 var respTests = []respTest{
31 // Unchunked response without Content-Length.
33 "HTTP/1.0 200 OK\r\n" +
34 "Connection: close\r\n" +
44 Request: dummyReq("GET"),
46 "Connection": {"close"}, // TODO(rsc): Delete?
55 // Unchunked HTTP/1.1 response without Content-Length or
56 // Connection headers.
58 "HTTP/1.1 200 OK\r\n" +
68 Request: dummyReq("GET"),
76 // Unchunked HTTP/1.1 204 response without Content-Length.
78 "HTTP/1.1 204 No Content\r\n" +
80 "Body should not be read!\n",
83 Status: "204 No Content",
88 Request: dummyReq("GET"),
96 // Unchunked response with Content-Length.
98 "HTTP/1.0 200 OK\r\n" +
99 "Content-Length: 10\r\n" +
100 "Connection: close\r\n" +
110 Request: dummyReq("GET"),
112 "Connection": {"close"}, // TODO(rsc): Delete?
113 "Content-Length": {"10"}, // TODO(rsc): Delete?
122 // Chunked response without Content-Length.
124 "HTTP/1.0 200 OK\r\n" +
125 "Transfer-Encoding: chunked\r\n" +
140 Request: dummyReq("GET"),
144 TransferEncoding: []string{"chunked"},
147 "Body here\ncontinued",
150 // Chunked response with Content-Length.
152 "HTTP/1.0 200 OK\r\n" +
153 "Transfer-Encoding: chunked\r\n" +
154 "Content-Length: 10\r\n" +
167 Request: dummyReq("GET"),
170 ContentLength: -1, // TODO(rsc): Fix?
171 TransferEncoding: []string{"chunked"},
177 // Chunked response in response to a HEAD request (the "chunked" should
178 // be ignored, as HEAD responses never have bodies)
180 "HTTP/1.0 200 OK\r\n" +
181 "Transfer-Encoding: chunked\r\n" +
190 Request: dummyReq("HEAD"),
199 // explicit Content-Length of 0.
201 "HTTP/1.1 200 OK\r\n" +
202 "Content-Length: 0\r\n" +
211 Request: dummyReq("GET"),
213 "Content-Length": {"0"},
222 // Status line without a Reason-Phrase, but trailing space.
223 // (permitted by RFC 2616)
225 "HTTP/1.0 303 \r\n\r\n",
232 Request: dummyReq("GET"),
241 // Status line without a Reason-Phrase, and no trailing space.
242 // (not permitted by RFC 2616, but we'll accept it anyway)
244 "HTTP/1.0 303\r\n\r\n",
251 Request: dummyReq("GET"),
261 func TestReadResponse(t *testing.T) {
262 for i := range respTests {
264 var braw bytes.Buffer
265 braw.WriteString(tt.Raw)
266 resp, err := ReadResponse(bufio.NewReader(&braw), tt.Resp.Request)
268 t.Errorf("#%d: %s", i, err)
273 diff(t, fmt.Sprintf("#%d Response", i), resp, &tt.Resp)
274 var bout bytes.Buffer
276 io.Copy(&bout, rbody)
279 body := bout.String()
281 t.Errorf("#%d: Body = %q want %q", i, body, tt.Body)
286 var readResponseCloseInMiddleTests = []struct {
287 chunked, compressed bool
294 // TestReadResponseCloseInMiddle tests that closing a body after
295 // reading only part of its contents advances the read to the end of
296 // the request, right up until the next request.
297 func TestReadResponseCloseInMiddle(t *testing.T) {
298 for _, test := range readResponseCloseInMiddleTests {
299 fatalf := func(format string, args ...interface{}) {
300 args = append([]interface{}{test.chunked, test.compressed}, args...)
301 t.Fatalf("on test chunked=%v, compressed=%v: "+format, args...)
303 checkErr := func(err error, msg string) {
307 fatalf(msg+": %v", err)
310 buf.WriteString("HTTP/1.1 200 OK\r\n")
312 buf.WriteString("Transfer-Encoding: chunked\r\n")
314 buf.WriteString("Content-Length: 1000000\r\n")
316 var wr io.Writer = &buf
318 wr = &chunkedWriter{wr}
321 buf.WriteString("Content-Encoding: gzip\r\n")
323 wr, err = gzip.NewWriter(wr)
324 checkErr(err, "gzip.NewWriter")
326 buf.WriteString("\r\n")
328 chunk := bytes.Repeat([]byte{'x'}, 1000)
329 for i := 0; i < 1000; i++ {
331 // Otherwise this compresses too well.
332 _, err := io.ReadFull(rand.Reader, chunk)
333 checkErr(err, "rand.Reader ReadFull")
338 err := wr.(*gzip.Compressor).Close()
339 checkErr(err, "compressor close")
342 buf.WriteString("0\r\n\r\n")
344 buf.WriteString("Next Request Here")
346 bufr := bufio.NewReader(&buf)
347 resp, err := ReadResponse(bufr, dummyReq("GET"))
348 checkErr(err, "ReadResponse")
349 expectedLength := int64(-1)
351 expectedLength = 1000000
353 if resp.ContentLength != expectedLength {
354 fatalf("expected response length %d, got %d", expectedLength, resp.ContentLength)
356 if resp.Body == nil {
360 gzReader, err := gzip.NewReader(resp.Body)
361 checkErr(err, "gzip.NewReader")
362 resp.Body = &readFirstCloseBoth{gzReader, resp.Body}
365 rbuf := make([]byte, 2500)
366 n, err := io.ReadFull(resp.Body, rbuf)
367 checkErr(err, "2500 byte ReadFull")
369 fatalf("ReadFull only read %d bytes", n)
371 if test.compressed == false && !bytes.Equal(bytes.Repeat([]byte{'x'}, 2500), rbuf) {
372 fatalf("ReadFull didn't read 2500 'x'; got %q", string(rbuf))
376 rest, err := ioutil.ReadAll(bufr)
377 checkErr(err, "ReadAll on remainder")
378 if e, g := "Next Request Here", string(rest); e != g {
379 fatalf("remainder = %q, expected %q", g, e)
384 func diff(t *testing.T, prefix string, have, want interface{}) {
385 hv := reflect.ValueOf(have).Elem()
386 wv := reflect.ValueOf(want).Elem()
387 if hv.Type() != wv.Type() {
388 t.Errorf("%s: type mismatch %v want %v", prefix, hv.Type(), wv.Type())
390 for i := 0; i < hv.NumField(); i++ {
391 hf := hv.Field(i).Interface()
392 wf := wv.Field(i).Interface()
393 if !reflect.DeepEqual(hf, wf) {
394 t.Errorf("%s: %s = %v want %v", prefix, hv.Type().Field(i).Name, hf, wf)
399 type responseLocationTest struct {
400 location string // Response's Location header or ""
401 requrl string // Response.Request.URL or ""
406 var responseLocationTests = []responseLocationTest{
407 {"/foo", "http://bar.com/baz", "http://bar.com/foo", nil},
408 {"http://foo.com/", "http://bar.com/baz", "http://foo.com/", nil},
409 {"", "http://bar.com/baz", "", ErrNoLocation},
412 func TestLocationResponse(t *testing.T) {
413 for i, tt := range responseLocationTests {
415 res.Header = make(Header)
416 res.Header.Set("Location", tt.location)
418 res.Request = &Request{}
420 res.Request.URL, err = url.Parse(tt.requrl)
422 t.Fatalf("bad test URL %q: %v", tt.requrl, err)
426 got, err := res.Location()
427 if tt.wantErr != nil {
429 t.Errorf("%d. err=nil; want %q", i, tt.wantErr)
432 if g, e := err.Error(), tt.wantErr.Error(); g != e {
433 t.Errorf("%d. err=%q; want %q", i, g, e)
439 t.Errorf("%d. err=%q", i, err)
442 if g, e := got.String(), tt.want; g != e {
443 t.Errorf("%d. Location=%q; want %q", i, g, e)