]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: accept empty method in Transport again
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 18 Nov 2015 20:22:29 +0000 (20:22 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 19 Nov 2015 01:35:36 +0000 (01:35 +0000)
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: Andrew Gerrand <adg@golang.org>
src/net/http/transport.go
src/net/http/transport_test.go

index 96096a6bef83b52ca1c98b8a5e138badf61a96c0..63abd377e97049a4e25ad04f63879e3e09adbcad 100644 (file)
@@ -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 == "" {
index a404eeb5ccf3568fd3c2e98775d2e010f3f34c96..fc9dc5eb488a2b55d35da745a41e794cc1ab88e5 100644 (file)
@@ -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)