Skip to content

Commit 06d365f

Browse files
committed
maxBytes param for bytes() func. tests updated
1 parent 7debf11 commit 06d365f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Sources/AsyncHTTPClient/AsyncAwait/HTTPClientResponse.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,11 @@ extension HTTPClientResponse {
141141
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
142142
extension HTTPClientResponse {
143143
/// Response body as `ByteBuffer`.
144+
/// - Parameter maxBytes: The maximum number of bytes this method is allowed to accumulate.
145+
/// Will accumulate all available bytes if nil passed.
144146
/// - Returns: Bytes collected over time
145-
public func bytes() async throws -> ByteBuffer {
146-
if let expectedBytes = self.headers.first(name: "content-length").flatMap(Int.init) {
147+
public func bytes(upTo maxBytes: Int? = nil) async throws -> ByteBuffer {
148+
if let expectedBytes = maxBytes ?? self.headers.first(name: "content-length").flatMap(Int.init) {
147149
return try await self.body.collect(upTo: expectedBytes)
148150
}
149151

Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,18 @@ final class AsyncAwaitEndToEndTests: XCTestCase {
852852
) else { return }
853853
XCTAssertEqual(response.headers["content-length"], ["4"])
854854
guard let body = await XCTAssertNoThrowWithResult(
855-
try await response.bytes()
855+
try await response.bytes(upTo: 4)
856856
) else { return }
857857
XCTAssertEqual(body, ByteBuffer(string: "1234"))
858+
859+
guard var responseNoContentLength = await XCTAssertNoThrowWithResult(
860+
try await client.execute(request, deadline: .now() + .seconds(10), logger: logger)
861+
) else { return }
862+
responseNoContentLength.headers.remove(name: "content-length")
863+
guard let body2 = await XCTAssertNoThrowWithResult(
864+
try await responseNoContentLength.bytes()
865+
) else { return }
866+
XCTAssertEqual(body2, ByteBuffer(string: "1234"))
858867
}
859868
}
860869

0 commit comments

Comments
 (0)