Skip to content

httptest: empty response (EOF) #46838

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

Closed
andig opened this issue Jun 20, 2021 · 3 comments
Closed

httptest: empty response (EOF) #46838

andig opened this issue Jun 20, 2021 · 3 comments

Comments

@andig
Copy link
Contributor

andig commented Jun 20, 2021

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

$ go version
go version go1.16.5 darwin/arm64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/andig/Library/Caches/go-build"
GOENV="/Users/andig/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/andig/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/andig/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.16.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.16.5/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/andig/htdocs/evcc/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sv/rs_453y57xj86xsbz3kw1mbc0000gn/T/go-build3556353379=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Testing an http client doing JSON request using httptest my test always failed.

This is the test:

func TestHttp(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Println(w, `{"list":[{"useMeter":true}]}`)
	}))
	defer ts.Close()

	resp, err := http.Get(ts.URL)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	t.Log(string(b))

	var res interface{}
	err = json.Unmarshal(b, &res)
	t.Log(err)
}

What did you expect to see?

Successful test, the JSON is valid.

What did you see instead?

unexpected end of JSON input

The error is from inside the json decoder. If I change the test to

var res interface{}
err = json.NewDecoder(resp.Body).Decode(&res)
t.Log(err)

the error changes to

EOF

Stepping through the original test I can see that the io.ReadAlls result is already empty which it probably shouldn't be?

@andig andig changed the title httptest: unexpected end of JSON input httptest: empty response (EOF) Jun 20, 2021
@andig
Copy link
Contributor Author

andig commented Jun 20, 2021

It does help to use Fprintln here...

@andig andig closed this as completed Jun 20, 2021
@pgarneau
Copy link

pgarneau commented Jan 6, 2022

So what did you end up doing? Getting a similar issue and causing one of my tests to be flaky.

@davecheney
Copy link
Contributor

@pgarneau please open a new issue

@golang golang locked and limited conversation to collaborators Jan 6, 2023
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

4 participants