From b512566acfe0b4ed3496f30a8801b951edb4ea9b Mon Sep 17 00:00:00 2001 From: Kevin Mulvey Date: Thu, 28 May 2015 21:28:24 -0400 Subject: [PATCH 1/3] unix socket integration test --- gin_integration_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 gin_integration_test.go diff --git a/gin_integration_test.go b/gin_integration_test.go new file mode 100644 index 0000000..47d1e10 --- /dev/null +++ b/gin_integration_test.go @@ -0,0 +1,40 @@ +package gin + +import ( + "bufio" + "bytes" + "fmt" + "net" + "net/http" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestUnixSocket(t *testing.T) { + buffer := new(bytes.Buffer) + router := New() + go func() { + router.Use(LoggerWithWriter(buffer)) + router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) + router.RunUnix("/tmp/unix_unit_test") + }() + // 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("unix", "/tmp/unix_unit_test") + if err != nil { + println(err) + t.FailNow() + } + 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") +} From da5be64c9f1db8b3a3d7ffa9c566cd1ad521f626 Mon Sep 17 00:00:00 2001 From: Kevin Mulvey Date: Fri, 29 May 2015 07:29:33 -0400 Subject: [PATCH 2/3] test standard router --- gin_integration_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/gin_integration_test.go b/gin_integration_test.go index 47d1e10..1ec764f 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "io/ioutil" "net" "net/http" "testing" @@ -12,6 +13,31 @@ import ( "github.com/stretchr/testify/assert" ) +func TestRun(t *testing.T) { + buffer := new(bytes.Buffer) + router := New() + go func() { + router.Use(LoggerWithWriter(buffer)) + router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) + router.Run(":5150") + }() + // have to wait for the goroutine to start and run the server + // otherwise the main thread will complete + time.Sleep(5 * time.Millisecond) + + resp, err := http.Get("http://localhost:5150/example") + if err != nil { + t.FailNow() + } + defer resp.Body.Close() + body, ioerr := ioutil.ReadAll(resp.Body) + if ioerr != nil { + t.FailNow() + } + assert.Equal(t, "it worked", body, "resp body should match") + assert.Equal(t, "200 OK", resp.Status, "should get a 200") +} + func TestUnixSocket(t *testing.T) { buffer := new(bytes.Buffer) router := New() From b235d14ca18ddd0f00e010362ef71b695b213fd1 Mon Sep 17 00:00:00 2001 From: Kevin Mulvey Date: Tue, 2 Jun 2015 20:52:33 -0400 Subject: [PATCH 3/3] compare the byte array --- gin_integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gin_integration_test.go b/gin_integration_test.go index 1ec764f..cb12164 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -34,7 +34,7 @@ func TestRun(t *testing.T) { if ioerr != nil { t.FailNow() } - assert.Equal(t, "it worked", body, "resp body should match") + assert.Equal(t, "it worked", string(body[:]), "resp body should match") assert.Equal(t, "200 OK", resp.Status, "should get a 200") }