@@ -646,10 +646,8 @@ class HTTPClientTests: XCTestCase {
646646 task.cancel()
647647 }
648648
649- XCTAssertThrowsError(try task.wait(), "Should fail") { error in
650- guard case let error = error as? HTTPClientError, error == .cancelled else {
651- return XCTFail("Should fail with cancelled")
652- }
649+ XCTAssertThrowsError(try task.wait()) {
650+ XCTAssertEqual($0 as? HTTPClientError, .cancelled)
653651 }
654652 }
655653
@@ -730,19 +728,23 @@ class HTTPClientTests: XCTestCase {
730728
731729 func testProxyPlaintextWithIncorrectlyAuthorization() throws {
732730 let localHTTPBin = HTTPBin(proxy: .simulate(authorization: "Basic YWxhZGRpbjpvcGVuc2VzYW1l"))
733- let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
734- configuration: .init(proxy: .server(host: "localhost",
735- port: localHTTPBin.port,
736- authorization: .basic(username: "aladdin",
737- password: "opensesamefoo"))))
731+ let localClient = HTTPClient(
732+ eventLoopGroupProvider: .shared(self.clientGroup),
733+ configuration: .init(
734+ timeout: .init(connect: .seconds(2), read: nil),
735+ proxy: .server(
736+ host: "localhost",
737+ port: localHTTPBin.port,
738+ authorization: .basic(username: "aladdin", password: "opensesamefoo")
739+ )
740+ )
741+ )
738742 defer {
739743 XCTAssertNoThrow(try localClient.syncShutdown())
740744 XCTAssertNoThrow(try localHTTPBin.shutdown())
741745 }
742- XCTAssertThrowsError(try localClient.get(url: "http://test/ok").wait(), "Should fail") { error in
743- guard case let error = error as? HTTPClientError, error == .proxyAuthenticationRequired else {
744- return XCTFail("Should fail with HTTPClientError.proxyAuthenticationRequired")
745- }
746+ XCTAssertThrowsError(try localClient.get(url: "http://test/ok").wait()) {
747+ XCTAssertEqual($0 as? HTTPClientError, .proxyAuthenticationRequired)
746748 }
747749 }
748750
@@ -859,31 +861,41 @@ class HTTPClientTests: XCTestCase {
859861
860862 func testLoopDetectionRedirectLimit() throws {
861863 let localHTTPBin = HTTPBin(.http1_1(ssl: true))
862- let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
863- configuration: HTTPClient.Configuration(certificateVerification: .none, redirectConfiguration: .follow(max: 5, allowCycles: false)))
864+ let localClient = HTTPClient(
865+ eventLoopGroupProvider: .shared(self.clientGroup),
866+ configuration: .init(
867+ certificateVerification: .none,
868+ redirectConfiguration: .follow(max: 5, allowCycles: false)
869+ )
870+ )
864871
865872 defer {
866873 XCTAssertNoThrow(try localClient.syncShutdown())
867874 XCTAssertNoThrow(try localHTTPBin.shutdown())
868875 }
869876
870- XCTAssertThrowsError(try localClient.get(url: "https://localhost:\(localHTTPBin.port)/redirect/infinite1").wait(), "Should fail with redirect limit" ) { error in
871- XCTAssertEqual(error as? HTTPClientError, HTTPClientError.redirectCycleDetected)
877+ XCTAssertThrowsError(try localClient.get(url: "https://localhost:\(localHTTPBin.port)/redirect/infinite1").wait()) {
878+ XCTAssertEqual($0 as? HTTPClientError, HTTPClientError.redirectCycleDetected)
872879 }
873880 }
874881
875882 func testCountRedirectLimit() throws {
876883 let localHTTPBin = HTTPBin(.http1_1(ssl: true))
877- let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
878- configuration: HTTPClient.Configuration(certificateVerification: .none, redirectConfiguration: .follow(max: 10, allowCycles: true)))
884+ let localClient = HTTPClient(
885+ eventLoopGroupProvider: .shared(self.clientGroup),
886+ configuration: .init(
887+ certificateVerification: .none,
888+ redirectConfiguration: .follow(max: 10, allowCycles: true)
889+ )
890+ )
879891
880892 defer {
881893 XCTAssertNoThrow(try localClient.syncShutdown())
882894 XCTAssertNoThrow(try localHTTPBin.shutdown())
883895 }
884896
885- XCTAssertThrowsError(try localClient.get(url: "https://localhost:\(localHTTPBin.port)/redirect/infinite1").wait(), "Should fail with redirect limit" ) { error in
886- XCTAssertEqual(error as? HTTPClientError, HTTPClientError.redirectLimitReached)
897+ XCTAssertThrowsError(try localClient.get(url: "https://localhost:\(localHTTPBin.port)/redirect/infinite1").wait()) {
898+ XCTAssertEqual($0 as? HTTPClientError, HTTPClientError.redirectLimitReached)
887899 }
888900 }
889901
@@ -1105,9 +1117,15 @@ class HTTPClientTests: XCTestCase {
11051117 }
11061118
11071119 func testStressGetHttpsSSLError() throws {
1120+ let localClient = HTTPClient(
1121+ eventLoopGroupProvider: .createNew,
1122+ configuration: .init(timeout: .init(connect: .seconds(2), read: nil))
1123+ )
1124+ defer { XCTAssertNoThrow(try localClient.syncShutdown()) }
1125+
11081126 let request = try Request(url: "https://localhost:\(self.defaultHTTPBin.port)/wait", method: .GET)
11091127 let tasks = (1...100).map { _ -> HTTPClient.Task<TestHTTPDelegate.Response> in
1110- self.defaultClient .execute(request: request, delegate: TestHTTPDelegate())
1128+ localClient .execute(request: request, delegate: TestHTTPDelegate())
11111129 }
11121130
11131131 let results = try EventLoopFuture<TestHTTPDelegate.Response>.whenAllComplete(tasks.map { $0.futureResult }, on: self.defaultClient.eventLoopGroup.next()).wait()
@@ -1148,14 +1166,10 @@ class HTTPClientTests: XCTestCase {
11481166 XCTAssertNoThrow(try localClient.syncShutdown())
11491167 XCTAssertNoThrow(try localHTTPBin.shutdown())
11501168 }
1151- do {
1152- _ = try localClient.get(url: "http://localhost:\(localHTTPBin.port)/get").timeout(after: .seconds(5)).wait()
1153- XCTFail("Shouldn't succeed")
1154- } catch {
1155- guard !(error is EventLoopFutureTimeoutError) else {
1156- XCTFail("Timed out but should have failed immediately")
1157- return
1158- }
1169+
1170+ let future = localClient.get(url: "http://localhost:\(localHTTPBin.port)/get").timeout(after: .seconds(5))
1171+ XCTAssertThrowsError(try future.wait()) {
1172+ XCTAssertFalse($0 is EventLoopFutureTimeoutError, "Timed out but should have failed immediately")
11591173 }
11601174 }
11611175
@@ -1296,41 +1310,26 @@ class HTTPClientTests: XCTestCase {
12961310 case .success:
12971311 XCTFail("Shouldn't succeed")
12981312 case .failure(let error):
1299- if let clientError = error as? HTTPClientError, clientError == .cancelled {
1300- continue
1301- } else {
1302- XCTFail("Unexpected error: \(error)")
1303- }
1313+ XCTAssertEqual(error as? HTTPClientError, .cancelled)
13041314 }
13051315 }
13061316 }
13071317
13081318 func testDoubleShutdown() {
13091319 let client = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
13101320 XCTAssertNoThrow(try client.syncShutdown())
1311- do {
1312- try client.syncShutdown()
1313- XCTFail("Shutdown should fail with \(HTTPClientError.alreadyShutdown)")
1314- } catch {
1315- guard let clientError = error as? HTTPClientError, clientError == .alreadyShutdown else {
1316- XCTFail("Unexpected error: \(error) instead of \(HTTPClientError.alreadyShutdown)")
1317- return
1318- }
1321+
1322+ XCTAssertThrowsError(try client.syncShutdown()) {
1323+ XCTAssertEqual($0 as? HTTPClientError, .alreadyShutdown)
13191324 }
13201325 }
13211326
13221327 func testTaskFailsWhenClientIsShutdown() {
13231328 let client = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
13241329 XCTAssertNoThrow(try client.syncShutdown())
1325- do {
1326- _ = try client.get(url: "http://localhost/").wait()
1327- XCTFail("Request shouldn't succeed")
1328- } catch {
1329- if let error = error as? HTTPClientError, error == .alreadyShutdown {
1330- return
1331- } else {
1332- XCTFail("Unexpected error: \(error)")
1333- }
1330+
1331+ XCTAssertThrowsError(try client.get(url: "http://localhost/").wait()) {
1332+ XCTAssertEqual($0 as? HTTPClientError, .alreadyShutdown)
13341333 }
13351334 }
13361335
@@ -1712,25 +1711,28 @@ class HTTPClientTests: XCTestCase {
17121711 }
17131712
17141713 func testRacePoolIdleConnectionsAndGet() {
1715- let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
1716- configuration: .init(connectionPool: .init(idleTimeout: .milliseconds(10))))
1717- defer {
1718- XCTAssertNoThrow(try localClient.syncShutdown())
1719- }
1714+ let localClient = HTTPClient(
1715+ eventLoopGroupProvider: .shared(self.clientGroup),
1716+ configuration: .init(connectionPool: .init(idleTimeout: .milliseconds(10)))
1717+ )
1718+
1719+ defer { XCTAssertNoThrow(try localClient.syncShutdown()) }
17201720 for _ in 1...500 {
17211721 XCTAssertNoThrow(try localClient.get(url: self.defaultHTTPBinURLPrefix + "get").wait())
17221722 Thread.sleep(forTimeInterval: 0.01 + .random(in: -0.05...0.05))
17231723 }
17241724 }
17251725
17261726 func testAvoidLeakingTLSHandshakeCompletionPromise() {
1727- let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), configuration: .init(timeout: .init(connect: .milliseconds(100))))
17281727 let localHTTPBin = HTTPBin()
17291728 let port = localHTTPBin.port
17301729 XCTAssertNoThrow(try localHTTPBin.shutdown())
1731- defer {
1732- XCTAssertNoThrow(try localClient.syncShutdown())
1733- }
1730+
1731+ let localClient = HTTPClient(
1732+ eventLoopGroupProvider: .shared(self.clientGroup),
1733+ configuration: .init(timeout: .init(connect: .milliseconds(100)))
1734+ )
1735+ defer { XCTAssertNoThrow(try localClient.syncShutdown()) }
17341736
17351737 XCTAssertThrowsError(try localClient.get(url: "http://localhost:\(port)").wait()) { error in
17361738 if isTestingNIOTS() {
0 commit comments