44 lines
1.5 KiB
Swift
44 lines
1.5 KiB
Swift
import Hummingbird
|
|
import HummingbirdTesting
|
|
import Infrastructure
|
|
import Testing
|
|
|
|
@testable import WebsiteLibrary
|
|
|
|
@Suite(
|
|
"NoIndexMiddleware middleware",
|
|
.tags(.type)
|
|
)
|
|
struct NoIndexMiddlewareTests {
|
|
|
|
// MARK: Functional tests
|
|
|
|
@Test
|
|
func `marks what is served, what redirects, and what fails`() async throws {
|
|
let router = Router(context: WebsiteRequestContext.self)
|
|
|
|
router.add(middleware: NoIndexMiddleware())
|
|
router.get("/page") { _, _ in "A page" }
|
|
router.get("/moved") { _, _ in Response.redirect(to: "/page") }
|
|
router.get("/forbidden") { _, _ -> String in throw HTTPError(.forbidden) }
|
|
router.get("/marked") { _, _ in
|
|
Response(status: .ok, headers: [.robotsTag: "noindex"])
|
|
}
|
|
|
|
try await Application(router: router).test(.router) { client in
|
|
for (uri, status) in [("/page", HTTPResponse.Status.ok), ("/moved", .seeOther), ("/forbidden", .forbidden), ("/missing", .notFound)] {
|
|
try await client.execute(uri: uri, method: .get) { response in
|
|
#expect(response.status == status)
|
|
#expect(response.headers[.robotsTag] == "noindex, nofollow")
|
|
}
|
|
}
|
|
|
|
// One mark, not two: a response that came marked is marked over.
|
|
try await client.execute(uri: "/marked", method: .get) { response in
|
|
#expect(response.headers[values: .robotsTag] == ["noindex, nofollow"])
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|