Skip to content

Commit ce82178

Browse files
authored
fix validation error propagation (#221)
1 parent 2a344e7 commit ce82178

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Sources/AsyncHTTPClient/HTTPHandler.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,10 @@ extension HTTPClient {
554554

555555
func fail<Delegate: HTTPClientResponseDelegate>(with error: Error, delegateType: Delegate.Type) {
556556
if let connection = self.connection {
557-
connection.channel.close(promise: nil)
558557
self.releaseAssociatedConnection(delegateType: delegateType, closing: true)
559558
.whenSuccess {
560559
self.promise.fail(error)
560+
connection.channel.close(promise: nil)
561561
}
562562
}
563563
}
@@ -729,7 +729,7 @@ extension TaskHandler: ChannelDuplexHandler {
729729
try headers.validate(method: request.method, body: request.body)
730730
} catch {
731731
promise?.fail(error)
732-
context.fireErrorCaught(error)
732+
self.failTaskAndNotifyDelegate(error: error, self.delegate.didReceiveError)
733733
self.state = .end
734734
return
735735
}

Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ extension HTTPClientTests {
9696
("testRacePoolIdleConnectionsAndGet", testRacePoolIdleConnectionsAndGet),
9797
("testAvoidLeakingTLSHandshakeCompletionPromise", testAvoidLeakingTLSHandshakeCompletionPromise),
9898
("testAsyncShutdown", testAsyncShutdown),
99+
("testValidationErrorsAreSurfaced", testValidationErrorsAreSurfaced),
99100
("testUploadsReallyStream", testUploadsReallyStream),
100101
]
101102
}

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+18-1
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ class HTTPClientTests: XCTestCase {
16671667
}
16681668
}
16691669

1670-
func testAsyncShutdown() {
1670+
func testAsyncShutdown() throws {
16711671
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
16721672
let promise = self.clientGroup.next().makePromise(of: Void.self)
16731673
self.clientGroup.next().execute {
@@ -1679,6 +1679,23 @@ class HTTPClientTests: XCTestCase {
16791679
XCTAssertNoThrow(try promise.futureResult.wait())
16801680
}
16811681

1682+
func testValidationErrorsAreSurfaced() throws {
1683+
let httpBin = HTTPBin()
1684+
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
1685+
defer {
1686+
XCTAssertNoThrow(try httpClient.syncShutdown())
1687+
XCTAssertNoThrow(try httpBin.shutdown())
1688+
}
1689+
1690+
let request = try HTTPClient.Request(url: "http://localhost:\(httpBin.port)/get", method: .TRACE, body: .stream { _ in
1691+
httpClient.eventLoopGroup.next().makeSucceededFuture(())
1692+
})
1693+
let runningRequest = httpClient.execute(request: request)
1694+
XCTAssertThrowsError(try runningRequest.wait()) { error in
1695+
XCTAssertEqual(HTTPClientError.traceRequestWithBody, error as? HTTPClientError)
1696+
}
1697+
}
1698+
16821699
func testUploadsReallyStream() {
16831700
final class HTTPServer: ChannelInboundHandler {
16841701
typealias InboundIn = HTTPServerRequestPart

0 commit comments

Comments
 (0)