From bc3952abad8e1a5b8e8aaf3abb7f58a505746535 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 25 Nov 2021 19:40:20 +0100 Subject: [PATCH 1/2] bind to a port defined by the operating system --- Tests/AsyncHTTPClientTests/HTTPClient+SOCKSTests.swift | 4 ++-- Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Tests/AsyncHTTPClientTests/HTTPClient+SOCKSTests.swift b/Tests/AsyncHTTPClientTests/HTTPClient+SOCKSTests.swift index f0692de1f..5fdc5ac61 100644 --- a/Tests/AsyncHTTPClientTests/HTTPClient+SOCKSTests.swift +++ b/Tests/AsyncHTTPClientTests/HTTPClient+SOCKSTests.swift @@ -76,7 +76,7 @@ class HTTPClientSOCKSTests: XCTestCase { func testProxySOCKS() throws { let socksBin = try MockSOCKSServer(expectedURL: "/socks/test", expectedResponse: "it works!") let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), - configuration: .init(proxy: .socksServer(host: "localhost"))) + configuration: .init(proxy: .socksServer(host: "localhost", port: socksBin.port))) defer { XCTAssertNoThrow(try localClient.syncShutdown()) @@ -125,7 +125,7 @@ class HTTPClientSOCKSTests: XCTestCase { func testProxySOCKSMisbehavingServer() throws { let socksBin = try MockSOCKSServer(expectedURL: "/socks/test", expectedResponse: "it works!", misbehave: true) let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), - configuration: .init(proxy: .socksServer(host: "localhost"))) + configuration: .init(proxy: .socksServer(host: "localhost", port: socksBin.port))) defer { XCTAssertNoThrow(try localClient.syncShutdown()) diff --git a/Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift b/Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift index fb8ee449a..611751532 100644 --- a/Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift +++ b/Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift @@ -35,7 +35,10 @@ class TestSOCKSBadServerHandler: ChannelInboundHandler { class MockSOCKSServer { let channel: Channel - + + var port: Int { + channel.localAddress!.port! + } init(expectedURL: String, expectedResponse: String, misbehave: Bool = false, file: String = #file, line: UInt = #line) throws { let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1) let bootstrap: ServerBootstrap @@ -57,7 +60,7 @@ class MockSOCKSServer { ]) } } - self.channel = try bootstrap.bind(host: "localhost", port: 1080).wait() + self.channel = try bootstrap.bind(host: "localhost", port: 0).wait() } func shutdown() throws { From 2b54600475d8edbdfd5dd83a01be97a5e36b9037 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 25 Nov 2021 19:48:03 +0100 Subject: [PATCH 2/2] run SwiftFormat --- Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift b/Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift index 611751532..d7c97e6fe 100644 --- a/Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift +++ b/Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift @@ -35,10 +35,11 @@ class TestSOCKSBadServerHandler: ChannelInboundHandler { class MockSOCKSServer { let channel: Channel - + var port: Int { - channel.localAddress!.port! + self.channel.localAddress!.port! } + init(expectedURL: String, expectedResponse: String, misbehave: Bool = false, file: String = #file, line: UInt = #line) throws { let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1) let bootstrap: ServerBootstrap