Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http/httptest: NewRequest sets body to {} instead of nil #20965

Closed
devdinu opened this issue Jul 10, 2017 · 3 comments
Closed

net/http/httptest: NewRequest sets body to {} instead of nil #20965

devdinu opened this issue Jul 10, 2017 · 3 comments

Comments

@devdinu
Copy link

devdinu commented Jul 10, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.8rc2 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOBIN="/Users/dineshkumar/Documents/Learn/Golang/workspace/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/dineshkumar/Documents/Learn/Golang/workspace"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/cw/0ps5z6nd6m19x9vpdrwlbm_m0000gn/T/go-build939833936=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

httptest.NewRequest("GET", "/path/some_url", nil)


import (
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestHttpNewRequest(t *testing.T) {
	r := httptest.NewRequest(http.MethodGet, "/some_url", nil)
	httpRequest, _ := http.NewRequest(http.MethodGet, "/some_url", nil)

	dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Body != nil {
			t.Log("body should be nil")
			t.FailNow()
		}
	})

	dummyHandler(httptest.NewRecorder(), httpRequest)
	t.Log("http Request works as expected")
	dummyHandler(httptest.NewRecorder(), r)
	t.Log("httptest Request works as expected")
}

What did you expect to see?

httptest request works as expected, which is its body to be nil.

What did you see instead?

it fails for body being not nil. it actually sets the body to be {}.

@devdinu devdinu changed the title httptest setting body to {} when its nil httptest NewRequest sets body to {} instead of nil Jul 10, 2017
@bradfitz
Copy link
Contributor

Working as intended:

https://golang.org/pkg/net/http/#Request.Body

        // For server requests the Request Body is always non-nil
        // but will return EOF immediately when no body is present.
        // The Server will close the request body. The ServeHTTP
        // Handler does not need to.

@devdinu
Copy link
Author

devdinu commented Jul 10, 2017

@bradfitz sorry, could you please elaborate for me to understand, so if it should be non-nil then why does http.NewRequest results in nil body?

(or) Does this mean when the request is sent to (actual) server it will have an empty body?

@bradfitz
Copy link
Contributor

httptest.NewRequest is for generating incoming (server) requests, for an http.Handler.

http.NewRequest is for generating outgoing (client) requests, for http.Transport.

The Request fields have slightly different (but documented) semantics and requirements for client vs. server.

@mikioh mikioh changed the title httptest NewRequest sets body to {} instead of nil net/http/httptest: NewRequest sets body to {} instead of nil Jul 21, 2017
@golang golang locked and limited conversation to collaborators Jul 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants