fix tests

This commit is contained in:
ljluestc 2025-11-29 14:45:35 -08:00
parent f6e887d460
commit 4c81c2a933

View File

@ -16,6 +16,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"sync" "sync"
"testing" "testing"
@ -64,7 +65,16 @@ func testRequest(t *testing.T, params ...string) {
} }
func TestRunEmpty(t *testing.T) { 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() router := New()
go func() { go func() {
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 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 // otherwise the main thread will complete
time.Sleep(5 * time.Millisecond) time.Sleep(5 * time.Millisecond)
require.Error(t, router.Run(":8080")) require.Error(t, router.Run(":"+port))
testRequest(t, "http://localhost:8080/example") testRequest(t, "http://localhost:"+port+"/example")
} }
func TestBadTrustedCIDRs(t *testing.T) { func TestBadTrustedCIDRs(t *testing.T) {