-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support informational response heads (#469)
- Loading branch information
1 parent
cc8e7a6
commit 170fd53
Showing
10 changed files
with
179 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Tests/AsyncHTTPClientTests/HTTPClientInformationalResponsesTests+XCTest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2018-2019 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// HTTPClientInformationalResponsesTests+XCTest.swift | ||
// | ||
import XCTest | ||
|
||
/// | ||
/// NOTE: This file was generated by generate_linux_tests.rb | ||
/// | ||
/// Do NOT edit this file directly as it will be regenerated automatically when needed. | ||
/// | ||
|
||
extension HTTPClientReproTests { | ||
static var allTests: [(String, (HTTPClientReproTests) -> () throws -> Void)] { | ||
return [ | ||
("testServerSends100ContinueFirst", testServerSends100ContinueFirst), | ||
("testServerSendsSwitchingProtocols", testServerSendsSwitchingProtocols), | ||
] | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
Tests/AsyncHTTPClientTests/HTTPClientInformationalResponsesTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import AsyncHTTPClient | ||
import Logging | ||
import NIOCore | ||
import NIOHTTP1 | ||
import XCTest | ||
|
||
final class HTTPClientReproTests: XCTestCase { | ||
func testServerSends100ContinueFirst() { | ||
final class HTTPInformationalResponseHandler: ChannelInboundHandler { | ||
typealias InboundIn = HTTPServerRequestPart | ||
typealias OutboundOut = HTTPServerResponsePart | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
switch self.unwrapInboundIn(data) { | ||
case .head: | ||
context.writeAndFlush(self.wrapOutboundOut(.head(.init(version: .http1_1, status: .continue))), promise: nil) | ||
case .body: | ||
break | ||
case .end: | ||
context.write(self.wrapOutboundOut(.head(.init(version: .http1_1, status: .ok))), promise: nil) | ||
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) | ||
} | ||
} | ||
} | ||
|
||
let client = HTTPClient(eventLoopGroupProvider: .createNew) | ||
defer { XCTAssertNoThrow(try client.syncShutdown()) } | ||
|
||
let httpBin = HTTPBin(.http1_1(ssl: false, compress: false)) { _ in | ||
HTTPInformationalResponseHandler() | ||
} | ||
|
||
let body = #"{"foo": "bar"}"# | ||
|
||
var maybeRequest: HTTPClient.Request? | ||
XCTAssertNoThrow(maybeRequest = try HTTPClient.Request( | ||
url: "http://localhost:\(httpBin.port)/", | ||
method: .POST, | ||
headers: [ | ||
"Content-Type": "application/json", | ||
], | ||
body: .string(body) | ||
)) | ||
guard let request = maybeRequest else { return XCTFail("Expected to have a request here") } | ||
|
||
var logger = Logger(label: "test") | ||
logger.logLevel = .trace | ||
|
||
var response: HTTPClient.Response? | ||
XCTAssertNoThrow(response = try client.execute(request: request, logger: logger).wait()) | ||
XCTAssertEqual(response?.status, .ok) | ||
} | ||
|
||
func testServerSendsSwitchingProtocols() { | ||
final class HTTPInformationalResponseHandler: ChannelInboundHandler { | ||
typealias InboundIn = HTTPServerRequestPart | ||
typealias OutboundOut = HTTPServerResponsePart | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
switch self.unwrapInboundIn(data) { | ||
case .head: | ||
let head = HTTPResponseHead(version: .http1_1, status: .switchingProtocols, headers: [ | ||
"Connection": "Upgrade", | ||
"Upgrade": "Websocket", | ||
]) | ||
let body = context.channel.allocator.buffer(string: "foo bar") | ||
|
||
context.write(self.wrapOutboundOut(.head(head)), promise: nil) | ||
context.write(self.wrapOutboundOut(.body(.byteBuffer(body))), promise: nil) | ||
// we purposefully don't send an `.end` here. | ||
context.flush() | ||
case .body: | ||
break | ||
case .end: | ||
break | ||
} | ||
} | ||
} | ||
|
||
let client = HTTPClient(eventLoopGroupProvider: .createNew) | ||
defer { XCTAssertNoThrow(try client.syncShutdown()) } | ||
|
||
let httpBin = HTTPBin(.http1_1(ssl: false, compress: false)) { _ in | ||
HTTPInformationalResponseHandler() | ||
} | ||
|
||
let body = #"{"foo": "bar"}"# | ||
|
||
var maybeRequest: HTTPClient.Request? | ||
XCTAssertNoThrow(maybeRequest = try HTTPClient.Request( | ||
url: "http://localhost:\(httpBin.port)/", | ||
method: .POST, | ||
headers: [ | ||
"Content-Type": "application/json", | ||
], | ||
body: .string(body) | ||
)) | ||
guard let request = maybeRequest else { return XCTFail("Expected to have a request here") } | ||
|
||
var logger = Logger(label: "test") | ||
logger.logLevel = .trace | ||
|
||
var response: HTTPClient.Response? | ||
XCTAssertNoThrow(response = try client.execute(request: request, logger: logger).wait()) | ||
XCTAssertEqual(response?.status, .switchingProtocols) | ||
XCTAssertNil(response?.body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters