Skip to content

Commit

Permalink
Use async version of HBRequestBody.consumeBody, adding tests (#185)
Browse files Browse the repository at this point in the history
* Use async version of HBRequestBody.consumeBody

Also add tests

* Add async routergroup test
  • Loading branch information
adam-fowler authored Mar 23, 2023
1 parent 17b914c commit 3c07665
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
5 changes: 2 additions & 3 deletions Sources/Hummingbird/AsyncAwaitSupport/Router+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
45 changes: 45 additions & 0 deletions Tests/HummingbirdTests/AsyncAwaitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3c07665

Please sign in to comment.