1 // Copyright 2011 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.
17 type dumpTest
struct {
19 Body
interface{} // optional []byte or func() io.ReadCloser to populate Req.Body
23 NoBody
bool // if true, set DumpRequest{,Out} body to false
26 var dumpTests
= []dumpTest
{
28 // HTTP/1.1 => chunked coding; body; empty trailer
34 Host
: "www.google.com",
39 TransferEncoding
: []string{"chunked"},
42 Body
: []byte("abcdef"),
44 WantDump
: "GET /search HTTP/1.1\r\n" +
45 "Host: www.google.com\r\n" +
46 "Transfer-Encoding: chunked\r\n\r\n" +
47 chunk("abcdef") + chunk(""),
50 // Verify that DumpRequest preserves the HTTP version number, doesn't add a Host,
51 // and doesn't add a User-Agent.
55 URL
: mustParseURL("/foo"),
59 "X-Foo": []string{"X-Bar"},
63 WantDump
: "GET /foo HTTP/1.0\r\n" +
64 "X-Foo: X-Bar\r\n\r\n",
68 Req
: *mustNewRequest("GET", "http://example.com/foo", nil),
70 WantDumpOut
: "GET /foo HTTP/1.1\r\n" +
71 "Host: example.com\r\n" +
72 "User-Agent: Go 1.1 package http\r\n" +
73 "Accept-Encoding: gzip\r\n\r\n",
76 // Test that an https URL doesn't try to do an SSL negotiation
77 // with a bytes.Buffer and hang with all goroutines not
80 Req
: *mustNewRequest("GET", "https://example.com/foo", nil),
82 WantDumpOut
: "GET /foo HTTP/1.1\r\n" +
83 "Host: example.com\r\n" +
84 "User-Agent: Go 1.1 package http\r\n" +
85 "Accept-Encoding: gzip\r\n\r\n",
88 // Request with Body, but Dump requested without it.
102 Body
: []byte("abcdef"),
104 WantDumpOut
: "POST / HTTP/1.1\r\n" +
105 "Host: post.tld\r\n" +
106 "User-Agent: Go 1.1 package http\r\n" +
107 "Content-Length: 6\r\n" +
108 "Accept-Encoding: gzip\r\n\r\n",
114 func TestDumpRequest(t
*testing
.T
) {
115 for i
, tt
:= range dumpTests
{
120 switch b
:= tt
.Body
.(type) {
122 tt
.Req
.Body
= ioutil
.NopCloser(bytes
.NewReader(b
))
123 case func() io
.ReadCloser
:
128 if tt
.Req
.Header
== nil {
129 tt
.Req
.Header
= make(http
.Header
)
132 if tt
.WantDump
!= "" {
134 dump
, err
:= DumpRequest(&tt
.Req
, !tt
.NoBody
)
136 t
.Errorf("DumpRequest #%d: %s", i
, err
)
139 if string(dump
) != tt
.WantDump
{
140 t
.Errorf("DumpRequest %d, expecting:\n%s\nGot:\n%s\n", i
, tt
.WantDump
, string(dump
))
145 if tt
.WantDumpOut
!= "" {
147 dump
, err
:= DumpRequestOut(&tt
.Req
, !tt
.NoBody
)
149 t
.Errorf("DumpRequestOut #%d: %s", i
, err
)
152 if string(dump
) != tt
.WantDumpOut
{
153 t
.Errorf("DumpRequestOut %d, expecting:\n%s\nGot:\n%s\n", i
, tt
.WantDumpOut
, string(dump
))
160 func chunk(s
string) string {
161 return fmt
.Sprintf("%x\r\n%s\r\n", len(s
), s
)
164 func mustParseURL(s
string) *url
.URL
{
165 u
, err
:= url
.Parse(s
)
167 panic(fmt
.Sprintf("Error parsing URL %q: %v", s
, err
))
172 func mustNewRequest(method
, url
string, body io
.Reader
) *http
.Request
{
173 req
, err
:= http
.NewRequest(method
, url
, body
)
175 panic(fmt
.Sprintf("NewRequest(%q, %q, %p) err = %v", method
, url
, body
, err
))