mirror of
https://github.com/gin-gonic/gin.git
synced 2025-12-11 19:47:00 +08:00
fix tests
This commit is contained in:
parent
f6e887d460
commit
4c81c2a933
@ -16,6 +16,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@ -64,7 +65,16 @@ func testRequest(t *testing.T, params ...string) {
|
||||
}
|
||||
|
||||
func TestRunEmpty(t *testing.T) {
|
||||
os.Setenv("PORT", "")
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
require.NoError(t, err)
|
||||
l, err := net.ListenTCP("tcp", addr)
|
||||
require.NoError(t, err)
|
||||
port := strconv.Itoa(l.Addr().(*net.TCPAddr).Port)
|
||||
l.Close()
|
||||
|
||||
os.Setenv("PORT", port)
|
||||
defer os.Unsetenv("PORT")
|
||||
|
||||
router := New()
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
@ -74,8 +84,8 @@ func TestRunEmpty(t *testing.T) {
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
require.Error(t, router.Run(":8080"))
|
||||
testRequest(t, "http://localhost:8080/example")
|
||||
require.Error(t, router.Run(":"+port))
|
||||
testRequest(t, "http://localhost:"+port+"/example")
|
||||
}
|
||||
|
||||
func TestBadTrustedCIDRs(t *testing.T) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user