Skip to content

Commit

Permalink
Test the error reporting for the environment through the provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mattesmohr committed Jan 8, 2025
1 parent 801b1ed commit 3bb47e3
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Tests/HTMLKitVaporTests/ProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
}

0 comments on commit 3bb47e3

Please sign in to comment.