Files
async-http-client/Tests/AsyncHTTPClientTests/HTTPClientNIOTSTests.swift
T
5298f20331 Support NIO Transport Services - part 2 (#184)
This is the continuation of the good work of @Yasumoto and @weissi in #135

The following code adds support for NIO Transport services. When the ConnectionPool asks for a connection bootstrap it is returned a NIOClientTCPBootstrap which wraps either a NIOTSConnectionBootstrap or a ClientBootstrap depending on whether the EventLoop we are running on is NIOTSEventLoop.

If you initialize an HTTPClient with eventLoopGroupProvider set to .createNew then if you are running on iOS, macOS 10.14 or later it will provide a NIOTSEventLoopGroup instead of a EventLoopGroup.

Currently a number of tests are failing. 4 of these are related to the NIOSSLUncleanShutdown error the others all seem related to various race conditions which are being dealt with on other PRs. I have tested this code with aws-sdk-swift and it is working on both macOS and iOS.

Things look into:

The aws-sdk-swift NIOTS HTTP client had issues with on Mojave. We should check if this is the case for async-http-client as well.

Co-authored-by: Joe Smith <yasumoto7@gmail.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-04-18 14:17:46 +01:00

116 lines
4.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2018-2019 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@testable import AsyncHTTPClient
#if canImport(Network)
import Network
#endif
import NIO
import NIOSSL
import NIOTransportServices
import XCTest
class HTTPClientNIOTSTests: XCTestCase {
var clientGroup: EventLoopGroup!
override func setUp() {
XCTAssertNil(self.clientGroup)
self.clientGroup = getDefaultEventLoopGroup(numberOfThreads: 3)
}
override func tearDown() {
XCTAssertNotNil(self.clientGroup)
XCTAssertNoThrow(try self.clientGroup.syncShutdownGracefully())
self.clientGroup = nil
}
func testCorrectEventLoopGroup() {
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
defer {
XCTAssertNoThrow(try httpClient.syncShutdown())
}
#if canImport(Network)
if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
XCTAssertTrue(httpClient.eventLoopGroup is NIOTSEventLoopGroup)
return
}
#endif
XCTAssertTrue(httpClient.eventLoopGroup is MultiThreadedEventLoopGroup)
}
func testTLSFailError() {
guard isTestingNIOTS() else { return }
#if canImport(Network)
let httpBin = HTTPBin(ssl: true)
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
defer {
XCTAssertNoThrow(try httpClient.syncShutdown(requiresCleanClose: true))
XCTAssertNoThrow(try httpBin.shutdown())
}
do {
_ = try httpClient.get(url: "https://localhost:\(httpBin.port)/get").wait()
XCTFail("This should have failed")
} catch let error as HTTPClient.NWTLSError {
XCTAssertEqual(error.status, errSSLHandshakeFail)
} catch {
XCTFail("Error should have been NWTLSError not \(type(of: error))")
}
#endif
}
func testConnectionFailError() {
guard isTestingNIOTS() else { return }
let httpBin = HTTPBin(ssl: true)
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
defer {
XCTAssertNoThrow(try httpClient.syncShutdown(requiresCleanClose: true))
}
let port = httpBin.port
XCTAssertNoThrow(try httpBin.shutdown())
do {
_ = try httpClient.get(url: "https://localhost:\(port)/get").wait()
XCTFail("This should have failed")
} catch ChannelError.connectTimeout {
} catch {
XCTFail("Error should have been ChannelError.connectTimeout not \(type(of: error))")
}
}
func testTLSVersionError() {
guard isTestingNIOTS() else { return }
#if canImport(Network)
let httpBin = HTTPBin(ssl: true)
let httpClient = HTTPClient(
eventLoopGroupProvider: .shared(self.clientGroup),
configuration: .init(tlsConfiguration: TLSConfiguration.forClient(minimumTLSVersion: .tlsv11, maximumTLSVersion: .tlsv1, certificateVerification: .none))
)
defer {
XCTAssertNoThrow(try httpClient.syncShutdown(requiresCleanClose: true))
XCTAssertNoThrow(try httpBin.shutdown())
}
do {
_ = try httpClient.get(url: "https://localhost:\(httpBin.port)/get").wait()
XCTFail("This should have failed")
} catch let error as HTTPClient.NWTLSError {
XCTAssertEqual(error.status, errSSLHandshakeFail)
} catch {
XCTFail("Error should have been NWTLSError not \(type(of: error))")
}
#endif
}
}