@@ -646,10 +646,8 @@ class HTTPClientTests: XCTestCase {
646646 task. cancel ( )
647647}
648648
649- XCTAssertThrowsError ( try task. wait ( ) , " Should fail " ) { errorin
650- guard caselet error= erroras? HTTPClientError , error== . cancelledelse {
651- return XCTFail ( " Should fail with cancelled " )
652- }
649+ XCTAssertThrowsError ( try task. wait ( ) ) {
650+ XCTAssertEqual ( $0as? HTTPClientError , . cancelled)
653651}
654652}
655653
@@ -730,19 +728,23 @@ class HTTPClientTests: XCTestCase {
730728
731729func testProxyPlaintextWithIncorrectlyAuthorization( ) throws {
732730let 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+ )
738742defer {
739743XCTAssertNoThrow ( try localClient. syncShutdown ( ) )
740744XCTAssertNoThrow ( try localHTTPBin. shutdown ( ) )
741745}
742- XCTAssertThrowsError ( try localClient. get ( url: " http://test/ok " ) . wait ( ) , " Should fail " ) { errorin
743- guard caselet error= erroras? HTTPClientError , error== . proxyAuthenticationRequiredelse {
744- return XCTFail ( " Should fail with HTTPClientError.proxyAuthenticationRequired " )
745- }
746+ XCTAssertThrowsError ( try localClient. get ( url: " http://test/ok " ) . wait ( ) ) {
747+ XCTAssertEqual ( $0as? HTTPClientError , . proxyAuthenticationRequired)
746748}
747749}
748750
@@ -859,31 +861,41 @@ class HTTPClientTests: XCTestCase {
859861
860862func testLoopDetectionRedirectLimit( ) throws {
861863let 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
865872defer {
866873XCTAssertNoThrow ( try localClient. syncShutdown ( ) )
867874XCTAssertNoThrow ( 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
875882func testCountRedirectLimit( ) throws {
876883let 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
880892defer {
881893XCTAssertNoThrow ( try localClient. syncShutdown ( ) )
882894XCTAssertNoThrow ( 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
11071119func 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+
11081126let request = try Request ( url: " https://localhost: \( self . defaultHTTPBin. port) /wait " , method: . GET)
11091127let 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
11131131let results = try EventLoopFuture < TestHTTPDelegate . Response > . whenAllComplete ( tasks. map { $0. futureResult} , on: self . defaultClient. eventLoopGroup. next ( ) ) . wait ( )
@@ -1148,14 +1166,10 @@ class HTTPClientTests: XCTestCase {
11481166XCTAssertNoThrow ( try localClient. syncShutdown ( ) )
11491167XCTAssertNoThrow ( 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 isEventLoopFutureTimeoutError ) 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 isEventLoopFutureTimeoutError , " Timed out but should have failed immediately " )
11591173}
11601174}
11611175
@@ -1296,41 +1310,26 @@ class HTTPClientTests: XCTestCase {
12961310case . success:
12971311XCTFail ( " Shouldn't succeed " )
12981312case . failure( let error) :
1299- if let clientError= erroras? HTTPClientError , clientError== . cancelled{
1300- continue
1301- } else {
1302- XCTFail ( " Unexpected error: \( error) " )
1303- }
1313+ XCTAssertEqual ( erroras? HTTPClientError , . cancelled)
13041314}
13051315}
13061316}
13071317
13081318func testDoubleShutdown( ) {
13091319let client = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) )
13101320XCTAssertNoThrow ( try client. syncShutdown ( ) )
1311- do {
1312- try client. syncShutdown ( )
1313- XCTFail ( " Shutdown should fail with \( HTTPClientError . alreadyShutdown) " )
1314- } catch {
1315- guard let clientError= erroras? HTTPClientError , clientError== . alreadyShutdownelse {
1316- XCTFail ( " Unexpected error: \( error) instead of \( HTTPClientError . alreadyShutdown) " )
1317- return
1318- }
1321+
1322+ XCTAssertThrowsError ( try client. syncShutdown ( ) ) {
1323+ XCTAssertEqual ( $0as? HTTPClientError , . alreadyShutdown)
13191324}
13201325}
13211326
13221327func testTaskFailsWhenClientIsShutdown( ) {
13231328let client = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) )
13241329XCTAssertNoThrow ( 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= erroras? 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 ( $0as? HTTPClientError , . alreadyShutdown)
13341333}
13351334}
13361335
@@ -1712,25 +1711,28 @@ class HTTPClientTests: XCTestCase {
17121711}
17131712
17141713func 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 ( ) ) }
17201720for _ in 1 ... 500 {
17211721XCTAssertNoThrow ( try localClient. get ( url: self . defaultHTTPBinURLPrefix+ " get " ) . wait ( ) )
17221722Thread . sleep ( forTimeInterval: 0.01 + . random( in: - 0.05 ... 0.05 ) )
17231723}
17241724}
17251725
17261726func testAvoidLeakingTLSHandshakeCompletionPromise( ) {
1727- let localClient = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) , configuration: . init( timeout: . init( connect: . milliseconds( 100 ) ) ) )
17281727let localHTTPBin = HTTPBin ( )
17291728let port = localHTTPBin. port
17301729XCTAssertNoThrow ( 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
17351737XCTAssertThrowsError ( try localClient. get ( url: " http://localhost: \( port) " ) . wait ( ) ) { errorin
17361738if isTestingNIOTS ( ) {