Skip to content

Commit 83fb548

Browse files
committed
Added NIOTS specific tests
1 parent c1c97ce commit 83fb548

File tree

3 files changed

+130
-17
lines changed

3 files changed

+130
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ extension HTTPClientTests {
9696
("testPoolClosesIdleConnections", testPoolClosesIdleConnections),
9797
("testRacePoolIdleConnectionsAndGet", testRacePoolIdleConnectionsAndGet),
9898
("testAvoidLeakingTLSHandshakeCompletionPromise", testAvoidLeakingTLSHandshakeCompletionPromise),
99-
("testAsyncShutdown", testAsyncShutdown),
100-
("testCorrectEventLoopGroup", testCorrectEventLoopGroup)
99+
("testAsyncShutdown", testAsyncShutdown)
101100
]
102101
}
103102
}

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

-15
Original file line numberDiff line numberDiff line change
@@ -1698,19 +1698,4 @@ class HTTPClientTests: XCTestCase {
16981698
}
16991699
XCTAssertNoThrow(try promise.futureResult.wait())
17001700
}
1701-
1702-
1703-
func testCorrectEventLoopGroup() {
1704-
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
1705-
defer {
1706-
XCTAssertNoThrow(try httpClient.syncShutdown())
1707-
}
1708-
#if canImport(Network)
1709-
if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
1710-
XCTAssertTrue(httpClient.eventLoopGroup is NIOTSEventLoopGroup)
1711-
return
1712-
}
1713-
#endif
1714-
XCTAssertTrue(httpClient.eventLoopGroup is MultiThreadedEventLoopGroup)
1715-
}
17161701
}

0 commit comments

Comments
 (0)