This PR contains the work done to rename the _Web_ package as _Infrastructure_, to provide a clear naming and purpose to this particular package within the project. To provide further details about the work: * Infrastructure * Asset fingerprinting: an FNV-1a token derived from the static files directory, appended as ?v= to asset URLs so deploys bust caches; pre-rendered pages also revalidate via weak ETags. * New middlewares: fixed-window RateLimitMiddleware (per-client budgets keyed by trusted X-Forwarded-For or remote address) and VaryMiddleware (Accept-Encoding on every response); SecurityHeadersMiddleware now also stamps error responses. * Auto-generated HEAD endpoints, cache max-age configuration, and Docker build/Compose refinements. * Protocols and scaffolding: Asset/AssetExtension, the Page protocol (viewport, stylesheets, scripts, versioned URLs), and LocalizedRequestContext. * Rate limiter's counter store swapped from an actor to a Mutex (no executor hop per request) with amortized batch eviction instead of O(n²) scans under client floods. * FingerprintAssets reports unreadable files to a logger instead of silently producing a token that never busts their cache. Reviewed-on: rock-n-code/loud-amsterdam#25 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
132 lines
3.7 KiB
Swift
132 lines
3.7 KiB
Swift
import HTTPTypes
|
|
import Hummingbird
|
|
import HummingbirdTesting
|
|
import Testing
|
|
|
|
@testable import Infrastructure
|
|
|
|
@Suite("VaryMiddleware middleware", .tags(.middleware))
|
|
struct VaryMiddlewareTests {
|
|
|
|
// MARK: Functional tests
|
|
|
|
@Test
|
|
func `adds the default field to a response without a vary header`() async throws {
|
|
try await app().test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/plain",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == .ok)
|
|
#expect(response.headers[.vary] == "Accept-Encoding")
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func `appends to an existing vary header`() async throws {
|
|
try await app().test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/localized",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.headers[.vary] == "Accept-Language, Accept-Encoding")
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func `does not duplicate a name already present`() async throws {
|
|
try await app().test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/encoded",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.headers[.vary] == "Accept-Encoding")
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func `matches an existing name regardless of its casing`() async throws {
|
|
try await app().test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/lowercased",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.headers[.vary] == "accept-encoding")
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func `normalizes the whitespace of an existing list`() async throws {
|
|
try await app().test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/spaced",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.headers[.vary] == "Accept-Language, User-Agent, Accept-Encoding")
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func `appends every configured field`() async throws {
|
|
try await app(
|
|
fields: [.acceptEncoding, .acceptLanguage]
|
|
).test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/plain",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.headers[.vary] == "Accept-Encoding, Accept-Language")
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private extension VaryMiddlewareTests {
|
|
|
|
// MARK: Methods
|
|
|
|
/// Builds an application whose router applies the vary middleware ahead of routes whose
|
|
/// responses carry different `Vary` starting points: `/plain` none, `/localized` an
|
|
/// `Accept-Language`, `/encoded` an `Accept-Encoding` already, `/lowercased` a lowercase
|
|
/// `accept-encoding`, and `/spaced` a list with irregular whitespace.
|
|
func app(
|
|
fields: [HTTPField.Name] = [.acceptEncoding]
|
|
) -> some ApplicationProtocol {
|
|
let router = Router()
|
|
|
|
router.addMiddleware {
|
|
VaryMiddleware(fields: fields)
|
|
}
|
|
|
|
router.get("plain") { _, _ in
|
|
"Hello!"
|
|
}
|
|
|
|
for (path, vary) in [
|
|
("localized", "Accept-Language"),
|
|
("encoded", "Accept-Encoding"),
|
|
("lowercased", "accept-encoding"),
|
|
("spaced", "Accept-Language , User-Agent"),
|
|
] {
|
|
router.get(RouterPath(path)) { _, _ -> Response in
|
|
var response = Response(status: .ok)
|
|
|
|
response.headers[.vary] = vary
|
|
|
|
return response
|
|
}
|
|
}
|
|
|
|
return Application(router: router)
|
|
}
|
|
|
|
}
|