From: Brad Fitzpatrick Date: Wed, 18 Nov 2015 20:22:29 +0000 (+0000) Subject: net/http: accept empty method in Transport again X-Git-Tag: go1.6beta1~384 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e4a1acced742a53421e22fe498d66c81d623110b;p=gostls13.git net/http: accept empty method in Transport again Fix regression from https://golang.org/cl/16829 ("require valid methods in NewRequest and Transport.RoundTrip"). An empty string is a valid method (it means "GET", per the docs). Fixes #13311 Change-Id: I26b71dc4ccc146498b5d7e38fbe31ed11dd5a6cf Reviewed-on: https://go-review.googlesource.com/16952 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Andrew Gerrand --- diff --git a/src/net/http/transport.go b/src/net/http/transport.go index 96096a6bef..63abd377e9 100644 --- a/src/net/http/transport.go +++ b/src/net/http/transport.go @@ -237,7 +237,7 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) { req.closeBody() return nil, &badStringError{"unsupported protocol scheme", s} } - if !validMethod(req.Method) { + if req.Method != "" && !validMethod(req.Method) { return nil, fmt.Errorf("net/http: invalid method %q", req.Method) } if req.URL.Host == "" { diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go index a404eeb5cc..fc9dc5eb48 100644 --- a/src/net/http/transport_test.go +++ b/src/net/http/transport_test.go @@ -20,6 +20,7 @@ import ( "net" . "net/http" "net/http/httptest" + "net/http/httputil" "net/url" "os" "reflect" @@ -1775,7 +1776,6 @@ func TestTransportNoHost(t *testing.T) { defer afterTest(t) tr := &Transport{} _, err := tr.RoundTrip(&Request{ - Method: "GET", Header: make(Header), URL: &url.URL{ Scheme: "http", @@ -1787,6 +1787,19 @@ func TestTransportNoHost(t *testing.T) { } } +// Issue 13311 +func TestTransportEmptyMethod(t *testing.T) { + req, _ := NewRequest("GET", "http://foo.com/", nil) + req.Method = "" // docs say "For client requests an empty string means GET" + got, err := httputil.DumpRequestOut(req, false) // DumpRequestOut uses Transport + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(got), "GET ") { + t.Fatalf("expected substring 'GET '; got: %s", got) + } +} + func TestTransportSocketLateBinding(t *testing.T) { defer afterTest(t)