From 756029a1b0de8e17376052c366aec44b00408b06 Mon Sep 17 00:00:00 2001 From: Mark Ellzey Date: Wed, 25 Oct 2023 12:07:07 -0400 Subject: [PATCH] Don't use static listener ports for unit tests Unit tests fail if anything on the system is bound to port 9000; instead we bind to port 0 and allow the system to choose. --- url/rawparam_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/url/rawparam_test.go b/url/rawparam_test.go index dc4a498..2198229 100644 --- a/url/rawparam_test.go +++ b/url/rawparam_test.go @@ -2,6 +2,7 @@ package urlutil import ( "fmt" + "net" "net/http" "net/url" "testing" @@ -52,7 +53,10 @@ func TestParamIntegration(t *testing.T) { w.WriteHeader(http.StatusOK) }) //nolint:all - go http.ListenAndServe(":9000", nil) + listener, err := net.Listen("tcp", ":0") + require.Nil(t, err) + + go http.Serve(listener, nil) p := NewParams() p.Add("sqli", "1+AND+(SELECT+*+FROM+(SELECT(SLEEP(12)))nQIP)") @@ -60,9 +64,9 @@ func TestParamIntegration(t *testing.T) { p.Add("xssiwthspace", "") p.Add("jsprotocol", "javascript://alert(1)") - url, _ := url.Parse("http://localhost:9000/params") + url, _ := url.Parse("http://" + listener.Addr().String() + "/params") url.RawQuery = p.Encode() - _, err := http.Get(url.String()) + _, err = http.Get(url.String()) require.Nil(t, err) require.Nil(t, routerErr) }