mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation: - `ChannelPipeline.add(name:handler:...)` had a strange order of arguments - `remove(handler:)` and `remove(ctx:)` both remove `ChannelHandler`s but they read like they remove different things So let's just fix the argument order and name them `addHandler` and `removeHandler` making clear what they do. Modifications: - rename all `ChannelPipeline.add(name:handler:...)`s to `ChannelPipeline.addHandler(_:name:...)` - rename all `ChannelPipeline.remove(...)`s to `ChannelPipeline.removeHandler(...)` Result: more readable and consistent code
114 lines
5.2 KiB
Swift
114 lines
5.2 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
|
|
@testable import NIO
|
|
@testable import NIOHTTP1
|
|
|
|
private extension ByteBuffer {
|
|
func assertContainsOnly(_ string: String) {
|
|
let innerData = self.getString(at: self.readerIndex, length: self.readableBytes)!
|
|
XCTAssertEqual(innerData, string)
|
|
}
|
|
}
|
|
|
|
class HTTPResponseEncoderTests: XCTestCase {
|
|
private func sendResponse(withStatus status: HTTPResponseStatus, andHeaders headers: HTTPHeaders) -> ByteBuffer {
|
|
let channel = EmbeddedChannel()
|
|
defer {
|
|
XCTAssertEqual(.some(false), try? channel.finish())
|
|
}
|
|
|
|
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPResponseEncoder()).wait())
|
|
var switchingResponse = HTTPResponseHead(version: HTTPVersion(major: 1, minor:1), status: status)
|
|
switchingResponse.headers = headers
|
|
XCTAssertNoThrow(try channel.writeOutbound(HTTPServerResponsePart.head(switchingResponse)))
|
|
if let buffer = channel.readOutbound(as: ByteBuffer.self) {
|
|
return buffer
|
|
} else {
|
|
fatalError("Could not read ByteBuffer from channel")
|
|
}
|
|
}
|
|
|
|
func testNoAutoHeadersFor101() throws {
|
|
let writtenData = sendResponse(withStatus: .switchingProtocols, andHeaders: HTTPHeaders())
|
|
writtenData.assertContainsOnly("HTTP/1.1 101 Switching Protocols\r\n\r\n")
|
|
}
|
|
|
|
func testNoAutoHeadersForCustom1XX() throws {
|
|
let headers = HTTPHeaders([("Link", "</styles.css>; rel=preload; as=style")])
|
|
let writtenData = sendResponse(withStatus: .custom(code: 103, reasonPhrase: "Early Hints"), andHeaders: headers)
|
|
writtenData.assertContainsOnly("HTTP/1.1 103 Early Hints\r\nLink: </styles.css>; rel=preload; as=style\r\n\r\n")
|
|
}
|
|
|
|
func testNoAutoHeadersFor204() throws {
|
|
let writtenData = sendResponse(withStatus: .noContent, andHeaders: HTTPHeaders())
|
|
writtenData.assertContainsOnly("HTTP/1.1 204 No Content\r\n\r\n")
|
|
}
|
|
|
|
func testNoContentLengthHeadersFor101() throws {
|
|
let headers = HTTPHeaders([("content-length", "0")])
|
|
let writtenData = sendResponse(withStatus: .switchingProtocols, andHeaders: headers)
|
|
writtenData.assertContainsOnly("HTTP/1.1 101 Switching Protocols\r\n\r\n")
|
|
}
|
|
|
|
func testNoContentLengthHeadersForCustom1XX() throws {
|
|
let headers = HTTPHeaders([("Link", "</styles.css>; rel=preload; as=style"), ("content-length", "0")])
|
|
let writtenData = sendResponse(withStatus: .custom(code: 103, reasonPhrase: "Early Hints"), andHeaders: headers)
|
|
writtenData.assertContainsOnly("HTTP/1.1 103 Early Hints\r\nLink: </styles.css>; rel=preload; as=style\r\n\r\n")
|
|
}
|
|
|
|
func testNoContentLengthHeadersFor204() throws {
|
|
let headers = HTTPHeaders([("content-length", "0")])
|
|
let writtenData = sendResponse(withStatus: .noContent, andHeaders: headers)
|
|
writtenData.assertContainsOnly("HTTP/1.1 204 No Content\r\n\r\n")
|
|
}
|
|
|
|
func testNoTransferEncodingHeadersFor101() throws {
|
|
let headers = HTTPHeaders([("transfer-encoding", "chunked")])
|
|
let writtenData = sendResponse(withStatus: .switchingProtocols, andHeaders: headers)
|
|
writtenData.assertContainsOnly("HTTP/1.1 101 Switching Protocols\r\n\r\n")
|
|
}
|
|
|
|
func testNoTransferEncodingHeadersForCustom1XX() throws {
|
|
let headers = HTTPHeaders([("Link", "</styles.css>; rel=preload; as=style"), ("transfer-encoding", "chunked")])
|
|
let writtenData = sendResponse(withStatus: .custom(code: 103, reasonPhrase: "Early Hints"), andHeaders: headers)
|
|
writtenData.assertContainsOnly("HTTP/1.1 103 Early Hints\r\nLink: </styles.css>; rel=preload; as=style\r\n\r\n")
|
|
}
|
|
|
|
func testNoTransferEncodingHeadersFor204() throws {
|
|
let headers = HTTPHeaders([("transfer-encoding", "chunked")])
|
|
let writtenData = sendResponse(withStatus: .noContent, andHeaders: headers)
|
|
writtenData.assertContainsOnly("HTTP/1.1 204 No Content\r\n\r\n")
|
|
}
|
|
|
|
func testNoChunkedEncodingForHTTP10() throws {
|
|
let channel = EmbeddedChannel()
|
|
defer {
|
|
XCTAssertEqual(.some(false), try? channel.finish())
|
|
}
|
|
|
|
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPResponseEncoder()).wait())
|
|
|
|
// This response contains neither Transfer-Encoding: chunked or Content-Length.
|
|
let response = HTTPResponseHead(version: HTTPVersion(major: 1, minor:0), status: .ok)
|
|
XCTAssertNoThrow(try channel.writeOutbound(HTTPServerResponsePart.head(response)))
|
|
guard let b = channel.readOutbound(as: ByteBuffer.self) else {
|
|
XCTFail("Could not read byte buffer")
|
|
return
|
|
}
|
|
let writtenResponse = b.getString(at: b.readerIndex, length: b.readableBytes)!
|
|
XCTAssertEqual(writtenResponse, "HTTP/1.0 200 OK\r\n\r\n")
|
|
}
|
|
}
|