import Hummingbird import HummingbirdTesting import NIOCore import Testing @testable import WebsiteCore @Suite("NotFoundMiddleware 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")) } } } }