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

gin.CreateTestContext is not working #2970

Open
paullaffitte opened this issue Nov 30, 2021 · 1 comment
Open

gin.CreateTestContext is not working #2970

paullaffitte opened this issue Nov 30, 2021 · 1 comment

Comments

@paullaffitte
Copy link

paullaffitte commented Nov 30, 2021

Description

When I try to use gin.CreateTestContext(), it create a context that cannot be used to serve http requests. Indeed, it takes a http.ResponseWriter as a parameter, which is incompatible with http.CloseNotifier, causing a panic.

How to reproduce

func Test(t *testing.T) {
	c, _ := gin.CreateTestContext(httptest.NewRecorder())

	request, err := http.NewRequest("GET", "/ping", nil)
	if err != nil {
		panic(err)
	}

	url, _ := url.Parse("http://localhost:8080")
	proxy := httputil.NewSingleHostReverseProxy(url)

	proxy.ServeHTTP(c.Writer, request)
}

Expectations

gin.CreateTestContext() should take as parameter an interface compatible with the http.CloseNotifier interface and should not produce a panic.

Actual result

The code panics.

--- FAIL: Test (0.00s)
panic: interface conversion: *httptest.ResponseRecorder is not http.CloseNotifier: missing method CloseNotify [recovered]
	panic: interface conversion: *httptest.ResponseRecorder is not http.CloseNotifier: missing method CloseNotify

goroutine 35 [running]:
testing.tRunner.func1.2(0xa79ba0, 0xc0003838f0)
	/home/linuxbrew/.linuxbrew/Cellar/go/1.16.6/libexec/src/testing/testing.go:1143 +0x332
testing.tRunner.func1(0xc0003c0300)
	/home/linuxbrew/.linuxbrew/Cellar/go/1.16.6/libexec/src/testing/testing.go:1146 +0x4b6
panic(0xa79ba0, 0xc0003838f0)
	/home/linuxbrew/.linuxbrew/Cellar/go/1.16.6/libexec/src/runtime/panic.go:965 +0x1b9
github.com/gin-gonic/gin.(*responseWriter).CloseNotify(0xc00040c300, 0xc000126010)
	/home/paul/go/pkg/mod/github.com/gin-gonic/gin@v1.7.3/response_writer.go:112 +0x45
net/http/httputil.(*ReverseProxy).ServeHTTP(0xc000398780, 0x7f853e3e8970, 0xc00040c300, 0xc00040c400)
	/home/linuxbrew/.linuxbrew/Cellar/go/1.16.6/libexec/src/net/http/httputil/reverseproxy.go:223 +0x1693
gitlab.enix.io/products/docker-cache-registry/internal/proxy.Test(0xc0003c0300)
	/home/paul/dev/internal/docker-cache-registry/internal/proxy/server_test.go:38 +0x1b1
testing.tRunner(0xc0003c0300, 0xb5bc48)
	/home/linuxbrew/.linuxbrew/Cellar/go/1.16.6/libexec/src/testing/testing.go:1193 +0xef
created by testing.(*T).Run
	/home/linuxbrew/.linuxbrew/Cellar/go/1.16.6/libexec/src/testing/testing.go:1238 +0x2b3
FAIL	gitlab.enix.io/products/docker-cache-registry/internal/proxy	0.007s
FAIL

Environment

  • go version: go1.16.6 linux/amd64
  • gin version (or commit ref): v1.7.3
  • operating system: ubuntu 20.04

Notes

The issue is coming from responseWriter.CloseNotify():

// CloseNotify implements the http.CloseNotify interface.
func (w *responseWriter) CloseNotify() <-chan bool {
	return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
}

w.ResponseWriter is casted without verification to http.CloseNotifier. However, the httptest.ResponseRecorder interface returned by gin.CreateTestContext() is incompatible with http.CloseNotifier.

@myzhan
Copy link

myzhan commented Oct 16, 2023

Yes, I met the same issue when using gin as a reverse proxy.

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

No branches or pull requests

2 participants