Skip to content

Commit a076303

Browse files
authored
Reorganise tests (AsyncHttpClient#51)
* Split out @internal tests to separate testsuite * Rename test classes and filenames
1 parent 34e3403 commit a076303

8 files changed

+258
-198
lines changed

Tests/LinuxMain.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import XCTest
2626
@testable import NIOHTTPClientTests
2727

2828
XCTMain([
29-
testCase(HTTPCookieTests.allTests),
30-
testCase(SwiftHTTPTests.allTests),
29+
testCase(HTTPClientCookieTests.allTests),
30+
testCase(HTTPClientInternalTests.allTests),
31+
testCase(HTTPClientTests.allTests),
3132
])
3233
#endif

Tests/NIOHTTPClientTests/HTTPCookieTests+XCTest.swift renamed to Tests/NIOHTTPClientTests/HTTPClientCookieTests+XCTest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414
//
15-
// HTTPCookieTests+XCTest.swift
15+
// HTTPClientCookieTests+XCTest.swift
1616
//
1717
import XCTest
1818

@@ -22,8 +22,8 @@ import XCTest
2222
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
2323
///
2424

25-
extension HTTPCookieTests {
26-
static var allTests: [(String, (HTTPCookieTests) -> () throws -> Void)] {
25+
extension HTTPClientCookieTests {
26+
static var allTests: [(String, (HTTPClientCookieTests) -> () throws -> Void)] {
2727
return [
2828
("testCookie", testCookie),
2929
("testCookieDefaults", testCookieDefaults),

Tests/NIOHTTPClientTests/HTTPCookieTests.swift renamed to Tests/NIOHTTPClientTests/HTTPClientCookieTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import Foundation
16-
@testable import NIOHTTPClient
16+
import NIOHTTPClient
1717
import XCTest
1818

19-
class HTTPCookieTests: XCTestCase {
19+
class HTTPClientCookieTests: XCTestCase {
2020
func testCookie() {
2121
let v = "key=value; Path=/path; Domain=example.com; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Max-Age=42; Secure; HttpOnly"
2222
let c = HTTPClient.Cookie(from: v, defaultDomain: "exampe.org")!
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIOHTTPClient open source project
4+
//
5+
// Copyright (c) 2018-2019 Swift Server Working Group and the SwiftNIOHTTPClient 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 SwiftNIOHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
//
15+
// HTTPClientInternalTests+XCTest.swift
16+
//
17+
import XCTest
18+
19+
///
20+
/// NOTE: This file was generated by generate_linux_tests.rb
21+
///
22+
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
23+
///
24+
25+
extension HTTPClientInternalTests {
26+
static var allTests: [(String, (HTTPClientInternalTests) -> () throws -> Void)] {
27+
return [
28+
("testHTTPPartsHandler", testHTTPPartsHandler),
29+
("testHTTPPartsHandlerMultiBody", testHTTPPartsHandlerMultiBody),
30+
("testProxyStreaming", testProxyStreaming),
31+
("testProxyStreamingFailure", testProxyStreamingFailure),
32+
("testUploadStreamingBackpressure", testUploadStreamingBackpressure),
33+
]
34+
}
35+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIOHTTPClient open source project
4+
//
5+
// Copyright (c) 2018-2019 Swift Server Working Group and the SwiftNIOHTTPClient 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 SwiftNIOHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import NIO
16+
import NIOConcurrencyHelpers
17+
import NIOHTTP1
18+
@testable import NIOHTTPClient
19+
import XCTest
20+
21+
class HTTPClientInternalTests: XCTestCase {
22+
typealias Request = HTTPClient.Request
23+
typealias Task = HTTPClient.Task
24+
25+
func testHTTPPartsHandler() throws {
26+
let channel = EmbeddedChannel()
27+
let recorder = RecordingHandler<HTTPClientResponsePart, HTTPClientRequestPart>()
28+
let task = Task<Void>(eventLoop: channel.eventLoop)
29+
30+
try channel.pipeline.addHandler(recorder).wait()
31+
try channel.pipeline.addHandler(TaskHandler(task: task, delegate: TestHTTPDelegate(), redirectHandler: nil)).wait()
32+
33+
var request = try Request(url: "http://localhost/get")
34+
request.headers.add(name: "X-Test-Header", value: "X-Test-Value")
35+
request.body = .string("1234")
36+
37+
XCTAssertNoThrow(try channel.writeOutbound(request))
38+
XCTAssertEqual(3, recorder.writes.count)
39+
var head = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), method: .GET, uri: "/get")
40+
head.headers.add(name: "X-Test-Header", value: "X-Test-Value")
41+
head.headers.add(name: "Host", value: "localhost")
42+
head.headers.add(name: "Content-Length", value: "4")
43+
head.headers.add(name: "Connection", value: "close")
44+
XCTAssertEqual(HTTPClientRequestPart.head(head), recorder.writes[0])
45+
let buffer = ByteBuffer.of(string: "1234")
46+
XCTAssertEqual(HTTPClientRequestPart.body(.byteBuffer(buffer)), recorder.writes[1])
47+
48+
XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.head(HTTPResponseHead(version: HTTPVersion(major: 1, minor: 1), status: HTTPResponseStatus.ok))))
49+
XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.end(nil)))
50+
}
51+
52+
func testHTTPPartsHandlerMultiBody() throws {
53+
let channel = EmbeddedChannel()
54+
let delegate = TestHTTPDelegate()
55+
let task = Task<Void>(eventLoop: channel.eventLoop)
56+
let handler = TaskHandler(task: task, delegate: delegate, redirectHandler: nil)
57+
58+
try channel.pipeline.addHandler(handler).wait()
59+
60+
handler.state = .sent
61+
var body = channel.allocator.buffer(capacity: 4)
62+
body.writeStaticString("1234")
63+
64+
XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.head(HTTPResponseHead(version: HTTPVersion(major: 1, minor: 1), status: HTTPResponseStatus.ok))))
65+
XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.body(body)))
66+
XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.body(body)))
67+
68+
switch delegate.state {
69+
case .body(_, let body):
70+
XCTAssertEqual(8, body.readableBytes)
71+
default:
72+
XCTFail("Expecting .body")
73+
}
74+
}
75+
76+
func testProxyStreaming() throws {
77+
let httpBin = HttpBin()
78+
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
79+
defer {
80+
try! httpClient.syncShutdown()
81+
httpBin.shutdown()
82+
}
83+
84+
let body: HTTPClient.Body = .stream(length: 50) { writer in
85+
do {
86+
var request = try Request(url: "http://localhost:\(httpBin.port)/events/10/1")
87+
request.headers.add(name: "Accept", value: "text/event-stream")
88+
89+
let delegate = CopyingDelegate { part in
90+
writer.write(.byteBuffer(part))
91+
}
92+
return httpClient.execute(request: request, delegate: delegate).futureResult
93+
} catch {
94+
return httpClient.eventLoopGroup.next().makeFailedFuture(error)
95+
}
96+
}
97+
98+
let upload = try! httpClient.post(url: "http://localhost:\(httpBin.port)/post", body: body).wait()
99+
let bytes = upload.body.flatMap { $0.getData(at: 0, length: $0.readableBytes) }
100+
let data = try! JSONDecoder().decode(RequestInfo.self, from: bytes!)
101+
102+
XCTAssertEqual(.ok, upload.status)
103+
XCTAssertEqual("id: 0id: 1id: 2id: 3id: 4id: 5id: 6id: 7id: 8id: 9", data.data)
104+
}
105+
106+
func testProxyStreamingFailure() throws {
107+
let httpBin = HttpBin()
108+
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
109+
defer {
110+
try! httpClient.syncShutdown()
111+
httpBin.shutdown()
112+
}
113+
114+
var body: HTTPClient.Body = .stream(length: 50) { _ in
115+
httpClient.eventLoopGroup.next().makeFailedFuture(HTTPClientError.invalidProxyResponse)
116+
}
117+
118+
XCTAssertThrowsError(try httpClient.post(url: "http://localhost:\(httpBin.port)/post", body: body).wait())
119+
120+
body = .stream(length: 50) { _ in
121+
do {
122+
var request = try Request(url: "http://localhost:\(httpBin.port)/events/10/1")
123+
request.headers.add(name: "Accept", value: "text/event-stream")
124+
125+
let delegate = CopyingDelegate { _ in
126+
httpClient.eventLoopGroup.next().makeFailedFuture(HTTPClientError.invalidProxyResponse)
127+
}
128+
return httpClient.execute(request: request, delegate: delegate).futureResult
129+
} catch {
130+
return httpClient.eventLoopGroup.next().makeFailedFuture(error)
131+
}
132+
}
133+
134+
XCTAssertThrowsError(try httpClient.post(url: "http://localhost:\(httpBin.port)/post", body: body).wait())
135+
}
136+
137+
func testUploadStreamingBackpressure() throws {
138+
class BackpressureTestDelegate: HTTPClientResponseDelegate {
139+
typealias Response = Void
140+
141+
var _reads = 0
142+
let lock: Lock
143+
let promise: EventLoopPromise<Void>
144+
145+
init(promise: EventLoopPromise<Void>) {
146+
self.lock = Lock()
147+
self.promise = promise
148+
}
149+
150+
var reads: Int {
151+
return self.lock.withLock {
152+
self._reads
153+
}
154+
}
155+
156+
func didReceivePart(task: HTTPClient.Task<Response>, _ buffer: ByteBuffer) -> EventLoopFuture<Void> {
157+
self.lock.withLockVoid {
158+
self._reads += 1
159+
}
160+
return self.promise.futureResult
161+
}
162+
163+
func didFinishRequest(task: HTTPClient.Task<Response>) throws {}
164+
}
165+
166+
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
167+
let promise: EventLoopPromise<Channel> = httpClient.eventLoopGroup.next().makePromise()
168+
let httpBin = HttpBin(channelPromise: promise)
169+
170+
defer {
171+
try! httpClient.syncShutdown()
172+
httpBin.shutdown()
173+
}
174+
175+
let request = try Request(url: "http://localhost:\(httpBin.port)/custom")
176+
let delegate = BackpressureTestDelegate(promise: httpClient.eventLoopGroup.next().makePromise())
177+
let future = httpClient.execute(request: request, delegate: delegate).futureResult
178+
179+
let channel = try promise.futureResult.wait()
180+
181+
// Send 3 parts, but only one should be received until the future is complete
182+
let buffer = ByteBuffer.of(string: "1234")
183+
try channel.writeAndFlush(HTTPServerResponsePart.body(.byteBuffer(buffer))).wait()
184+
try channel.writeAndFlush(HTTPServerResponsePart.body(.byteBuffer(buffer))).wait()
185+
try channel.writeAndFlush(HTTPServerResponsePart.body(.byteBuffer(buffer))).wait()
186+
187+
XCTAssertEqual(delegate.reads, 1)
188+
189+
delegate.promise.succeed(())
190+
191+
try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait()
192+
try future.wait()
193+
194+
XCTAssertEqual(delegate.reads, 3)
195+
}
196+
}
197+

