Skip to content

Commit 2787693

Browse files
committed
Add h2 stream integration tests
1 parent 1119893 commit 2787693

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let package = Package(
2323
dependencies: [
2424
.package(url: "https://github.com/apple/swift-nio.git", from: "2.34.0"),
2525
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.14.1"),
26-
.package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.18.2"),
26+
.package(url: "https://github.com/apple/swift-nio-http2.git", .branch("main")),
2727
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.10.0"),
2828
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.11.0"),
2929
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),

Tests/AsyncHTTPClientTests/HTTP2ClientTests+XCTest.swift

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extension HTTP2ClientTests {
2626
static var allTests: [(String, (HTTP2ClientTests) -> () throws -> Void)] {
2727
return [
2828
("testSimpleGet", testSimpleGet),
29+
("testStreamRequestBodyWithoutKnowledgeAboutLength", testStreamRequestBodyWithoutKnowledgeAboutLength),
30+
("testStreamRequestBodyWithFalseKnowledgeAboutLength", testStreamRequestBodyWithFalseKnowledgeAboutLength),
2931
("testConcurrentRequests", testConcurrentRequests),
3032
("testConcurrentRequestsFromDifferentThreads", testConcurrentRequestsFromDifferentThreads),
3133
("testConcurrentRequestsWorkWithRequiredEventLoop", testConcurrentRequestsWorkWithRequiredEventLoop),

Tests/AsyncHTTPClientTests/HTTP2ClientTests.swift

+32
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@ class HTTP2ClientTests: XCTestCase {
6262
XCTAssertEqual(response?.version, .http2)
6363
}
6464

65+
func testStreamRequestBodyWithoutKnowledgeAboutLength() {
66+
let bin = HTTPBin(.http2(compress: false)) { _ in HTTPEchoHandler() }
67+
defer { XCTAssertNoThrow(try bin.shutdown()) }
68+
let client = self.makeDefaultHTTPClient()
69+
defer { XCTAssertNoThrow(try client.syncShutdown()) }
70+
var response: HTTPClient.Response?
71+
let body = HTTPClient.Body.stream(length: nil) { writer in
72+
writer.write(.byteBuffer(ByteBuffer(integer: UInt64(0)))).flatMap {
73+
writer.write(.byteBuffer(ByteBuffer(integer: UInt64(0))))
74+
}
75+
}
76+
XCTAssertNoThrow(response = try client.post(url: "https://localhost:\(bin.port)", body: body).wait())
77+
78+
XCTAssertEqual(.ok, response?.status)
79+
XCTAssertEqual(response?.version, .http2)
80+
}
81+
82+
func testStreamRequestBodyWithFalseKnowledgeAboutLength() {
83+
let bin = HTTPBin(.http2(compress: false)) { _ in HTTPEchoHandler() }
84+
defer { XCTAssertNoThrow(try bin.shutdown()) }
85+
let client = self.makeDefaultHTTPClient()
86+
defer { XCTAssertNoThrow(try client.syncShutdown()) }
87+
let body = HTTPClient.Body.stream(length: 12) { writer in
88+
writer.write(.byteBuffer(ByteBuffer(integer: UInt64(0)))).flatMap {
89+
writer.write(.byteBuffer(ByteBuffer(integer: UInt64(0))))
90+
}
91+
}
92+
XCTAssertThrowsError(try client.post(url: "https://localhost:\(bin.port)", body: body).wait()) {
93+
XCTAssertEqual($0 as? HTTPClientError, .bodyLengthMismatch)
94+
}
95+
}
96+
6597
func testConcurrentRequests() {
6698
let bin = HTTPBin(.http2(compress: false))
6799
defer { XCTAssertNoThrow(try bin.shutdown()) }

Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -1218,13 +1218,14 @@ class HTTPEchoHandler: ChannelInboundHandler {
12181218
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
12191219
let request = self.unwrapInboundIn(data)
12201220
switch request {
1221-
case .head:
1222-
context.writeAndFlush(self.wrapOutboundOut(.head(.init(version: .http1_1, status: .ok))), promise: nil)
1221+
case .head(let requestHead):
1222+
context.writeAndFlush(self.wrapOutboundOut(.head(.init(version: .http1_1, status: .ok, headers: requestHead.headers))), promise: nil)
12231223
case .body(let bytes):
12241224
context.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(bytes))), promise: nil)
12251225
case .end:
1226-
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
1227-
context.close(promise: nil)
1226+
context.writeAndFlush(self.wrapOutboundOut(.end(nil))).whenSuccess {
1227+
context.close(promise: nil)
1228+
}
12281229
}
12291230
}
12301231
}

0 commit comments

Comments
 (0)