You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When http.NewRequest is called, it does not fill in (http.Request).RequestURI, but http.Serverdoes fill in (http.Request).RequestURI, which has caused issues writing tests that use a httptest.ResponseRecorder instead of a httptest.Server.
A test case:
package issue
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestNewRequestRequestURI(t *testing.T) {
var httpServerRequestURI string
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
httpServerRequestURI = r.RequestURI
})
srv := httptest.NewServer(h)
defer srv.Close()
req, err := http.NewRequest("GET", srv.URL+"/path", nil)
if err != nil {
t.Fatal(err)
}
newRequestRequestURI := req.RequestURI
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if newRequestRequestURI != httpServerRequestURI {
t.Fatalf("NewRequest().RequestURI = %#v, but sending to server yielded %#v",
newRequestRequestURI, httpServerRequestURI)
}
}
Which yields:
--- FAIL: TestNewRequestRequestURI (0.00s)
test_test.go:33: NewRequest().RequestURI = "", but sending to server yielded "/path"
FAIL
exit status 1
FAIL issue 0.004s
On go version go1.6 linux/amd64 and go version devel +c12e1b0 Tue Mar 22 17:09:29 2016 +0000 linux/amd64.
The text was updated successfully, but these errors were encountered:
// RequestURI is the unmodified Request-URI of the
// Request-Line (RFC 2616, Section 5.1) as sent by the client
// to a server. Usually the URL field should be used instead.
// It is an error to set this field in an HTTP client request.
RequestURI string
NewRequest returns a Request suitable for use with Client.Do
or Transport.RoundTrip. To create a request for use with testing
a Server Handler use either ReadRequest or manually update the
Request fields. See the Request type's documentation for the
difference between inbound and outbound request fields.
For Go 1.7, I do want to make a new httptest helper to make it easier to create a Request for testing, though. I think there's already an open bug, but just in case I'll re-open this one and assign it to me.
When
http.NewRequest
is called, it does not fill in(http.Request).RequestURI
, buthttp.Server
does fill in(http.Request).RequestURI
, which has caused issues writing tests that use ahttptest.ResponseRecorder
instead of ahttptest.Server
.A test case:
Which yields:
On
go version go1.6 linux/amd64
andgo version devel +c12e1b0 Tue Mar 22 17:09:29 2016 +0000 linux/amd64
.The text was updated successfully, but these errors were encountered: