Commit 9733f96b authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

http: initialize request Header for the transport

Fixes #1558

R=rsc, r, bradfitzwork
CC=golang-dev
https://golang.org/cl/4260042
parent 1d258a55
...@@ -36,6 +36,9 @@ type ClientTransport interface { ...@@ -36,6 +36,9 @@ type ClientTransport interface {
// be reserved for failure to obtain a response. Similarly, Do should // be reserved for failure to obtain a response. Similarly, Do should
// not attempt to handle higher-level protocol details such as redirects, // not attempt to handle higher-level protocol details such as redirects,
// authentication, or cookies. // authentication, or cookies.
//
// Transports may modify the request. The request Headers field is
// guaranteed to be initalized.
Do(req *Request) (resp *Response, err os.Error) Do(req *Request) (resp *Response, err os.Error)
} }
...@@ -109,6 +112,14 @@ func send(req *Request, t ClientTransport) (resp *Response, err os.Error) { ...@@ -109,6 +112,14 @@ func send(req *Request, t ClientTransport) (resp *Response, err os.Error) {
return return
} }
} }
// Most the callers of send (Get, Post, et al) don't need
// Headers, leaving it uninitialized. We guarantee to the
// ClientTransport that this has been initialized, though.
if req.Header == nil {
req.Header = Header(make(map[string][]string))
}
info := req.URL.RawUserinfo info := req.URL.RawUserinfo
if len(info) > 0 { if len(info) > 0 {
enc := base64.URLEncoding enc := base64.URLEncoding
......
...@@ -55,9 +55,12 @@ func TestGetRequestFormat(t *testing.T) { ...@@ -55,9 +55,12 @@ func TestGetRequestFormat(t *testing.T) {
url := "http://dummy.faketld/" url := "http://dummy.faketld/"
client.Get(url) // Note: doesn't hit network client.Get(url) // Note: doesn't hit network
if tr.req.Method != "GET" { if tr.req.Method != "GET" {
t.Fatalf("expected method %q; got %q", "GET", tr.req.Method) t.Errorf("expected method %q; got %q", "GET", tr.req.Method)
} }
if tr.req.URL.String() != url { if tr.req.URL.String() != url {
t.Fatalf("expected URL %q; got %q", url, tr.req.URL.String()) t.Errorf("expected URL %q; got %q", url, tr.req.URL.String())
}
if tr.req.Header == nil {
t.Errorf("expected non-nil request Header")
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment