Skip to content

Commit

Permalink
Update for HB 2.0.0-alpha.2 (#14)
Browse files Browse the repository at this point in the history
* Update for HB 2.0.0-alpha.2

* Ensure request/response length is set
  • Loading branch information
adam-fowler authored Feb 6, 2024
1 parent 81deeb9 commit 8007a5f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-alpha.1"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-alpha.2"),
],
targets: [
.target(
Expand Down
30 changes: 19 additions & 11 deletions Sources/OpenAPIHummingbird/OpenAPITransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ extension HBRequest {
/// Construct ``OpenAPIRuntime.Request`` from Hummingbird ``HBRequest``
func makeOpenAPIRequest<Context: HBBaseRequestContext>(context: Context) throws -> (HTTPRequest, HTTPBody?) {
let request = self.head
let body: HTTPBody?
switch self.body {
case .byteBuffer(let buffer):
body = HTTPBody([UInt8](buffer: buffer))
case .stream(let streamer):
body = .init(
streamer.map { [UInt8](buffer: $0) },
length: .unknown,
iterationBehavior: .single
)
// extract length from content-length header
let length = if let contentLengthHeader = self.headers[.contentLength], let contentLength = Int(contentLengthHeader) {
HTTPBody.Length.known(numericCast(contentLength))
} else {
HTTPBody.Length.unknown
}
let body = HTTPBody(
self.body.map { [UInt8](buffer: $0) },
length: length,
iterationBehavior: .single
)
return (request, body)
}
}
Expand All @@ -80,7 +80,15 @@ extension HBResponse {
let responseBody: HBResponseBody
if let body = body {
let bufferSequence = body.map { ByteBuffer(bytes: $0)}
responseBody = .init(asyncSequence: bufferSequence)
if case .known(let length) = body.length {
responseBody = .init(contentLength: numericCast(length)) { writer in
for try await buffer in bufferSequence {
try await writer.write(buffer)
}
}
} else {
responseBody = .init(asyncSequence: bufferSequence)
}
} else {
responseBody = .init(byteBuffer: ByteBuffer())
}
Expand Down
1 change: 1 addition & 0 deletions Tests/OpenAPIHummingbirdTests/OpenAPITransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ final class HBOpenAPITransportTests: XCTestCase {
// Check the HBResponse (created from the Response) is what meets expectations.
XCTAssertEqual(hbResponse.status, .created)
XCTAssertEqual(hbResponse.headers[.xMumble], "mumble")
XCTAssertEqual(hbResponse.headers[.contentLength], "👋".utf8.count.description)
XCTAssertEqual(try String(buffer: XCTUnwrap(hbResponse.body)), "👋")
}
}
Expand Down

0 comments on commit 8007a5f

Please sign in to comment.