diff --git a/Sources/AsyncHTTPClient/HTTPHandler.swift b/Sources/AsyncHTTPClient/HTTPHandler.swift index 6adb55342..b644c1fa8 100644 --- a/Sources/AsyncHTTPClient/HTTPHandler.swift +++ b/Sources/AsyncHTTPClient/HTTPHandler.swift @@ -554,10 +554,10 @@ extension HTTPClient { func fail(with error: Error, delegateType: Delegate.Type) { if let connection = self.connection { - connection.channel.close(promise: nil) self.releaseAssociatedConnection(delegateType: delegateType, closing: true) .whenSuccess { self.promise.fail(error) + connection.channel.close(promise: nil) } } } @@ -729,7 +729,7 @@ extension TaskHandler: ChannelDuplexHandler { try headers.validate(method: request.method, body: request.body) } catch { promise?.fail(error) - context.fireErrorCaught(error) + self.failTaskAndNotifyDelegate(error: error, self.delegate.didReceiveError) self.state = .end return } diff --git a/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift b/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift index 863a377d9..7dcaaf200 100644 --- a/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift +++ b/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift @@ -96,6 +96,7 @@ extension HTTPClientTests { ("testRacePoolIdleConnectionsAndGet", testRacePoolIdleConnectionsAndGet), ("testAvoidLeakingTLSHandshakeCompletionPromise", testAvoidLeakingTLSHandshakeCompletionPromise), ("testAsyncShutdown", testAsyncShutdown), + ("testValidationErrorsAreSurfaced", testValidationErrorsAreSurfaced), ("testUploadsReallyStream", testUploadsReallyStream), ] } diff --git a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift index 4505f44ce..6297767fa 100644 --- a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift +++ b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift @@ -1667,7 +1667,7 @@ class HTTPClientTests: XCTestCase { } } - func testAsyncShutdown() { + func testAsyncShutdown() throws { let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup)) let promise = self.clientGroup.next().makePromise(of: Void.self) self.clientGroup.next().execute { @@ -1679,6 +1679,23 @@ class HTTPClientTests: XCTestCase { XCTAssertNoThrow(try promise.futureResult.wait()) } + func testValidationErrorsAreSurfaced() throws { + let httpBin = HTTPBin() + let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup)) + defer { + XCTAssertNoThrow(try httpClient.syncShutdown()) + XCTAssertNoThrow(try httpBin.shutdown()) + } + + let request = try HTTPClient.Request(url: "http://localhost:\(httpBin.port)/get", method: .TRACE, body: .stream { _ in + httpClient.eventLoopGroup.next().makeSucceededFuture(()) + }) + let runningRequest = httpClient.execute(request: request) + XCTAssertThrowsError(try runningRequest.wait()) { error in + XCTAssertEqual(HTTPClientError.traceRequestWithBody, error as? HTTPClientError) + } + } + func testUploadsReallyStream() { final class HTTPServer: ChannelInboundHandler { typealias InboundIn = HTTPServerRequestPart