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.
14
type respWriteTest struct {
19
func TestResponseWrite(t *testing.T) {
20
respWriteTests := []respWriteTest{
21
// HTTP/1.0, identity coding; no trailer
27
Request: dummyReq("GET"),
29
Body: ioutil.NopCloser(strings.NewReader("abcdef")),
33
"HTTP/1.0 503 Service Unavailable\r\n" +
34
"Content-Length: 6\r\n\r\n" +
37
// Unchunked response without Content-Length.
43
Request: dummyReq("GET"),
45
Body: ioutil.NopCloser(strings.NewReader("abcdef")),
48
"HTTP/1.0 200 OK\r\n" +
52
// HTTP/1.1 response with unknown length and Connection: close
58
Request: dummyReq("GET"),
60
Body: ioutil.NopCloser(strings.NewReader("abcdef")),
64
"HTTP/1.1 200 OK\r\n" +
65
"Connection: close\r\n" +
69
// HTTP/1.1 response with unknown length and not setting connection: close
75
Request: dummyReq11("GET"),
77
Body: ioutil.NopCloser(strings.NewReader("abcdef")),
81
"HTTP/1.1 200 OK\r\n" +
82
"Connection: close\r\n" +
86
// HTTP/1.1 response with unknown length and not setting connection: close, but
93
Request: dummyReq11("GET"),
95
Body: ioutil.NopCloser(strings.NewReader("abcdef")),
97
TransferEncoding: []string{"chunked"},
100
"HTTP/1.1 200 OK\r\n" +
101
"Transfer-Encoding: chunked\r\n\r\n" +
102
"6\r\nabcdef\r\n0\r\n\r\n",
104
// HTTP/1.1 response 0 content-length, and nil body
110
Request: dummyReq11("GET"),
116
"HTTP/1.1 200 OK\r\n" +
117
"Content-Length: 0\r\n" +
120
// HTTP/1.1 response 0 content-length, and non-nil empty body
126
Request: dummyReq11("GET"),
128
Body: ioutil.NopCloser(strings.NewReader("")),
132
"HTTP/1.1 200 OK\r\n" +
133
"Content-Length: 0\r\n" +
136
// HTTP/1.1 response 0 content-length, and non-nil non-empty body
142
Request: dummyReq11("GET"),
144
Body: ioutil.NopCloser(strings.NewReader("foo")),
148
"HTTP/1.1 200 OK\r\n" +
149
"Connection: close\r\n" +
152
// HTTP/1.1, chunked coding; empty trailer; close
158
Request: dummyReq("GET"),
160
Body: ioutil.NopCloser(strings.NewReader("abcdef")),
162
TransferEncoding: []string{"chunked"},
166
"HTTP/1.1 200 OK\r\n" +
167
"Connection: close\r\n" +
168
"Transfer-Encoding: chunked\r\n\r\n" +
169
"6\r\nabcdef\r\n0\r\n\r\n",
172
// Header value with a newline character (Issue 914).
173
// Also tests removal of leading and trailing whitespace.
179
Request: dummyReq("GET"),
181
"Foo": []string{" Bar\nBaz "},
185
TransferEncoding: []string{"chunked"},
189
"HTTP/1.1 204 No Content\r\n" +
190
"Connection: close\r\n" +
195
// Want a single Content-Length header. Fixing issue 8180 where
199
StatusCode: StatusOK,
202
Request: &Request{Method: "POST"},
205
TransferEncoding: nil,
208
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
211
// When a response to a POST has Content-Length: -1, make sure we don't
212
// write the Content-Length as -1.
215
StatusCode: StatusOK,
218
Request: &Request{Method: "POST"},
221
Body: ioutil.NopCloser(strings.NewReader("abcdef")),
223
"HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nabcdef",
226
// Status code under 100 should be zero-padded to
227
// three digits. Still bogus, but less bogus. (be
228
// consistent with generating three digits, since the
229
// Transport requires it)
233
Status: "license to violate specs",
236
Request: dummyReq("GET"),
241
"HTTP/1.0 007 license to violate specs\r\nContent-Length: 0\r\n\r\n",
244
// No stutter. Status code in 1xx range response should
245
// not include a Content-Length header. See issue #16942.
249
Status: "123 Sesame Street",
252
Request: dummyReq("GET"),
257
"HTTP/1.0 123 Sesame Street\r\n\r\n",
260
// Status code 204 (No content) response should not include a
261
// Content-Length header. See issue #16942.
265
Status: "No Content",
268
Request: dummyReq("GET"),
273
"HTTP/1.0 204 No Content\r\n\r\n",
277
for i := range respWriteTests {
278
tt := &respWriteTests[i]
279
var braw bytes.Buffer
280
err := tt.Resp.Write(&braw)
282
t.Errorf("error writing #%d: %s", i, err)
285
sraw := braw.String()
287
t.Errorf("Test %d, expecting:\n%q\nGot:\n%q\n", i, tt.Raw, sraw)