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.
16 type respTest
struct {
22 var respTests
= []respTest
{
23 // Unchunked response without Content-Length.
25 "HTTP/1.0 200 OK\r\n" +
26 "Connection: close\r\n" +
37 Header
: map[string]string{
38 "Connection": "close", // TODO(rsc): Delete?
47 // Unchunked response with Content-Length.
49 "HTTP/1.0 200 OK\r\n" +
50 "Content-Length: 10\r\n" +
51 "Connection: close\r\n" +
62 Header
: map[string]string{
63 "Connection": "close", // TODO(rsc): Delete?
64 "Content-Length": "10", // TODO(rsc): Delete?
73 // Chunked response without Content-Length.
75 "HTTP/1.0 200 OK\r\n" +
76 "Transfer-Encoding: chunked\r\n" +
90 Header
: map[string]string{},
93 TransferEncoding
: []string{"chunked"},
99 // Chunked response with Content-Length.
101 "HTTP/1.0 200 OK\r\n" +
102 "Transfer-Encoding: chunked\r\n" +
103 "Content-Length: 10\r\n" +
116 RequestMethod
: "GET",
117 Header
: map[string]string{},
119 ContentLength
: -1, // TODO(rsc): Fix?
120 TransferEncoding
: []string{"chunked"},
127 func TestReadResponse(t
*testing
.T
) {
128 for i
:= range respTests
{
130 var braw bytes
.Buffer
131 braw
.WriteString(tt
.Raw
)
132 resp
, err
:= ReadResponse(bufio
.NewReader(&braw
), tt
.Resp
.RequestMethod
)
134 t
.Errorf("#%d: %s", i
, err
)
139 diff(t
, fmt
.Sprintf("#%d Response", i
), resp
, &tt
.Resp
)
140 var bout bytes
.Buffer
142 io
.Copy(&bout
, rbody
)
145 body
:= bout
.String()
147 t
.Errorf("#%d: Body = %q want %q", i
, body
, tt
.Body
)
152 func diff(t
*testing
.T
, prefix
string, have
, want
interface{}) {
153 hv
:= reflect
.NewValue(have
).(*reflect
.PtrValue
).Elem().(*reflect
.StructValue
)
154 wv
:= reflect
.NewValue(want
).(*reflect
.PtrValue
).Elem().(*reflect
.StructValue
)
155 if hv
.Type() != wv
.Type() {
156 t
.Errorf("%s: type mismatch %v vs %v", prefix
, hv
.Type(), wv
.Type())
158 for i
:= 0; i
< hv
.NumField(); i
++ {
159 hf
:= hv
.Field(i
).Interface()
160 wf
:= wv
.Field(i
).Interface()
161 if !reflect
.DeepEqual(hf
, wf
) {
162 t
.Errorf("%s: %s = %v want %v", prefix
, hv
.Type().(*reflect
.StructType
).Field(i
).Name
, hf
, wf
)