@@ -3525,4 +3525,49 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
3525
3525
let response = try client. get ( url: self . defaultHTTPBinURLPrefix + " get " ) . wait ( )
3526
3526
XCTAssertEqual ( . ok, response. status)
3527
3527
}
3528
+
3529
+ func testAsyncExecuteWithCustomTLS( ) async throws {
3530
+ let httpsBin = HTTPBin ( . http1_1( ssl: true ) )
3531
+ defer {
3532
+ XCTAssertNoThrow ( try httpsBin. shutdown ( ) )
3533
+ }
3534
+
3535
+ // A client with default TLS settings, i.e. it won't accept `httpsBin`'s fake self-signed cert
3536
+ let client = HTTPClient ( eventLoopGroup: MultiThreadedEventLoopGroup . singleton)
3537
+ defer {
3538
+ XCTAssertNoThrow ( try client. shutdown ( ) . wait ( ) )
3539
+ }
3540
+
3541
+ var request = HTTPClientRequest ( url: " https://localhost: \( httpsBin. port) /get " )
3542
+
3543
+ // For now, let's allow bad TLS certs
3544
+ request. tlsConfiguration = TLSConfiguration . clientDefault
3545
+ // ! is safe, assigned above
3546
+ request. tlsConfiguration!. certificateVerification = . none
3547
+
3548
+ let response1 = try await client. execute ( request, timeout: /* infinity */ . hours( 99 ) )
3549
+ XCTAssertEqual ( . ok, response1. status)
3550
+
3551
+ // For the second request, we reset the TLS config
3552
+ request. tlsConfiguration = nil
3553
+ do {
3554
+ let response2 = try await client. execute ( request, timeout: /* infinity */ . hours( 99 ) )
3555
+ XCTFail ( " shouldn't succeed, self-signed cert: \( response2) " )
3556
+ } catch {
3557
+ switch error as? NIOSSLError {
3558
+ case . some( . handshakeFailed( _) ) :
3559
+ ( ) // ok
3560
+ default :
3561
+ XCTFail ( " unexpected error: \( error) " )
3562
+ }
3563
+ }
3564
+
3565
+ // And finally we allow it again.
3566
+ request. tlsConfiguration = TLSConfiguration . clientDefault
3567
+ // ! is safe, assigned above
3568
+ request. tlsConfiguration!. certificateVerification = . none
3569
+
3570
+ let response3 = try await client. execute ( request, timeout: /* infinity */ . hours( 99 ) )
3571
+ XCTAssertEqual ( . ok, response3. status)
3572
+ }
3528
3573
}
0 commit comments