@@ -17,6 +17,7 @@ limitations under the License.
17
17
package portforward
18
18
19
19
import (
20
+ "fmt"
20
21
"net"
21
22
"net/http"
22
23
"os"
@@ -309,12 +310,13 @@ func TestGetListener(t *testing.T) {
309
310
}
310
311
311
312
for i , testCase := range testCases {
312
- expectedListenerPort := " 12345"
313
- listener , err := pf .getListener (testCase .Protocol , testCase .Hostname , & ForwardedPort { 12345 , 12345 } )
313
+ forwardedPort := & ForwardedPort { Local : 0 , Remote : 12345 }
314
+ listener , err := pf .getListener (testCase .Protocol , testCase .Hostname , forwardedPort )
314
315
if err != nil && strings .Contains (err .Error (), "cannot assign requested address" ) {
315
316
t .Logf ("Can't test #%d: %v" , i , err )
316
317
continue
317
318
}
319
+ expectedListenerPort := fmt .Sprintf ("%d" , forwardedPort .Local )
318
320
errorRaised := err != nil
319
321
320
322
if testCase .ShouldRaiseError != errorRaised {
@@ -331,7 +333,7 @@ func TestGetListener(t *testing.T) {
331
333
}
332
334
333
335
host , port , _ := net .SplitHostPort (listener .Addr ().String ())
334
- t .Logf ("Asked a %s forward for: %s:%v , got listener %s:%s, expected: %s" , testCase .Protocol , testCase .Hostname , 12345 , host , port , expectedListenerPort )
336
+ t .Logf ("Asked a %s forward for: %s:0 , got listener %s:%s, expected: %s" , testCase .Protocol , testCase .Hostname , host , port , expectedListenerPort )
335
337
if host != testCase .ExpectedListenerAddress {
336
338
t .Errorf ("Test case #%d failed: Listener does not listen on expected address: asked '%v' got '%v'" , i , testCase .ExpectedListenerAddress , host )
337
339
}
@@ -340,7 +342,6 @@ func TestGetListener(t *testing.T) {
340
342
341
343
}
342
344
listener .Close ()
343
-
344
345
}
345
346
}
346
347
0 commit comments