Skip to content

Commit

Permalink
net/http: accept empty method in Transport again
Browse files Browse the repository at this point in the history
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 golang#13311

Change-Id: I26b71dc4ccc146498b5d7e38fbe31ed11dd5a6cf
Reviewed-on: https://go-review.googlesource.com/16952
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Andrew Gerrand <[email protected]>
  • Loading branch information
bradfitz committed Nov 19, 2015
1 parent e8e0d90 commit e4a1acc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/net/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
15 changes: 14 additions & 1 deletion src/net/http/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net"
. "net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"reflect"
Expand Down Expand Up @@ -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",
Expand All @@ -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)

Expand Down

0 comments on commit e4a1acc

Please sign in to comment.