From 2f0bfce5390b97b43b7ac8b267d2d0e0bbab4579 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Thu, 14 Oct 2021 19:38:31 +0200 Subject: [PATCH] net/http: omit invalid header value from error message Updates #43631 --- src/net/http/transport.go | 2 +- src/net/http/transport_test.go | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/net/http/transport.go b/src/net/http/transport.go index 17f0047b59982..788d480c9ed2e 100644 --- a/src/net/http/transport.go +++ b/src/net/http/transport.go @@ -525,7 +525,7 @@ func (t *Transport) roundTrip(req *Request) (*Response, error) { for _, v := range vv { if !httpguts.ValidHeaderFieldValue(v) { req.closeBody() - return nil, fmt.Errorf("net/http: invalid header field value %q for key %v", v, k) + return nil, fmt.Errorf("net/http: invalid header field value for %q", k) } } } diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go index 0cdd946de4269..00a05f3f71039 100644 --- a/src/net/http/transport_test.go +++ b/src/net/http/transport_test.go @@ -6060,14 +6060,14 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) { Method: " ", URL: u, }, - wantErr: "invalid method", + wantErr: `invalid method " "`, }, { name: "nil URL", req: &Request{ Method: "GET", }, - wantErr: "nil Request.URL", + wantErr: `nil Request.URL`, }, { name: "invalid header key", @@ -6076,7 +6076,7 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) { Header: Header{"💡": {"emoji"}}, URL: u, }, - wantErr: "invalid header field name", + wantErr: `invalid header field name "💡"`, }, { name: "invalid header value", @@ -6085,7 +6085,7 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) { Header: Header{"key": {"\x19"}}, URL: u, }, - wantErr: "invalid header field value", + wantErr: `invalid header field value for "key"`, // omits actual value }, { name: "non HTTP(s) scheme", @@ -6093,7 +6093,7 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) { Method: "POST", URL: &url.URL{Scheme: "faux"}, }, - wantErr: "unsupported protocol scheme", + wantErr: `unsupported protocol scheme "faux"`, }, { name: "no Host in URL", @@ -6101,7 +6101,7 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) { Method: "POST", URL: &url.URL{Scheme: "http"}, }, - wantErr: "no Host", + wantErr: `no Host in request URL`, }, } @@ -6117,8 +6117,8 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) { if !bc { t.Fatal("Expected body to have been closed") } - if g, w := err.Error(), tt.wantErr; !strings.Contains(g, w) { - t.Fatalf("Error mismatch\n\t%q\ndoes not contain\n\t%q", g, w) + if g, w := err.Error(), tt.wantErr; !strings.HasSuffix(g, w) { + t.Fatalf("Error mismatch: %q does not end with %q", g, w) } }) }