Commit e4a1acce authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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 <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarAndrew Gerrand <adg@golang.org>
parent e8e0d906
......@@ -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 == "" {
......
......@@ -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)
......
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