From 3bb47e391169180c70162335af59816b7151feed Mon Sep 17 00:00:00 2001 From: Mattes Mohr Date: Wed, 8 Jan 2025 21:38:01 +0100 Subject: [PATCH] Test the error reporting for the environment through the provider --- Tests/HTMLKitVaporTests/ProviderTests.swift | 72 +++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/Tests/HTMLKitVaporTests/ProviderTests.swift b/Tests/HTMLKitVaporTests/ProviderTests.swift index a96a9fb3..2877188b 100644 --- a/Tests/HTMLKitVaporTests/ProviderTests.swift +++ b/Tests/HTMLKitVaporTests/ProviderTests.swift @@ -393,4 +393,76 @@ final class ProviderTests: XCTestCase { ) } } + + /// Tests the error reporting to Vapor for issues that may occur during environment access. + /// + /// The error is expected to be classified as an internal server error and includes a error message. + func testEnvironmentErrorReporting() throws { + + struct TestObject { + + let firstName = "Jane" + } + + struct UnkownObject: HTMLKit.View { + + @EnvironmentObject(TestObject.self) + var object + + var body: HTMLKit.Content { + Paragraph { + Environment.when(object.firstName) { + "True" + } + } + } + } + + struct WrongCast: HTMLKit.View { + + @EnvironmentObject(TestObject.self) + var object + + var body: HTMLKit.Content { + Paragraph { + Environment.when(object.firstName) { + "True" + } + } + .environment(object: TestObject()) + } + } + + let app = Application(.testing) + + defer { app.shutdown() } + + app.get("unkownobject") { request async throws -> Vapor.View in + + return try await request.htmlkit.render(UnkownObject()) + } + + app.get("wrongcast") { request async throws -> Vapor.View in + + return try await request.htmlkit.render(WrongCast()) + } + + try app.test(.GET, "unkownobject") { response in + + XCTAssertEqual(response.status, .internalServerError) + + let abort = try response.content.decode(AbortResponse.self) + + XCTAssertEqual(abort.reason, "Unable to retrieve environment object.") + } + + try app.test(.GET, "wrongcast") { response in + + XCTAssertEqual(response.status, .internalServerError) + + let abort = try response.content.decode(AbortResponse.self) + + XCTAssertEqual(abort.reason, "Unable to cast the environment value.") + } + } }