-
Notifications
You must be signed in to change notification settings - Fork 125
HTTPClientRequest: allow custom TLS config #709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3525,4 +3525,49 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass { | |
let response = try client.get(url: self.defaultHTTPBinURLPrefix + "get").wait() | ||
XCTAssertEqual(.ok, response.status) | ||
} | ||
|
||
func testAsyncExecuteWithCustomTLS() async throws { | ||
let httpsBin = HTTPBin(.http1_1(ssl: true)) | ||
defer { | ||
XCTAssertNoThrow(try httpsBin.shutdown()) | ||
} | ||
|
||
// A client with default TLS settings, i.e. it won't accept `httpsBin`'s fake self-signed cert | ||
let client = HTTPClient(eventLoopGroup: MultiThreadedEventLoopGroup.singleton) | ||
defer { | ||
XCTAssertNoThrow(try client.shutdown().wait()) | ||
} | ||
|
||
var request = HTTPClientRequest(url: "https://localhost:\(httpsBin.port)/get") | ||
|
||
// For now, let's allow bad TLS certs | ||
request.tlsConfiguration = TLSConfiguration.clientDefault | ||
// ! is safe, assigned above | ||
request.tlsConfiguration!.certificateVerification = .none | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets get ride of the force unwrap here as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but that would hide issues. Force unwraps here are good because they'd catch that going wrong at the right place here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, Note that this shouldn't be an Optional property to begin with but that's the current API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, that would not be reasonable, that would be user hostile. If this breaks, AHC is broken, let's not hide this |
||
|
||
let response1 = try await client.execute(request, timeout: /* infinity */ .hours(99)) | ||
XCTAssertEqual(.ok, response1.status) | ||
|
||
// For the second request, we reset the TLS config | ||
request.tlsConfiguration = nil | ||
do { | ||
let response2 = try await client.execute(request, timeout: /* infinity */ .hours(99)) | ||
XCTFail("shouldn't succeed, self-signed cert: \(response2)") | ||
} catch { | ||
switch error as? NIOSSLError { | ||
case .some(.handshakeFailed(_)): | ||
() // ok | ||
default: | ||
XCTFail("unexpected error: \(error)") | ||
} | ||
} | ||
|
||
// And finally we allow it again. | ||
request.tlsConfiguration = TLSConfiguration.clientDefault | ||
// ! is safe, assigned above | ||
request.tlsConfiguration!.certificateVerification = .none | ||
|
||
let response3 = try await client.execute(request, timeout: /* infinity */ .hours(99)) | ||
XCTAssertEqual(.ok, response3.status) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.