From 4c81c2a933738dc74934576edc3173eaa688c769 Mon Sep 17 00:00:00 2001 From: ljluestc Date: Sat, 29 Nov 2025 14:45:35 -0800 Subject: [PATCH] fix tests --- gin_integration_test.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gin_integration_test.go b/gin_integration_test.go index e040993a..eb2c0c5c 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -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) {