Skip to content

Commit 8e60b94

Browse files
authored
use standard ByteBuffer construction methods (#262)
1 parent 275502f commit 8e60b94

File tree

4 files changed

+20
-39
lines changed

4 files changed

+20
-39
lines changed

Diff for: Sources/AsyncHTTPClient/HTTPHandler.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ extension HTTPClient {
7777
/// - data: Body `Data` representation.
7878
public static func data(_ data: Data) -> Body {
7979
return Body(length: data.count) { writer in
80-
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
81-
buffer.writeBytes(data)
82-
return writer.write(.byteBuffer(buffer))
80+
writer.write(.byteBuffer(ByteBuffer(bytes: data)))
8381
}
8482
}
8583

@@ -89,9 +87,7 @@ extension HTTPClient {
8987
/// - string: Body `String` representation.
9088
public static func string(_ string: String) -> Body {
9189
return Body(length: string.utf8.count) { writer in
92-
var buffer = ByteBufferAllocator().buffer(capacity: string.utf8.count)
93-
buffer.writeString(string)
94-
return writer.write(.byteBuffer(buffer))
90+
writer.write(.byteBuffer(ByteBuffer(string: string)))
9591
}
9692
}
9793
}

Diff for: Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class HTTPClientInternalTests: XCTestCase {
6666
head.headers.add(name: "Host", value: "localhost:8080")
6767
head.headers.add(name: "Content-Length", value: "4")
6868
XCTAssertEqual(HTTPClientRequestPart.head(head), recorder.writes[0])
69-
let buffer = ByteBuffer.of(string: "1234")
69+
let buffer = channel.allocator.buffer(string: "1234")
7070
XCTAssertEqual(HTTPClientRequestPart.body(.byteBuffer(buffer)), recorder.writes[1])
7171

7272
XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.head(HTTPResponseHead(version: HTTPVersion(major: 1, minor: 1), status: HTTPResponseStatus.ok))))
@@ -296,7 +296,7 @@ class HTTPClientInternalTests: XCTestCase {
296296
try delegate.optionsApplied.futureResult.wait()
297297

298298
// Send 4 bytes, but only one should be received until the backpressure promise is succeeded.
299-
let buffer = ByteBuffer.of(string: "1234")
299+
let buffer = channel.allocator.buffer(string: "1234")
300300
try channel.writeAndFlush(HTTPServerResponsePart.body(.byteBuffer(buffer))).wait()
301301

302302
// Now we wait until message is delivered to client channel pipeline
@@ -432,9 +432,9 @@ class HTTPClientInternalTests: XCTestCase {
432432
}
433433

434434
let body: HTTPClient.Body = .stream(length: 8) { writer in
435-
let buffer = ByteBuffer.of(string: "1234")
435+
let buffer = ByteBuffer(string: "1234")
436436
return writer.write(.byteBuffer(buffer)).flatMap {
437-
let buffer = ByteBuffer.of(string: "4321")
437+
let buffer = ByteBuffer(string: "4321")
438438
return writer.write(.byteBuffer(buffer))
439439
}
440440
}
@@ -450,7 +450,7 @@ class HTTPClientInternalTests: XCTestCase {
450450
let channel = try promise.futureResult.wait()
451451

452452
// Send 3 parts, but only one should be received until the future is complete
453-
let buffer = ByteBuffer.of(string: "1234")
453+
let buffer = channel.allocator.buffer(string: "1234")
454454
try channel.writeAndFlush(HTTPServerResponsePart.body(.byteBuffer(buffer))).wait()
455455

456456
try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait()
@@ -881,10 +881,10 @@ class HTTPClientInternalTests: XCTestCase {
881881

882882
let body: HTTPClient.Body = .stream(length: 8) { writer in
883883
XCTAssert(el1.inEventLoop)
884-
let buffer = ByteBuffer.of(string: "1234")
884+
let buffer = ByteBuffer(string: "1234")
885885
return writer.write(.byteBuffer(buffer)).flatMap {
886886
XCTAssert(el1.inEventLoop)
887-
let buffer = ByteBuffer.of(string: "4321")
887+
let buffer = ByteBuffer(string: "4321")
888888
return writer.write(.byteBuffer(buffer))
889889
}
890890
}
@@ -917,10 +917,10 @@ class HTTPClientInternalTests: XCTestCase {
917917
let taskPromise = group.next().makePromise(of: HTTPClient.Task<HTTPClient.Response>.self)
918918
let body: HTTPClient.Body = .stream(length: 8) { writer in
919919
XCTAssert(el1.inEventLoop)
920-
let buffer = ByteBuffer.of(string: "1234")
920+
let buffer = ByteBuffer(string: "1234")
921921
return writer.write(.byteBuffer(buffer)).flatMap {
922922
XCTAssert(el1.inEventLoop)
923-
let buffer = ByteBuffer.of(string: "4321")
923+
let buffer = ByteBuffer(string: "4321")
924924
return taskPromise.futureResult.map { (task: HTTPClient.Task<HTTPClient.Response>) -> Void in
925925
XCTAssertNotNil(task.connection)
926926
XCTAssert(task.connection?.channel.eventLoop === el2)

Diff for: Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift

+1-16
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,7 @@ internal final class HttpBinHandler: ChannelInboundHandler {
507507
case "/echohostheader":
508508
var builder = HTTPResponseBuilder(status: .ok)
509509
let hostValue = req.headers["Host"].first ?? ""
510-
var buff = context.channel.allocator.buffer(capacity: hostValue.utf8.count)
511-
buff.writeString(hostValue)
510+
let buff = context.channel.allocator.buffer(string: hostValue)
512511
builder.add(buff)
513512
self.resps.append(builder)
514513
return
@@ -724,20 +723,6 @@ internal final class HttpBinForSSLUncleanShutdownHandler: ChannelInboundHandler
724723
}
725724
}
726725

727-
extension ByteBuffer {
728-
public static func of(string: String) -> ByteBuffer {
729-
var buffer = ByteBufferAllocator().buffer(capacity: string.count)
730-
buffer.writeString(string)
731-
return buffer
732-
}
733-
734-
public static func of(bytes: [UInt8]) -> ByteBuffer {
735-
var buffer = ByteBufferAllocator().buffer(capacity: bytes.count)
736-
buffer.writeBytes(bytes)
737-
return buffer
738-
}
739-
}
740-
741726
struct EventLoopFutureTimeoutError: Error {}
742727

743728
extension EventLoopFuture {

Diff for: Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class HTTPClientTests: XCTestCase {
230230
}
231231

232232
func testMultipleContentLengthHeaders() throws {
233-
let body = ByteBuffer.of(string: "hello world!")
233+
let body = ByteBuffer(string: "hello world!")
234234

235235
var headers = HTTPHeaders()
236236
headers.add(name: "Content-Length", value: "12")
@@ -392,9 +392,9 @@ class HTTPClientTests: XCTestCase {
392392

393393
func testUploadStreaming() throws {
394394
let body: HTTPClient.Body = .stream(length: 8) { writer in
395-
let buffer = ByteBuffer.of(string: "1234")
395+
let buffer = ByteBuffer(string: "1234")
396396
return writer.write(.byteBuffer(buffer)).flatMap {
397-
let buffer = ByteBuffer.of(string: "4321")
397+
let buffer = ByteBuffer(string: "4321")
398398
return writer.write(.byteBuffer(buffer))
399399
}
400400
}
@@ -651,7 +651,7 @@ class HTTPClientTests: XCTestCase {
651651
}
652652

653653
var request = try HTTPClient.Request(url: "http://localhost:\(localHTTPBin.port)/post", method: .POST)
654-
request.body = .byteBuffer(ByteBuffer.of(bytes: [120, 156, 75, 76, 28, 5, 200, 0, 0, 248, 66, 103, 17]))
654+
request.body = .byteBuffer(ByteBuffer(bytes: [120, 156, 75, 76, 28, 5, 200, 0, 0, 248, 66, 103, 17]))
655655
request.headers.add(name: "Accept-Encoding", value: "deflate")
656656

657657
XCTAssertThrowsError(try localClient.execute(request: request).wait()) { error in
@@ -1682,7 +1682,7 @@ class HTTPClientTests: XCTestCase {
16821682
let promise = self.defaultClient.eventLoopGroup.next().makePromise(of: Void.self)
16831683
// We have to toleare callins from any thread
16841684
DispatchQueue(label: "upload-streaming").async {
1685-
writer.write(.byteBuffer(ByteBuffer.of(string: "1234"))).whenComplete { _ in
1685+
writer.write(.byteBuffer(ByteBuffer(string: "1234"))).whenComplete { _ in
16861686
promise.succeed(())
16871687
}
16881688
}
@@ -2049,7 +2049,7 @@ class HTTPClientTests: XCTestCase {
20492049
XCTAssertNoThrow(try httpServer.readInbound()) // .end
20502050

20512051
XCTAssertNoThrow(try httpServer.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .ok))))
2052-
XCTAssertNoThrow(try httpServer.writeOutbound(.body(.byteBuffer(ByteBuffer.of(string: "1234")))))
2052+
XCTAssertNoThrow(try httpServer.writeOutbound(.body(.byteBuffer(ByteBuffer(string: "1234")))))
20532053
XCTAssertNoThrow(try httpServer.writeOutbound(.end(nil)))
20542054

20552055
XCTAssertNoThrow(try future.wait())
@@ -2066,7 +2066,7 @@ class HTTPClientTests: XCTestCase {
20662066
streamWriter.write(.byteBuffer(ByteBuffer(string: "1"))).cascade(to: promise)
20672067
}
20682068
return promise.futureResult
2069-
})).wait()) { error in
2069+
})).wait()) { error in
20702070
XCTAssertEqual(error as! HTTPClientError, HTTPClientError.bodyLengthMismatch)
20712071
}
20722072
// Quickly try another request and check that it works.
@@ -2092,7 +2092,7 @@ class HTTPClientTests: XCTestCase {
20922092
Request(url: url,
20932093
body: .stream(length: 1) { streamWriter in
20942094
streamWriter.write(.byteBuffer(ByteBuffer(string: tooLong)))
2095-
})).wait()) { error in
2095+
})).wait()) { error in
20962096
XCTAssertEqual(error as! HTTPClientError, HTTPClientError.bodyLengthMismatch)
20972097
}
20982098
// Quickly try another request and check that it works. If we by accident wrote some extra bytes into the

0 commit comments

Comments
 (0)