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/httputil: DumpResponse return error when ContentLength=2 with Body length 0, but it's a response for http.MethodHead request #42478

Open
lrh3321 opened this issue Nov 10, 2020 · 3 comments
Milestone

Comments

@lrh3321
Copy link

@lrh3321 lrh3321 commented Nov 10, 2020

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

$ go version
go version go1.15.4 linux/amd64

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="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY="*.ucloudadmin.com"
GONOSUMDB="*.ucloudadmin.com"
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE="*.ucloudadmin.com"
GOPROXY="https://goproxy.cn,direct"
GOROOT="/root/sdk/go1.15.4"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/root/sdk/go1.15.4/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build737726766=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

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

func serveHTTP(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Content-Length", "2")
	switch r.Method {
	case http.MethodHead:
	default:
		w.Write([]byte("{}"))
	}
}

func TestDumpResponse(t *testing.T) {
	t.Run("Get", func(t *testing.T) {
		rw := httptest.NewRecorder()
		r := httptest.NewRequest(http.MethodGet, "/", nil)

		serveHTTP(rw, r)
		raw, err := httputil.DumpResponse(rw.Result(), true)
		if err != nil {
			t.Errorf("DumpResponse: (%v)\nRaw: %s", err, string(raw))
		}

	})
	t.Run("Head", func(t *testing.T) {
		rw := httptest.NewRecorder()
		r := httptest.NewRequest(http.MethodHead, "/", nil)

		serveHTTP(rw, r)
		raw, err := httputil.DumpResponse(rw.Result(), true)
		if err != nil {
			t.Errorf("DumpResponse: (%v)\nRaw: %s", err, string(raw))
		}

	})
}
go test main_test.go 

What did you expect to see?

--- FAIL: TestDumpResponse (0.00s)
    --- FAIL: TestDumpResponse/Head (0.00s)
        main_test.go:38: DumpResponse: (http: ContentLength=2 with Body length 0)
            Raw: 
FAIL
FAIL    command-line-arguments  0.003s
FAIL

What did you see instead?

ok      command-line-arguments  0.003s
@imxyb
Copy link
Contributor

@imxyb imxyb commented Nov 10, 2020

i think you may change httputil.DumpResponse(rw.Result(), true) to httputil.DumpResponse(rw.Result(), false)

@cagedmantis cagedmantis added this to the Backlog milestone Nov 18, 2020
@cagedmantis
Copy link
Contributor

@cagedmantis cagedmantis commented Nov 18, 2020

@empijei
Copy link
Contributor

@empijei empijei commented Nov 23, 2020

It's not clear to me what you're trying to convey.

You wrote

What did you expect to see?

--- FAIL: TestDumpResponse (0.00s)
    --- FAIL: TestDumpResponse/Head (0.00s)
        main_test.go:38: DumpResponse: (http: ContentLength=2 with Body length 0)
            Raw: 
FAIL
FAIL    command-line-arguments  0.003s
FAIL

What did you see instead?

ok      command-line-arguments  0.003s

But if I run the tests myself I obtain the exact opposite result. I guess you meant to say that you expected "ok" and got the error instead.

Assuming my guess is right, your bug is saying that DumpResponse is erroring on an invalid response (CL mismatch with body) and you didn't expect it to?

Why did you expect httputil to work on inconsistent responses?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.