Merge branch 'topic-add-test-using-httptest' of https://github.com/bigwheel/gin into bigwheel-topic-add-test-using-httptest

This commit is contained in:
Javier Provecho 2016-03-05 23:31:31 +01:00
commit 787aa6d170
1 changed files with 26 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"net/http/httptest"
) )
func testRequest(t *testing.T, url string) { func testRequest(t *testing.T, url string) {
@ -103,3 +104,28 @@ func TestBadUnixSocket(t *testing.T) {
router := New() router := New()
assert.Error(t, router.RunUnix("#/tmp/unix_unit_test")) assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
} }
func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
router := New()
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
ts := httptest.NewServer(router)
defer ts.Close()
testRequest(t, ts.URL+"/example")
}
func TestWithHttptestWithSpecifiedPort(t *testing.T) {
router := New()
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
l, _ := net.Listen("tcp", ":8033")
ts := httptest.Server{
Listener: l,
Config: &http.Server{Handler: router},
}
ts.Start()
defer ts.Close()
testRequest(t, "http://localhost:8033/example")
}