]> Cypherpunks repositories - gostls13.git/commitdiff
http: initialize request Header for the transport
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 2 Mar 2011 18:21:56 +0000 (10:21 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 2 Mar 2011 18:21:56 +0000 (10:21 -0800)
Fixes #1558

R=rsc, r, bradfitzwork
CC=golang-dev
https://golang.org/cl/4260042

src/pkg/http/client.go
src/pkg/http/client_test.go

index cbd628014eaf6bdb9728051dfbc3a7efa5bb1d62..b1fe5ec67803454a823dd37cb374e1f2a4d86b00 100644 (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
index 1b0fe0350910c8c1c2589ededf8915e497bc96cf..c89ecbce2d0d1a1a8fe422e775857adaebfa3bc9 100644 (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")
        }
 }