1
0
mirror of https://github.com/golang/go synced 2024-11-20 06:54:42 -07:00

http: initialize request Header for the transport

Fixes #1558

R=rsc, r, bradfitzwork
CC=golang-dev
https://golang.org/cl/4260042
This commit is contained in:
Brad Fitzpatrick 2011-03-02 10:21:56 -08:00
parent 1d258a554a
commit 9733f96b47
2 changed files with 16 additions and 2 deletions

View File

@ -36,6 +36,9 @@ type ClientTransport interface {
// be reserved for failure to obtain a response. Similarly, Do should
// not attempt to handle higher-level protocol details such as redirects,
// 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)
}
@ -109,6 +112,14 @@ func send(req *Request, t ClientTransport) (resp *Response, err os.Error) {
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
if len(info) > 0 {
enc := base64.URLEncoding

View File

@ -55,9 +55,12 @@ func TestGetRequestFormat(t *testing.T) {
url := "http://dummy.faketld/"
client.Get(url) // Note: doesn't hit network
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 {
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")
}
}