From e4611866a8611ef589cc70b5104e3326d9304650 Mon Sep 17 00:00:00 2001
From: Artem Redkin <aredkin@apple.com>
Date: Wed, 20 May 2020 10:49:35 +0100
Subject: [PATCH 1/3] fix validation error propagation

---
 Sources/AsyncHTTPClient/HTTPHandler.swift       |  4 ++--
 .../AsyncHTTPClientTests/HTTPClientTests.swift  | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)

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<Delegate: HTTPClientResponseDelegate>(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.swift b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift
index e9e6a3c2b..1ec2cd120 100644
--- a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift
+++ b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift
@@ -1678,4 +1678,21 @@ class HTTPClientTests: XCTestCase {
         }
         XCTAssertNoThrow(try promise.futureResult.wait())
     }
+
+    func testValidationErrorsAreSurfaced() {
+        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 { writer in
+            httpClient.eventLoopGroup.next().makeSucceededFuture(())
+        })
+        let runningRequest = httpClient.execute(request: request)
+        XCTAssertThrowsError(try runningRequest.wait()) { error in
+            XCTAssertEqual(HTTPClientError.traceRequestWithBody, error as? HTTPClientError)
+        }
+    }
 }

From f1ac9bfd67fd45426999e24c40280d0ff2becdc1 Mon Sep 17 00:00:00 2001
From: Artem Redkin <aredkin@apple.com>
Date: Wed, 20 May 2020 10:56:23 +0100
Subject: [PATCH 2/3] linux tests and format

---
 Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift | 1 +
 Tests/AsyncHTTPClientTests/HTTPClientTests.swift        | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift b/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift
index 49cac8454..4ecbedc6b 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),
         ]
     }
 }
diff --git a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift
index 1ec2cd120..3bcd6c25d 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 {
@@ -1687,7 +1687,7 @@ class HTTPClientTests: XCTestCase {
             XCTAssertNoThrow(try httpBin.shutdown())
         }
 
-        let request = try! HTTPClient.Request(url: "http://localhost:\(httpBin.port)/get", method: .TRACE, body: .stream { writer in
+        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)

From 30a09ec7748ae271b4c73b8e60d2ab27846a0b27 Mon Sep 17 00:00:00 2001
From: Artem Redkin <aredkin@apple.com>
Date: Wed, 20 May 2020 10:57:47 +0100
Subject: [PATCH 3/3] fix compilation

---
 Tests/AsyncHTTPClientTests/HTTPClientTests.swift | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift
index 3bcd6c25d..f995eb1ea 100644
--- a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift
+++ b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift
@@ -1679,7 +1679,7 @@ class HTTPClientTests: XCTestCase {
         XCTAssertNoThrow(try promise.futureResult.wait())
     }
 
-    func testValidationErrorsAreSurfaced() {
+    func testValidationErrorsAreSurfaced() throws {
         let httpBin = HTTPBin()
         let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
         defer {