From 3c0766502f21ea4532506658b9af9fb700bfe353 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 23 Mar 2023 15:10:04 +0000 Subject: [PATCH] Use async version of HBRequestBody.consumeBody, adding tests (#185) * Use async version of HBRequestBody.consumeBody Also add tests * Add async routergroup test --- Package.swift | 2 +- .../AsyncAwaitSupport/Router+async.swift | 5 +-- Tests/HummingbirdTests/AsyncAwaitTests.swift | 45 +++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index 6d54c954..a278647e 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( .package(url: "https://github.com/apple/swift-metrics.git", "1.0.0"..<"3.0.0"), .package(url: "https://github.com/apple/swift-nio.git", from: "2.45.0"), .package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "1.0.0-alpha.9"), - .package(url: "https://github.com/hummingbird-project/hummingbird-core.git", from: "1.2.0"), + .package(url: "https://github.com/hummingbird-project/hummingbird-core.git", from: "1.2.1"), ], targets: [ .target(name: "Hummingbird", dependencies: [ diff --git a/Sources/Hummingbird/AsyncAwaitSupport/Router+async.swift b/Sources/Hummingbird/AsyncAwaitSupport/Router+async.swift index d0a1e77a..84368219 100644 --- a/Sources/Hummingbird/AsyncAwaitSupport/Router+async.swift +++ b/Sources/Hummingbird/AsyncAwaitSupport/Router+async.swift @@ -80,9 +80,8 @@ extension HBRouterMethods { var request = request if case .stream = request.body, !options.contains(.streamBody) { let buffer = try await request.body.consumeBody( - maxSize: request.application.configuration.maxUploadSize, - on: request.eventLoop - ).get() + maxSize: request.application.configuration.maxUploadSize + ) request.body = .byteBuffer(buffer) } if options.contains(.editResponse) { diff --git a/Tests/HummingbirdTests/AsyncAwaitTests.swift b/Tests/HummingbirdTests/AsyncAwaitTests.swift index 6b3a70d3..7b6cdd25 100644 --- a/Tests/HummingbirdTests/AsyncAwaitTests.swift +++ b/Tests/HummingbirdTests/AsyncAwaitTests.swift @@ -52,6 +52,26 @@ final class AsyncAwaitTests: XCTestCase { } } + func testAsyncRouterGroup() throws { + #if os(macOS) + // disable macOS tests in CI. GH Actions are currently running this when they shouldn't + guard HBEnvironment().get("CI") != "true" else { throw XCTSkip() } + #endif + let app = HBApplication(testing: .asyncTest) + app.router.group("test").get("/hello") { request -> ByteBuffer in + return await self.getBuffer(request: request) + } + try app.XCTStart() + defer { app.XCTStop() } + + try app.XCTExecute(uri: "/test/hello", method: .GET) { response in + var body = try XCTUnwrap(response.body) + let string = body.readString(length: body.readableBytes) + XCTAssertEqual(response.status, .ok) + XCTAssertEqual(string, "Async Hello") + } + } + func testAsyncMiddleware() throws { #if os(macOS) // disable macOS tests in CI. GH Actions are currently running this when they shouldn't @@ -104,6 +124,31 @@ final class AsyncAwaitTests: XCTestCase { } } + func testCollatingRequestBody() throws { + #if os(macOS) + // disable macOS tests in CI. GH Actions are currently running this when they shouldn't + guard HBEnvironment().get("CI") != "true" else { throw XCTSkip() } + #endif + let app = HBApplication(testing: .asyncTest) + app.router.patch("size") { request -> String in + guard let body = request.body.buffer else { + throw HBHTTPError(.badRequest) + } + // force route to be async + try await Task.sleep(nanoseconds: 1) + return body.readableBytes.description + } + + try app.XCTStart() + defer { app.XCTStop() } + + let buffer = self.randomBuffer(size: 530_001) + try app.XCTExecute(uri: "/size", method: .PATCH, body: buffer) { response in + let body = try XCTUnwrap(response.body) + XCTAssertEqual(String(buffer: body), "530001") + } + } + /// Test streaming of requests via AsyncSequence func testStreaming() throws { #if os(macOS)