From 79840bc1c62d7d6104e2b1c5d39099f92f9f8d11 Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Mon, 30 Sep 2019 09:12:22 +0800 Subject: [PATCH] support run HTTP server with specific net.Listener (#2023) --- gin.go | 9 +++++++++ gin_integration_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/gin.go b/gin.go index cbdd080e..894cf094 100644 --- a/gin.go +++ b/gin.go @@ -338,6 +338,15 @@ func (engine *Engine) RunFd(fd int) (err error) { return } defer listener.Close() + err = engine.RunListener(listener) + return +} + +// RunListener attaches the router to a http.Server and starts listening and serving HTTP requests +// through the specified net.Listener +func (engine *Engine) RunListener(listener net.Listener) (err error) { + debugPrint("Listening and serving HTTP on listener what's bind with address@%s", listener.Addr()) + defer func() { debugPrintError(err) }() err = http.Serve(listener, engine) return } diff --git a/gin_integration_test.go b/gin_integration_test.go index 9beec14d..7e270b91 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -207,6 +207,42 @@ func TestBadFileDescriptor(t *testing.T) { assert.Error(t, router.RunFd(0)) } +func TestListener(t *testing.T) { + router := New() + addr, err := net.ResolveTCPAddr("tcp", "localhost:0") + assert.NoError(t, err) + listener, err := net.ListenTCP("tcp", addr) + assert.NoError(t, err) + go func() { + router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) + assert.NoError(t, router.RunListener(listener)) + }() + // have to wait for the goroutine to start and run the server + // otherwise the main thread will complete + time.Sleep(5 * time.Millisecond) + + c, err := net.Dial("tcp", listener.Addr().String()) + assert.NoError(t, err) + + fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n") + scanner := bufio.NewScanner(c) + var response string + for scanner.Scan() { + response += scanner.Text() + } + assert.Contains(t, response, "HTTP/1.0 200", "should get a 200") + assert.Contains(t, response, "it worked", "resp body should match") +} + +func TestBadListener(t *testing.T) { + router := New() + addr, err := net.ResolveTCPAddr("tcp", "localhost:10086") + assert.NoError(t, err) + listener, err := net.ListenTCP("tcp", addr) + listener.Close() + assert.Error(t, router.RunListener(listener)) +} + func TestWithHttptestWithAutoSelectedPort(t *testing.T) { router := New() router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })