Files
ccn/Services/Website/Tests/Library/Cases/Public/Middlewares/NotFoundMiddlewareTests.swift
T
javier 79ecf311f4 Local environment configuration support for the Website service (#19)
This PR contains the work done to amend the `.env.local` handling for the Website service, plus other small fixes.

Reviewed-on: rock-n-code/loud-amsterdam#19
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2026-07-19 02:28:05 +00:00

81 lines
2.2 KiB
Swift

import Hummingbird
import HummingbirdTesting
import NIOCore
import Testing
@testable import WebsiteLibrary
@Suite("NotFoundMiddleware middleware", .tags(.middleware))
struct NotFoundMiddlewareTests {
// MARK: Constants
private let app: Application = .init(router: {
let router = Router(context: WebsiteRequestContext.self)
router.addMiddleware {
LocalizationMiddleware()
NotFoundMiddleware()
}
router.get("hello") { _, _ in
"Hello!"
}
router.get("boom") { _, _ -> String in
throw HTTPError(.badRequest)
}
return router
}())
// MARK: Functional tests
@Test
func `renders the error page for an unmatched request`() async throws {
try await app.test(.router) { client in
try await client.execute(
uri: "/this-path-does-not-exist",
method: .get
) { response in
let body = String(buffer: response.body)
#expect(response.status == .notFound)
#expect(response.headers[.contentType] == "text/html; charset=utf-8")
#expect(response.headers[.contentLanguage] == "en")
#expect(response.headers[.vary] == "Accept-Language")
#expect(body.contains("Page Not Found"))
}
}
}
@Test
func `passes a matched response through untouched`() async throws {
try await app.test(.router) { client in
try await client.execute(
uri: "/hello",
method: .get
) { response in
#expect(response.status == .ok)
#expect(response.body == ByteBuffer(string: "Hello!"))
}
}
}
@Test
func `rethrows a non-not-found error unchanged`() async throws {
try await app.test(.router) { client in
try await client.execute(
uri: "/boom",
method: .get
) { response in
let body = String(buffer: response.body)
#expect(response.status == .badRequest)
#expect(!body.contains("Page Not Found"))
}
}
}
}