Files
swift-nio/Tests/NIOHTTP1Tests/ByteBufferUtilsTest.swift
T
Johannes WeissandCory Benfield e57122bd89 ByteBuffer: add convenience initialisers (#1533)
Motivation:

There are multiple sub-optimal ByteBuffer creation patterns that occur
in the wild. Most often they happen when people don't actually have
access to a `Channel` just want to "convert" a `String` into a
`ByteBuffer`. To do this, they are forced to type

    var buffer = ByteBufferAllocator().buffer(capacity: string.utf8.count)
    buffer.writeString(string)

Sometimes, they don't get the capacity calculation right or just put a
`0`.

Similar problems happen if NIO users want to cache a ByteBuffer in their
`ChannelHandler`. You will then find this code:

```swift
if self.buffer == nil {
    self.buffer = receivedBuffer
} else {
    var receivedBuffer = receivedBuffer
    self.buffer!.writeBuffer(&receivedBuffer)
}
```

And lastly, sometimes people want to append one `ByteBuffer` to another
without mutating the appendee. That's also cumbersome because we only
support a mutable version of `writeBuffer`.

Modifications:

- add `ByteBuffer` convenience initialisers
- add convenience `writeBuffer` methods to `Optional<ByteBuffer>`
- add `writeBufferImmutable` which doesn't mutate the appendee.

Result:

More convenience.

Co-authored-by: Cory Benfield <lukasa@apple.com>

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-06-04 21:02:11 +01:00

77 lines
3.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
@testable import NIOHTTP1
fileprivate enum DummyError: Error {
case err
}
class ByteBufferUtilsTest: XCTestCase {
func testComparators() {
let someByteBuffer: ByteBuffer = ByteBuffer(string: "fiRSt")
XCTAssert(
someByteBuffer.readableBytesView.compareCaseInsensitiveASCIIBytes(
to: "first".utf8))
XCTAssert(
someByteBuffer.readableBytesView.compareCaseInsensitiveASCIIBytes(
to: "fiRSt".utf8))
XCTAssert(
someByteBuffer.readableBytesView.compareCaseInsensitiveASCIIBytes(
to: "fIrst".utf8))
XCTAssertFalse(
someByteBuffer.readableBytesView.compareCaseInsensitiveASCIIBytes(
to: "fIrt".utf8))
XCTAssertFalse(
someByteBuffer.readableBytesView.compareCaseInsensitiveASCIIBytes(
to: "firsta".utf8))
XCTAssertFalse(
someByteBuffer.readableBytesView.compareCaseInsensitiveASCIIBytes(
to: "afirst".utf8))
XCTAssertFalse(
someByteBuffer.readableBytesView.compareCaseInsensitiveASCIIBytes(
to: "eiRSt".utf8))
XCTAssertFalse(
someByteBuffer.readableBytesView.compareCaseInsensitiveASCIIBytes(
to: "fIrso".utf8))
XCTAssertFalse(
someByteBuffer.readableBytesView.compareCaseInsensitiveASCIIBytes(
to: "firot".utf8))
}
private func byteBufferView(string: String) -> ByteBufferView {
let byteBufferAllocator = ByteBufferAllocator()
var buffer = byteBufferAllocator.buffer(capacity: string.lengthOfBytes(using: .utf8))
buffer.writeString(string)
return buffer.readableBytesView
}
func testTrimming() {
XCTAssertEqual(byteBufferView(string: " first").trimSpaces().map({CChar($0)}), byteBufferView(string: "first").map({CChar($0)}))
XCTAssertEqual(byteBufferView(string: " first ").trimSpaces().map({CChar($0)}), byteBufferView(string: "first").map({CChar($0)}))
XCTAssertEqual(byteBufferView(string: "first ").trimSpaces().map({CChar($0)}), byteBufferView(string: "first").map({CChar($0)}))
XCTAssertEqual(byteBufferView(string: "first").trimSpaces().map({CChar($0)}), byteBufferView(string: "first").map({CChar($0)}))
XCTAssertEqual(byteBufferView(string: " \t\t fi rst").trimSpaces().map({CChar($0)}), byteBufferView(string: "fi rst").map({CChar($0)}))
XCTAssertEqual(byteBufferView(string: " firs t \t ").trimSpaces().map({CChar($0)}), byteBufferView(string: "firs t").map({CChar($0)}))
XCTAssertEqual(byteBufferView(string: "f\t irst ").trimSpaces().map({CChar($0)}), byteBufferView(string: "f\t irst").map({CChar($0)}))
XCTAssertEqual(byteBufferView(string: "f i rs t").trimSpaces().map({CChar($0)}), byteBufferView(string: "f i rs t").map({CChar($0)}))
XCTAssertEqual(byteBufferView(string: " \t \t ").trimSpaces().map({CChar($0)}),
byteBufferView(string: "").map({CChar($0)}))
}
}