Skip to content

Commit 6474d8d

Browse files
authored
Add HTTPClient.Body.bytes as an alternative to .data (#534)
1 parent 972bcdd commit 6474d8d

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

Sources/AsyncHTTPClient/FoundationExtensions.swift

+10
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ extension HTTPClient.Cookie {
5454
)
5555
}
5656
}
57+
58+
extension HTTPClient.Body {
59+
/// Create and stream body using `Data`.
60+
///
61+
/// - parameters:
62+
/// - bytes: Body `Data` representation.
63+
public static func data(_ data: Data) -> HTTPClient.Body {
64+
return self.bytes(data)
65+
}
66+
}

Sources/AsyncHTTPClient/HTTPHandler.swift

+12-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ extension HTTPClient {
4949
/// Body chunk provider.
5050
public var stream: (StreamWriter) -> EventLoopFuture<Void>
5151

52+
@inlinable
53+
init(length: Int?, stream: @escaping (StreamWriter) -> EventLoopFuture<Void>) {
54+
self.length = length
55+
self.stream = stream
56+
}
57+
5258
/// Create and stream body using `ByteBuffer`.
5359
///
5460
/// - parameters:
@@ -69,13 +75,14 @@ extension HTTPClient {
6975
return Body(length: length, stream: stream)
7076
}
7177

72-
/// Create and stream body using `Data`.
78+
/// Create and stream body using a collection of bytes.
7379
///
7480
/// - parameters:
75-
/// - data: Body `Data` representation.
76-
public static func data(_ data: Data) -> Body {
77-
return Body(length: data.count) { writer in
78-
writer.write(.byteBuffer(ByteBuffer(bytes: data)))
81+
/// - data: Body binary representation.
82+
@inlinable
83+
public static func bytes<Bytes>(_ bytes: Bytes) -> Body where Bytes: RandomAccessCollection, Bytes.Element == UInt8 {
84+
return Body(length: bytes.count) { writer in
85+
writer.write(.byteBuffer(ByteBuffer(bytes: bytes)))
7986
}
8087
}
8188

Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ extension HTTPClientTests {
3636
("testGet", testGet),
3737
("testGetWithDifferentEventLoopBackpressure", testGetWithDifferentEventLoopBackpressure),
3838
("testPost", testPost),
39+
("testPostWithGenericBody", testPostWithGenericBody),
40+
("testPostWithFoundationDataBody", testPostWithFoundationDataBody),
3941
("testGetHttps", testGetHttps),
4042
("testGetHttpsWithIP", testGetHttpsWithIP),
4143
("testGetHTTPSWorksOnMTELGWithIP", testGetHTTPSWorksOnMTELGWithIP),

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+23
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,29 @@ class HTTPClientTests: XCTestCase {
296296
XCTAssertEqual("1234", data.data)
297297
}
298298

299+
func testPostWithGenericBody() throws {
300+
let bodyData = Array("hello, world!").lazy.map { $0.uppercased().first!.asciiValue! }
301+
let erasedData = AnyRandomAccessCollection(bodyData)
302+
303+
let response = try self.defaultClient.post(url: self.defaultHTTPBinURLPrefix + "post", body: .bytes(erasedData)).wait()
304+
let bytes = response.body.flatMap { $0.getData(at: 0, length: $0.readableBytes) }
305+
let data = try JSONDecoder().decode(RequestInfo.self, from: bytes!)
306+
307+
XCTAssertEqual(.ok, response.status)
308+
XCTAssertEqual("HELLO, WORLD!", data.data)
309+
}
310+
311+
func testPostWithFoundationDataBody() throws {
312+
let bodyData = Data("hello, world!".utf8)
313+
314+
let response = try self.defaultClient.post(url: self.defaultHTTPBinURLPrefix + "post", body: .data(bodyData)).wait()
315+
let bytes = response.body.flatMap { $0.getData(at: 0, length: $0.readableBytes) }
316+
let data = try JSONDecoder().decode(RequestInfo.self, from: bytes!)
317+
318+
XCTAssertEqual(.ok, response.status)
319+
XCTAssertEqual("hello, world!", data.data)
320+
}
321+
299322
func testGetHttps() throws {
300323
let localHTTPBin = HTTPBin(.http1_1(ssl: true))
301324
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),

0 commit comments

Comments
 (0)