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
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@lrh3321
Copy link

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 commented Nov 10, 2020

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

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 18, 2020
@cagedmantis cagedmantis added this to the Backlog milestone Nov 18, 2020
@cagedmantis
Copy link
Contributor

/cc @bradfitz @empijei

@empijei
Copy link
Contributor

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
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants