Files
swift-nio/Tests/NIOHTTP1Tests/ByteBufferUtilsTest.swift
T
fadi-botros 78729bd4ad Solving issue #410 (#414)
match all values in HTTPHeaders.isKeepAlive(...)

HTTPHeaders.isKeepAlive(...) does only match the first value.

### Motivation:

Keep-alive and Close may be something on comma separated arrays

### Modifications:

 - Made an extension function to `ByteBuffer` that could separate the strings at low level, without Swift string API, so that it could split the values
 - Another extension functions that compares the comma separated array with a given array to tell the caller which exists and which no
 - Added unit tests for them
 - Used them in the HTTPHeader

### Result:

- Now, if a request is sent using Keep-alive or Close where it was in an array, it will be handled
- fixes #410
2018-08-07 18:49:30 +01:00

78 lines
3.6 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() {
var someByteBuffer: ByteBuffer = ByteBufferAllocator().buffer(capacity: 16)
someByteBuffer.write(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.write(string: 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)}))
}
}