Skip to content

use standard ByteBuffer construction methods #262

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
Jun 17, 2020
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
8 changes: 2 additions & 6 deletions Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ extension HTTPClient {
/// - data: Body `Data` representation.
public static func data(_ data: Data) -> Body {
return Body(length: data.count) { writer in
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeBytes(data)
return writer.write(.byteBuffer(buffer))
writer.write(.byteBuffer(ByteBuffer(bytes: data)))
}
}

Expand All @@ -89,9 +87,7 @@ extension HTTPClient {
/// - string: Body `String` representation.
public static func string(_ string: String) -> Body {
return Body(length: string.utf8.count) { writer in
var buffer = ByteBufferAllocator().buffer(capacity: string.utf8.count)
buffer.writeString(string)
return writer.write(.byteBuffer(buffer))
writer.write(.byteBuffer(ByteBuffer(string: string)))
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class HTTPClientInternalTests: XCTestCase {
head.headers.add(name: "Host", value: "localhost:8080")
head.headers.add(name: "Content-Length", value: "4")
XCTAssertEqual(HTTPClientRequestPart.head(head), recorder.writes[0])
let buffer = ByteBuffer.of(string: "1234")
let buffer = channel.allocator.buffer(string: "1234")
XCTAssertEqual(HTTPClientRequestPart.body(.byteBuffer(buffer)), recorder.writes[1])

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

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

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

let body: HTTPClient.Body = .stream(length: 8) { writer in
let buffer = ByteBuffer.of(string: "1234")
let buffer = ByteBuffer(string: "1234")
return writer.write(.byteBuffer(buffer)).flatMap {
let buffer = ByteBuffer.of(string: "4321")
let buffer = ByteBuffer(string: "4321")
return writer.write(.byteBuffer(buffer))
}
}
Expand All @@ -450,7 +450,7 @@ class HTTPClientInternalTests: XCTestCase {
let channel = try promise.futureResult.wait()

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

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

let body: HTTPClient.Body = .stream(length: 8) { writer in
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "1234")
let buffer = ByteBuffer(string: "1234")
return writer.write(.byteBuffer(buffer)).flatMap {
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "4321")
let buffer = ByteBuffer(string: "4321")
return writer.write(.byteBuffer(buffer))
}
}
Expand Down Expand Up @@ -917,10 +917,10 @@ class HTTPClientInternalTests: XCTestCase {
let taskPromise = group.next().makePromise(of: HTTPClient.Task<HTTPClient.Response>.self)
let body: HTTPClient.Body = .stream(length: 8) { writer in
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "1234")
let buffer = ByteBuffer(string: "1234")
return writer.write(.byteBuffer(buffer)).flatMap {
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "4321")
let buffer = ByteBuffer(string: "4321")
return taskPromise.futureResult.map { (task: HTTPClient.Task<HTTPClient.Response>) -> Void in
XCTAssertNotNil(task.connection)
XCTAssert(task.connection?.channel.eventLoop === el2)
Expand Down
17 changes: 1 addition & 16 deletions Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,7 @@ internal final class HttpBinHandler: ChannelInboundHandler {
case "/echohostheader":
var builder = HTTPResponseBuilder(status: .ok)
let hostValue = req.headers["Host"].first ?? ""
var buff = context.channel.allocator.buffer(capacity: hostValue.utf8.count)
buff.writeString(hostValue)
let buff = context.channel.allocator.buffer(string: hostValue)
builder.add(buff)
self.resps.append(builder)
return
Expand Down Expand Up @@ -724,20 +723,6 @@ internal final class HttpBinForSSLUncleanShutdownHandler: ChannelInboundHandler
}
}

extension ByteBuffer {
public static func of(string: String) -> ByteBuffer {
var buffer = ByteBufferAllocator().buffer(capacity: string.count)
buffer.writeString(string)
return buffer
}

public static func of(bytes: [UInt8]) -> ByteBuffer {
var buffer = ByteBufferAllocator().buffer(capacity: bytes.count)
buffer.writeBytes(bytes)
return buffer
}
}

struct EventLoopFutureTimeoutError: Error {}

extension EventLoopFuture {
Expand Down
16 changes: 8 additions & 8 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class HTTPClientTests: XCTestCase {
}

func testMultipleContentLengthHeaders() throws {
let body = ByteBuffer.of(string: "hello world!")
let body = ByteBuffer(string: "hello world!")

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

func testUploadStreaming() throws {
let body: HTTPClient.Body = .stream(length: 8) { writer in
let buffer = ByteBuffer.of(string: "1234")
let buffer = ByteBuffer(string: "1234")
return writer.write(.byteBuffer(buffer)).flatMap {
let buffer = ByteBuffer.of(string: "4321")
let buffer = ByteBuffer(string: "4321")
return writer.write(.byteBuffer(buffer))
}
}
Expand Down Expand Up @@ -651,7 +651,7 @@ class HTTPClientTests: XCTestCase {
}

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

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

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

XCTAssertNoThrow(try future.wait())
Expand All @@ -2066,7 +2066,7 @@ class HTTPClientTests: XCTestCase {
streamWriter.write(.byteBuffer(ByteBuffer(string: "1"))).cascade(to: promise)
}
return promise.futureResult
})).wait()) { error in
})).wait()) { error in
XCTAssertEqual(error as! HTTPClientError, HTTPClientError.bodyLengthMismatch)
}
// Quickly try another request and check that it works.
Expand All @@ -2092,7 +2092,7 @@ class HTTPClientTests: XCTestCase {
Request(url: url,
body: .stream(length: 1) { streamWriter in
streamWriter.write(.byteBuffer(ByteBuffer(string: tooLong)))
})).wait()) { error in
})).wait()) { error in
XCTAssertEqual(error as! HTTPClientError, HTTPClientError.bodyLengthMismatch)
}
// Quickly try another request and check that it works. If we by accident wrote some extra bytes into the
Expand Down