OSDN Git Service

libgo: Update to weekly 2011-11-09.
[pf3gnuchains/gcc-fork.git] / libgo / go / net / http / responsewrite_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         "bytes"
9         "io/ioutil"
10         "testing"
11 )
12
13 type respWriteTest struct {
14         Resp Response
15         Raw  string
16 }
17
18 var respWriteTests = []respWriteTest{
19         // HTTP/1.0, identity coding; no trailer
20         {
21                 Response{
22                         StatusCode:    503,
23                         ProtoMajor:    1,
24                         ProtoMinor:    0,
25                         Request:       dummyReq("GET"),
26                         Header:        Header{},
27                         Body:          ioutil.NopCloser(bytes.NewBufferString("abcdef")),
28                         ContentLength: 6,
29                 },
30
31                 "HTTP/1.0 503 Service Unavailable\r\n" +
32                         "Content-Length: 6\r\n\r\n" +
33                         "abcdef",
34         },
35         // Unchunked response without Content-Length.
36         {
37                 Response{
38                         StatusCode:    200,
39                         ProtoMajor:    1,
40                         ProtoMinor:    0,
41                         Request:       dummyReq("GET"),
42                         Header:        Header{},
43                         Body:          ioutil.NopCloser(bytes.NewBufferString("abcdef")),
44                         ContentLength: -1,
45                 },
46                 "HTTP/1.0 200 OK\r\n" +
47                         "\r\n" +
48                         "abcdef",
49         },
50         // HTTP/1.1, chunked coding; empty trailer; close
51         {
52                 Response{
53                         StatusCode:       200,
54                         ProtoMajor:       1,
55                         ProtoMinor:       1,
56                         Request:          dummyReq("GET"),
57                         Header:           Header{},
58                         Body:             ioutil.NopCloser(bytes.NewBufferString("abcdef")),
59                         ContentLength:    6,
60                         TransferEncoding: []string{"chunked"},
61                         Close:            true,
62                 },
63
64                 "HTTP/1.1 200 OK\r\n" +
65                         "Connection: close\r\n" +
66                         "Transfer-Encoding: chunked\r\n\r\n" +
67                         "6\r\nabcdef\r\n0\r\n\r\n",
68         },
69
70         // Header value with a newline character (Issue 914).
71         // Also tests removal of leading and trailing whitespace.
72         {
73                 Response{
74                         StatusCode: 204,
75                         ProtoMajor: 1,
76                         ProtoMinor: 1,
77                         Request:    dummyReq("GET"),
78                         Header: Header{
79                                 "Foo": []string{" Bar\nBaz "},
80                         },
81                         Body:             nil,
82                         ContentLength:    0,
83                         TransferEncoding: []string{"chunked"},
84                         Close:            true,
85                 },
86
87                 "HTTP/1.1 204 No Content\r\n" +
88                         "Connection: close\r\n" +
89                         "Foo: Bar Baz\r\n" +
90                         "\r\n",
91         },
92 }
93
94 func TestResponseWrite(t *testing.T) {
95         for i := range respWriteTests {
96                 tt := &respWriteTests[i]
97                 var braw bytes.Buffer
98                 err := tt.Resp.Write(&braw)
99                 if err != nil {
100                         t.Errorf("error writing #%d: %s", i, err)
101                         continue
102                 }
103                 sraw := braw.String()
104                 if sraw != tt.Raw {
105                         t.Errorf("Test %d, expecting:\n%q\nGot:\n%q\n", i, tt.Raw, sraw)
106                         continue
107                 }
108         }
109 }