Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7cdebae

Browse files
author
Javier Provecho
committed
Merge branch 'bigwheel-topic-add-test-using-httptest' into develop
2 parents 288d1ae + 787aa6d commit 7cdebae

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

gin_integration_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/stretchr/testify/assert"
14+
"net/http/httptest"
1415
)
1516

1617
func testRequest(t *testing.T, url string) {
@@ -103,3 +104,28 @@ func TestBadUnixSocket(t *testing.T) {
103104
router := New()
104105
assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
105106
}
107+
108+
func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
109+
router := New()
110+
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
111+
112+
ts := httptest.NewServer(router)
113+
defer ts.Close()
114+
115+
testRequest(t, ts.URL+"/example")
116+
}
117+
118+
func TestWithHttptestWithSpecifiedPort(t *testing.T) {
119+
router := New()
120+
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
121+
122+
l, _ := net.Listen("tcp", ":8033")
123+
ts := httptest.Server{
124+
Listener: l,
125+
Config: &http.Server{Handler: router},
126+
}
127+
ts.Start()
128+
defer ts.Close()
129+
130+
testRequest(t, "http://localhost:8033/example")
131+
}

0 commit comments

Comments
 (0)