From f377115deba8c99467601851818bcc56c6d1e022 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Tue, 14 Jan 2025 20:18:47 +0000 Subject: [PATCH] Add test verifying connection is closed if error thrown in response body writer --- Tests/HummingbirdTests/ApplicationTests.swift | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Tests/HummingbirdTests/ApplicationTests.swift b/Tests/HummingbirdTests/ApplicationTests.swift index 84045c65..fc107018 100644 --- a/Tests/HummingbirdTests/ApplicationTests.swift +++ b/Tests/HummingbirdTests/ApplicationTests.swift @@ -983,6 +983,27 @@ final class ApplicationTests: XCTestCase { } } #endif + + func testErrorInResponseWriterClosesConnection() async throws { + let router = Router() + router.post("error") { request, context -> Response in + Response( + status: .ok, + body: .init { writer in + throw HTTPError(.badRequest) + } + ) + } + let app = Application(router: router) + try await app.test(.live) { client in + do { + _ = try await client.execute(uri: "/error", method: .post) + XCTFail("Should not receive a response as the response writer failed before finishing") + } catch TestClient.Error.connectionClosing { + // verify connection was closed before reading full response + } + } + } } /// HTTPField used during tests