Skip to content

Commit

Permalink
Use ResponseBody closure to write response
Browse files Browse the repository at this point in the history
FIx tests, up the hummingbird version
  • Loading branch information
adam-fowler committed Aug 23, 2024
1 parent 0e9fbcf commit c9f7409
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion response-body-processing/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "App", targets: ["App"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-rc.1"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-rc.4"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0"),
.package(url: "https://github.com/apple/swift-crypto", from: "3.0.0"),
],
Expand Down
8 changes: 7 additions & 1 deletion response-body-processing/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ public func buildApplication(_ arguments: some AppArguments) async throws -> som
router.add(middleware: AddSHA256DigestMiddleware())
// Add echo route
router.post("/echo") { request, context -> Response in
// Demostrating returning a ResponseBody initialized with a closure writing the contents
// of the body. This is the equivalent of `ResponseBody(asyncSequence: request.body)``
let responseBody = ResponseBody { writer in
try await writer.write(request.body)
try await writer.finish(nil)
}
return Response(
status: .ok,
headers: [.contentType: request.headers[.contentType] ?? "application/octet-stream"],
body: .init(asyncSequence: request.body)
body: responseBody
)
}
let app = Application(
Expand Down
2 changes: 1 addition & 1 deletion response-body-processing/Tests/AppTests/AppTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class AppTests: XCTestCase {
let buffer = Self.randomBuffer(size: 256 * 000)
let digest = SHA256.hash(data: Data(buffer.readableBytesView))
try await app.test(.live) { client in
try await client.execute(uri: "/echo", method: .get, body: buffer) { response in
try await client.execute(uri: "/echo", method: .post, body: buffer) { response in
XCTAssertEqual(
response.trailerHeaders?[.digest],
"sha256=\(digest.hexDigest())"
Expand Down

0 comments on commit c9f7409

Please sign in to comment.