Skip to content

Make Body.data generic #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/AsyncHTTPClient/FoundationExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ extension HTTPClient.Cookie {
)
}
}

extension HTTPClient.Body {
/// Create and stream body using `Data`.
///
/// - parameters:
/// - bytes: Body `Data` representation.
public static func data(_ data: Data) -> HTTPClient.Body {
return self.bytes(data)
}
}
17 changes: 12 additions & 5 deletions Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ extension HTTPClient {
/// Body chunk provider.
public var stream: (StreamWriter) -> EventLoopFuture<Void>

@inlinable
init(length: Int?, stream: @escaping (StreamWriter) -> EventLoopFuture<Void>) {
self.length = length
self.stream = stream
}

/// Create and stream body using `ByteBuffer`.
///
/// - parameters:
Expand All @@ -69,13 +75,14 @@ extension HTTPClient {
return Body(length: length, stream: stream)
}

/// Create and stream body using `Data`.
/// Create and stream body using a collection of bytes.
///
/// - parameters:
/// - data: Body `Data` representation.
public static func data(_ data: Data) -> Body {
return Body(length: data.count) { writer in
writer.write(.byteBuffer(ByteBuffer(bytes: data)))
/// - data: Body binary representation.
@inlinable
public static func bytes<Bytes>(_ bytes: Bytes) -> Body where Bytes: RandomAccessCollection, Bytes.Element == UInt8 {
return Body(length: bytes.count) { writer in
writer.write(.byteBuffer(ByteBuffer(bytes: bytes)))
}
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extension HTTPClientTests {
("testGet", testGet),
("testGetWithDifferentEventLoopBackpressure", testGetWithDifferentEventLoopBackpressure),
("testPost", testPost),
("testPostWithGenericBody", testPostWithGenericBody),
("testPostWithFoundationDataBody", testPostWithFoundationDataBody),
("testGetHttps", testGetHttps),
("testGetHttpsWithIP", testGetHttpsWithIP),
("testGetHTTPSWorksOnMTELGWithIP", testGetHTTPSWorksOnMTELGWithIP),
Expand Down
23 changes: 23 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,29 @@ class HTTPClientTests: XCTestCase {
XCTAssertEqual("1234", data.data)
}

func testPostWithGenericBody() throws {
let bodyData = Array("hello, world!").lazy.map { $0.uppercased().first!.asciiValue! }
let erasedData = AnyRandomAccessCollection(bodyData)

let response = try self.defaultClient.post(url: self.defaultHTTPBinURLPrefix + "post", body: .bytes(erasedData)).wait()
let bytes = response.body.flatMap { $0.getData(at: 0, length: $0.readableBytes) }
let data = try JSONDecoder().decode(RequestInfo.self, from: bytes!)

XCTAssertEqual(.ok, response.status)
XCTAssertEqual("HELLO, WORLD!", data.data)
}

func testPostWithFoundationDataBody() throws {
let bodyData = Data("hello, world!".utf8)

let response = try self.defaultClient.post(url: self.defaultHTTPBinURLPrefix + "post", body: .data(bodyData)).wait()
let bytes = response.body.flatMap { $0.getData(at: 0, length: $0.readableBytes) }
let data = try JSONDecoder().decode(RequestInfo.self, from: bytes!)

XCTAssertEqual(.ok, response.status)
XCTAssertEqual("hello, world!", data.data)
}

func testGetHttps() throws {
let localHTTPBin = HTTPBin(.http1_1(ssl: true))
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
Expand Down