|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the AsyncHTTPClient open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2018-2019 Apple Inc. and the AsyncHTTPClient project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#if canImport(Network) |
| 16 | + |
| 17 | +@testable import AsyncHTTPClient |
| 18 | +import Network |
| 19 | +import NIO |
| 20 | +import NIOSSL |
| 21 | +import NIOTransportServices |
| 22 | +import XCTest |
| 23 | + |
| 24 | +class HTTPClientNIOTSTests: XCTestCase { |
| 25 | + var clientGroup: EventLoopGroup! |
| 26 | + |
| 27 | + override func setUp() { |
| 28 | + XCTAssertNil(self.clientGroup) |
| 29 | + self.clientGroup = getDefaultEventLoopGroup(numberOfThreads: 3) |
| 30 | + } |
| 31 | + |
| 32 | + override func tearDown() { |
| 33 | + XCTAssertNotNil(self.clientGroup) |
| 34 | + XCTAssertNoThrow(try self.clientGroup.syncShutdownGracefully()) |
| 35 | + self.clientGroup = nil |
| 36 | + } |
| 37 | + |
| 38 | + func testCorrectEventLoopGroup() { |
| 39 | + let httpClient = HTTPClient(eventLoopGroupProvider: .createNew) |
| 40 | + defer { |
| 41 | + XCTAssertNoThrow(try httpClient.syncShutdown()) |
| 42 | + } |
| 43 | + if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) { |
| 44 | + XCTAssertTrue(httpClient.eventLoopGroup is NIOTSEventLoopGroup) |
| 45 | + return |
| 46 | + } |
| 47 | + XCTAssertTrue(httpClient.eventLoopGroup is MultiThreadedEventLoopGroup) |
| 48 | + } |
| 49 | + |
| 50 | + func testDNSFailError() { |
| 51 | + guard isTestingNIOTS() else { return } |
| 52 | + let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup)) |
| 53 | + defer { |
| 54 | + XCTAssertNoThrow(try httpClient.syncShutdown(requiresCleanClose: true)) |
| 55 | + } |
| 56 | + |
| 57 | + do { |
| 58 | + _ = try httpClient.get(url: "http://dnsfail/").wait() |
| 59 | + XCTFail("This should have failed") |
| 60 | + } catch let error as NWDNSError { |
| 61 | + XCTAssertEqual(error.errorType, DNSServiceErrorType(kDNSServiceErr_NoSuchRecord)) |
| 62 | + } catch { |
| 63 | + XCTFail("Error should have been NWDSNError not \(type(of:error))") |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + func testTLSFailError() { |
| 68 | + guard isTestingNIOTS() else { return } |
| 69 | + let httpBin = HTTPBin(ssl: true) |
| 70 | + let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup)) |
| 71 | + defer { |
| 72 | + XCTAssertNoThrow(try httpClient.syncShutdown(requiresCleanClose: true)) |
| 73 | + XCTAssertNoThrow(try httpBin.shutdown()) |
| 74 | + } |
| 75 | + |
| 76 | + do { |
| 77 | + _ = try httpClient.get(url: "https://localhost:\(httpBin.port)/get").wait() |
| 78 | + XCTFail("This should have failed") |
| 79 | + } catch let error as NWTLSError { |
| 80 | + XCTAssertEqual(error.status, errSSLHandshakeFail) |
| 81 | + } catch { |
| 82 | + XCTFail("Error should have been NWTLSError not \(type(of:error))") |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + func testConnectionFailError() { |
| 87 | + guard isTestingNIOTS() else { return } |
| 88 | + let httpBin = HTTPBin(ssl: true) |
| 89 | + let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup)) |
| 90 | + defer { |
| 91 | + XCTAssertNoThrow(try httpClient.syncShutdown(requiresCleanClose: true)) |
| 92 | + } |
| 93 | + let port = httpBin.port |
| 94 | + XCTAssertNoThrow(try httpBin.shutdown()) |
| 95 | + |
| 96 | + do { |
| 97 | + _ = try httpClient.get(url: "https://localhost:\(port)/get").wait() |
| 98 | + XCTFail("This should have failed") |
| 99 | + } catch let error as NWPOSIXError { |
| 100 | + XCTAssertEqual(error.errorCode, .ECONNREFUSED) |
| 101 | + } catch { |
| 102 | + XCTFail("Error should have been NWPOSIXError not \(type(of:error))") |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + func testTLSVersionError() { |
| 107 | + guard isTestingNIOTS() else { return } |
| 108 | + let httpBin = HTTPBin(ssl: true) |
| 109 | + let httpClient = HTTPClient( |
| 110 | + eventLoopGroupProvider: .shared(self.clientGroup), |
| 111 | + configuration: .init(tlsConfiguration: TLSConfiguration.forClient(minimumTLSVersion: .tlsv11, maximumTLSVersion: .tlsv1, certificateVerification: .none)) |
| 112 | + ) |
| 113 | + defer { |
| 114 | + XCTAssertNoThrow(try httpClient.syncShutdown(requiresCleanClose: true)) |
| 115 | + XCTAssertNoThrow(try httpBin.shutdown()) |
| 116 | + } |
| 117 | + |
| 118 | + do { |
| 119 | + _ = try httpClient.get(url: "https://localhost:\(httpBin.port)/get").wait() |
| 120 | + XCTFail("This should have failed") |
| 121 | + } catch let error as NWTLSError { |
| 122 | + XCTAssertEqual(error.status, errSSLHandshakeFail) |
| 123 | + } catch { |
| 124 | + XCTFail("Error should have been NWTLSError not \(type(of:error))") |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#endif |
0 commit comments