Tests/NIOHTTPClientTests/HTTPClientTestUtils.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
import Foundation
1616
import NIO
1717
import NIOHTTP1
18-
@testable import NIOHTTPClient
18+
import NIOHTTPClient
1919
import NIOSSL
2020

2121
class TestHTTPDelegate: HTTPClientResponseDelegate {
2222
typealias Response = Void
2323

24-
var state = ResponseAccumulator.State.idle
24+
enum State {
25+
case idle
26+
case head(HTTPResponseHead)
27+
case body(HTTPResponseHead, ByteBuffer)
28+
case end
29+
case error(Error)
30+
}
31+
32+
var state = State.idle
2533

2634
func didReceiveHead(task: HTTPClient.Task<Response>, _ head: HTTPResponseHead) -> EventLoopFuture<Void> {
2735
self.state = .head(head)

Tests/NIOHTTPClientTests/SwiftNIOHTTPTests+XCTest.swift renamed to Tests/NIOHTTPClientTests/HTTPClientTests+XCTest.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414
//
15-
// SwiftNIOHTTPTests+XCTest.swift
15+
// HTTPClientTests+XCTest.swift
1616
//
1717
import XCTest
1818

@@ -22,12 +22,10 @@ import XCTest
2222
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
2323
///
2424

25-
extension SwiftHTTPTests {
26-
static var allTests: [(String, (SwiftHTTPTests) -> () throws -> Void)] {
25+
extension HTTPClientTests {
26+
static var allTests: [(String, (HTTPClientTests) -> () throws -> Void)] {
2727
return [
2828
("testRequestURI", testRequestURI),
29-
("testHTTPPartsHandler", testHTTPPartsHandler),
30-
("testHTTPPartsHandlerMultiBody", testHTTPPartsHandlerMultiBody),
3129
("testGet", testGet),
3230
("testPost", testPost),
3331
("testGetHttps", testGetHttps),
@@ -42,9 +40,6 @@ extension SwiftHTTPTests {
4240
("testProxyPlaintext", testProxyPlaintext),
4341
("testProxyTLS", testProxyTLS),
4442
("testUploadStreaming", testUploadStreaming),
45-
("testProxyStreaming", testProxyStreaming),
46-
("testProxyStreamingFailure", testProxyStreamingFailure),
47-
("testUploadStreamingBackpressure", testUploadStreamingBackpressure),
4843
]
4944
}
5045
}

0 commit comments

Comments
 (0)