-
Couldn't load subscription status.
- Fork 18.4k
Description
httptest.NewRequest can create HTTP requests that differ subtly from that of production HTTP servers resulting in misleading test behavior.
Specifically, when httptest.NewRequest is passed a complete URL as its second argument, it will parse the URL and return a http.Request whose URL fields are populated with all of the data in that URL.
However in production per the http.Request Go documentation the http.Request objects passed to handlers will have URLs with only Path and RawQuery populated.
// URL specifies either the URI being requested (for server
// requests) or the URL to access (for client requests).
//
// For server requests, the URL is parsed from the URI
// supplied on the Request-Line as stored in RequestURI. For
// most requests, fields other than Path and RawQuery will be
// empty. (See [RFC 7230, Section 5.3](https://rfc-editor.org/rfc/rfc7230.html#section-5.3))
//
// For client requests, the URL's Host specifies the server to
// connect to, while the Request's Host field optionally
// specifies the Host header value to send in the HTTP
// request.
URL *[url](https://pkg.go.dev/net/url).[URL](https://pkg.go.dev/net/url#URL)
The result is that tests that consume httptest.NewRequest and that rely on the presence of URL fields other than Path and RawQuery will pass under go test but the behavior that they exercise will never actually execute in production.
An example of this misleading behavior can be found in the gorilla/csrf library whose Referer header checks were reliant on r.URL.Scheme being set to https.
/cc @golang/security