From 888548954d44d0b5fc4d48cc683a83155c2b10ee Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Tue, 30 Nov 2021 11:29:13 +0100 Subject: [PATCH] Add h2 stream integration tests --- Package.swift | 2 +- .../HTTP2ClientTests+XCTest.swift | 2 ++ .../HTTP2ClientTests.swift | 32 +++++++++++++++++++ .../HTTPClientTestUtils.swift | 9 +++--- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index 15c4bdb2c..b96f4df0d 100644 --- a/Package.swift +++ b/Package.swift @@ -23,7 +23,7 @@ let package = Package( dependencies: [ .package(url: "https://github.com/apple/swift-nio.git", from: "2.34.0"), .package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.14.1"), - .package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.18.2"), + .package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.19.0"), .package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.10.0"), .package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.11.0"), .package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"), diff --git a/Tests/AsyncHTTPClientTests/HTTP2ClientTests+XCTest.swift b/Tests/AsyncHTTPClientTests/HTTP2ClientTests+XCTest.swift index dff1c8a12..b74b66820 100644 --- a/Tests/AsyncHTTPClientTests/HTTP2ClientTests+XCTest.swift +++ b/Tests/AsyncHTTPClientTests/HTTP2ClientTests+XCTest.swift @@ -26,6 +26,8 @@ extension HTTP2ClientTests { static var allTests: [(String, (HTTP2ClientTests) -> () throws -> Void)] { return [ ("testSimpleGet", testSimpleGet), + ("testStreamRequestBodyWithoutKnowledgeAboutLength", testStreamRequestBodyWithoutKnowledgeAboutLength), + ("testStreamRequestBodyWithFalseKnowledgeAboutLength", testStreamRequestBodyWithFalseKnowledgeAboutLength), ("testConcurrentRequests", testConcurrentRequests), ("testConcurrentRequestsFromDifferentThreads", testConcurrentRequestsFromDifferentThreads), ("testConcurrentRequestsWorkWithRequiredEventLoop", testConcurrentRequestsWorkWithRequiredEventLoop), diff --git a/Tests/AsyncHTTPClientTests/HTTP2ClientTests.swift b/Tests/AsyncHTTPClientTests/HTTP2ClientTests.swift index 5bc4ce19e..0269dafa7 100644 --- a/Tests/AsyncHTTPClientTests/HTTP2ClientTests.swift +++ b/Tests/AsyncHTTPClientTests/HTTP2ClientTests.swift @@ -62,6 +62,38 @@ class HTTP2ClientTests: XCTestCase { XCTAssertEqual(response?.version, .http2) } + func testStreamRequestBodyWithoutKnowledgeAboutLength() { + let bin = HTTPBin(.http2(compress: false)) { _ in HTTPEchoHandler() } + defer { XCTAssertNoThrow(try bin.shutdown()) } + let client = self.makeDefaultHTTPClient() + defer { XCTAssertNoThrow(try client.syncShutdown()) } + var response: HTTPClient.Response? + let body = HTTPClient.Body.stream(length: nil) { writer in + writer.write(.byteBuffer(ByteBuffer(integer: UInt64(0)))).flatMap { + writer.write(.byteBuffer(ByteBuffer(integer: UInt64(0)))) + } + } + XCTAssertNoThrow(response = try client.post(url: "https://localhost:\(bin.port)", body: body).wait()) + + XCTAssertEqual(.ok, response?.status) + XCTAssertEqual(response?.version, .http2) + } + + func testStreamRequestBodyWithFalseKnowledgeAboutLength() { + let bin = HTTPBin(.http2(compress: false)) { _ in HTTPEchoHandler() } + defer { XCTAssertNoThrow(try bin.shutdown()) } + let client = self.makeDefaultHTTPClient() + defer { XCTAssertNoThrow(try client.syncShutdown()) } + let body = HTTPClient.Body.stream(length: 12) { writer in + writer.write(.byteBuffer(ByteBuffer(integer: UInt64(0)))).flatMap { + writer.write(.byteBuffer(ByteBuffer(integer: UInt64(0)))) + } + } + XCTAssertThrowsError(try client.post(url: "https://localhost:\(bin.port)", body: body).wait()) { + XCTAssertEqual($0 as? HTTPClientError, .bodyLengthMismatch) + } + } + func testConcurrentRequests() { let bin = HTTPBin(.http2(compress: false)) defer { XCTAssertNoThrow(try bin.shutdown()) } diff --git a/Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift b/Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift index 406f8a8ab..e71490bd1 100644 --- a/Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift +++ b/Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift @@ -1218,13 +1218,14 @@ class HTTPEchoHandler: ChannelInboundHandler { func channelRead(context: ChannelHandlerContext, data: NIOAny) { let request = self.unwrapInboundIn(data) switch request { - case .head: - context.writeAndFlush(self.wrapOutboundOut(.head(.init(version: .http1_1, status: .ok))), promise: nil) + case .head(let requestHead): + context.writeAndFlush(self.wrapOutboundOut(.head(.init(version: .http1_1, status: .ok, headers: requestHead.headers))), promise: nil) case .body(let bytes): context.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(bytes))), promise: nil) case .end: - context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) - context.close(promise: nil) + context.writeAndFlush(self.wrapOutboundOut(.end(nil))).whenSuccess { + context.close(promise: nil) + } } } }