@@ -646,10 +646,8 @@ class HTTPClientTests: XCTestCase {
646
646
task. cancel ( )
647
647
}
648
648
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)
653
651
}
654
652
}
655
653
@@ -730,19 +728,23 @@ class HTTPClientTests: XCTestCase {
730
728
731
729
func testProxyPlaintextWithIncorrectlyAuthorization( ) throws {
732
730
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
+ )
738
742
defer {
739
743
XCTAssertNoThrow ( try localClient. syncShutdown ( ) )
740
744
XCTAssertNoThrow ( try localHTTPBin. shutdown ( ) )
741
745
}
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)
746
748
}
747
749
}
748
750
@@ -859,31 +861,41 @@ class HTTPClientTests: XCTestCase {
859
861
860
862
func testLoopDetectionRedirectLimit( ) throws {
861
863
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
+ )
864
871
865
872
defer {
866
873
XCTAssertNoThrow ( try localClient. syncShutdown ( ) )
867
874
XCTAssertNoThrow ( try localHTTPBin. shutdown ( ) )
868
875
}
869
876
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)
872
879
}
873
880
}
874
881
875
882
func testCountRedirectLimit( ) throws {
876
883
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
+ )
879
891
880
892
defer {
881
893
XCTAssertNoThrow ( try localClient. syncShutdown ( ) )
882
894
XCTAssertNoThrow ( try localHTTPBin. shutdown ( ) )
883
895
}
884
896
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)
887
899
}
888
900
}
889
901
@@ -1105,9 +1117,15 @@ class HTTPClientTests: XCTestCase {
1105
1117
}
1106
1118
1107
1119
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
+
1108
1126
let request = try Request ( url: " https://localhost: \( self . defaultHTTPBin. port) /wait " , method: . GET)
1109
1127
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 ( ) )
1111
1129
}
1112
1130
1113
1131
let results = try EventLoopFuture < TestHTTPDelegate . Response > . whenAllComplete ( tasks. map { $0. futureResult } , on: self . defaultClient. eventLoopGroup. next ( ) ) . wait ( )
@@ -1148,14 +1166,10 @@ class HTTPClientTests: XCTestCase {
1148
1166
XCTAssertNoThrow ( try localClient. syncShutdown ( ) )
1149
1167
XCTAssertNoThrow ( try localHTTPBin. shutdown ( ) )
1150
1168
}
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 " )
1159
1173
}
1160
1174
}
1161
1175
@@ -1296,41 +1310,26 @@ class HTTPClientTests: XCTestCase {
1296
1310
case . success:
1297
1311
XCTFail ( " Shouldn't succeed " )
1298
1312
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)
1304
1314
}
1305
1315
}
1306
1316
}
1307
1317
1308
1318
func testDoubleShutdown( ) {
1309
1319
let client = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) )
1310
1320
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)
1319
1324
}
1320
1325
}
1321
1326
1322
1327
func testTaskFailsWhenClientIsShutdown( ) {
1323
1328
let client = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) )
1324
1329
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)
1334
1333
}
1335
1334
}
1336
1335
@@ -1712,25 +1711,28 @@ class HTTPClientTests: XCTestCase {
1712
1711
}
1713
1712
1714
1713
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 ( ) ) }
1720
1720
for _ in 1 ... 500 {
1721
1721
XCTAssertNoThrow ( try localClient. get ( url: self . defaultHTTPBinURLPrefix + " get " ) . wait ( ) )
1722
1722
Thread . sleep ( forTimeInterval: 0.01 + . random( in: - 0.05 ... 0.05 ) )
1723
1723
}
1724
1724
}
1725
1725
1726
1726
func testAvoidLeakingTLSHandshakeCompletionPromise( ) {
1727
- let localClient = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) , configuration: . init( timeout: . init( connect: . milliseconds( 100 ) ) ) )
1728
1727
let localHTTPBin = HTTPBin ( )
1729
1728
let port = localHTTPBin. port
1730
1729
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 ( ) ) }
1734
1736
1735
1737
XCTAssertThrowsError ( try localClient. get ( url: " http://localhost: \( port) " ) . wait ( ) ) { error in
1736
1738
if isTestingNIOTS ( ) {
0 commit comments