Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / http / response_test.go
blobf21587fd46b8a512217f69eba426d43609badfc6
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.
5 package http
7 import (
8 "bufio"
9 "bytes"
10 "fmt"
11 "io"
12 "reflect"
13 "testing"
16 type respTest struct {
17 Raw string
18 Resp Response
19 Body string
22 var respTests = []respTest{
23 // Unchunked response without Content-Length.
25 "HTTP/1.0 200 OK\r\n" +
26 "Connection: close\r\n" +
27 "\r\n" +
28 "Body here\n",
30 Response{
31 Status: "200 OK",
32 StatusCode: 200,
33 Proto: "HTTP/1.0",
34 ProtoMajor: 1,
35 ProtoMinor: 0,
36 RequestMethod: "GET",
37 Header: map[string]string{
38 "Connection": "close", // TODO(rsc): Delete?
40 Close: true,
41 ContentLength: -1,
44 "Body here\n",
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" +
52 "\r\n" +
53 "Body here\n",
55 Response{
56 Status: "200 OK",
57 StatusCode: 200,
58 Proto: "HTTP/1.0",
59 ProtoMajor: 1,
60 ProtoMinor: 0,
61 RequestMethod: "GET",
62 Header: map[string]string{
63 "Connection": "close", // TODO(rsc): Delete?
64 "Content-Length": "10", // TODO(rsc): Delete?
66 Close: true,
67 ContentLength: 10,
70 "Body here\n",
73 // Chunked response without Content-Length.
75 "HTTP/1.0 200 OK\r\n" +
76 "Transfer-Encoding: chunked\r\n" +
77 "\r\n" +
78 "0a\r\n" +
79 "Body here\n" +
80 "0\r\n" +
81 "\r\n",
83 Response{
84 Status: "200 OK",
85 StatusCode: 200,
86 Proto: "HTTP/1.0",
87 ProtoMajor: 1,
88 ProtoMinor: 0,
89 RequestMethod: "GET",
90 Header: map[string]string{},
91 Close: true,
92 ContentLength: -1,
93 TransferEncoding: []string{"chunked"},
96 "Body here\n",
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" +
104 "\r\n" +
105 "0a\r\n" +
106 "Body here\n" +
107 "0\r\n" +
108 "\r\n",
110 Response{
111 Status: "200 OK",
112 StatusCode: 200,
113 Proto: "HTTP/1.0",
114 ProtoMajor: 1,
115 ProtoMinor: 0,
116 RequestMethod: "GET",
117 Header: map[string]string{},
118 Close: true,
119 ContentLength: -1, // TODO(rsc): Fix?
120 TransferEncoding: []string{"chunked"},
123 "Body here\n",
127 func TestReadResponse(t *testing.T) {
128 for i := range respTests {
129 tt := &respTests[i]
130 var braw bytes.Buffer
131 braw.WriteString(tt.Raw)
132 resp, err := ReadResponse(bufio.NewReader(&braw), tt.Resp.RequestMethod)
133 if err != nil {
134 t.Errorf("#%d: %s", i, err)
135 continue
137 rbody := resp.Body
138 resp.Body = nil
139 diff(t, fmt.Sprintf("#%d Response", i), resp, &tt.Resp)
140 var bout bytes.Buffer
141 if rbody != nil {
142 io.Copy(&bout, rbody)
143 rbody.Close()
145 body := bout.String()
146 if body != tt.Body {
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)