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: wrong ContentLength for request with http.NoBody #68476

Open
jfrancisco0 opened this issue Jul 16, 2024 · 3 comments
Open
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@jfrancisco0
Copy link

Go version

go version go1.22.5 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/jfrancisco/Library/Caches/go-build'
GOENV='/Users/jfrancisco/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/jfrancisco/go/pkg/mod'
GONOPROXY='github.com/<redacted>/*'
GONOSUMDB='github.com/<redacted>/*'
GOOS='darwin'
GOPATH='/Users/jfrancisco/go'
GOPRIVATE='github.com/<redacted>/*'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.5/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.5/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.5'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/jfrancisco/golang/src/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/x9/7wxj63rn0f573d7zdjy3m63w0000gn/T/go-build2393148245=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

The gocritic linter suggests I should use http.NoBody instead of nil for empty body requests. However, httptest.NewRequest() shows different behaviour when http.NoBody is sent vs when nil is sent. Note that this is only the case for the httptest package, as http.NewRequest() - from the http package - works as expected.

See the example from https://go.dev/play/p/n9UxgUsAfM8

What did you see happen?

Creating a request with a http.NoBody body results in a ContentLength value of -1 instead of the expected 0 you would get with a nil body.

httptest.NewRequest("GET", "/", nil)
http.NewRequest("GET", "/", http.NoBody)

set ContentLength as 0, but

httptest.NewRequest("GET", "/", http.NoBody)

sets ContentLength as -1.

This makes my tests fail, as the server returns an error. According to the documentation, ContentLength: -1 means unknown, which should not be the case for http.NoBody, as that's and empty body (length 0). This happens because http.NoBody is a io.ReadCloser, which doesn't match any of the types in the switch.

What did you expect to see?

The request with a http.NoBody body should have ContentLength: 0.

The http package never sets a negative ContentLength for backwards compatibility (source).

I suggest that either the same logic is ported over to the httptest package, or that http.NoBody is accounted for as an exception, like it was in the http package before the reversal in this commit.

@cherrymui
Copy link
Member

As you already see, httptest.NewRequest is documented as

The provided body may be nil. If the body is of type *bytes.Reader, *strings.Reader, or *bytes.Buffer, the Request.ContentLength is set.

So it is clearly fine to pass a nil body. There is no need to change it.

Also, as http.NoBody is not any of those types, the ContentLength is "unset", which matches the documentation. Maybe we want to explicitly document it set to -1?

cc @neild

@cherrymui cherrymui added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Documentation labels Jul 19, 2024
@cherrymui cherrymui added this to the Backlog milestone Jul 19, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/599815 mentions this issue: net/http/httptest: match net/http ContentLength behavior for http.NoBody

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. FixPending Issues that have a fix which has not yet been reviewed or submitted. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Documentation labels Jul 22, 2024
@dmitshur dmitshur modified the milestones: Backlog, Go1.24 Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants