Project updates from Template
This commit contains the latest updates from the generic Website template, which rework the compression and localization: - Reworked the compression and localization in the Infrastructure package. (3c568e4) - Adopted the reworked compression and localization in the Website service. (08cf3e3) The template commit that only touched the root README (53676ed) was left out, as this project no longer carries that file. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import CompressNIO
|
||||
import Configuration
|
||||
import Foundation
|
||||
import Hummingbird
|
||||
@@ -247,6 +248,51 @@ struct AppTests {
|
||||
}
|
||||
}
|
||||
|
||||
/// `CompressionMiddleware` stands in for Hummingbird's `ResponseCompressionMiddleware`, which appends to `Content-Encoding` without
|
||||
/// checking for one: a pre-compressed page would ship as `gzip, gzip`. Decoding makes it visible — a doubly compressed page decodes once,
|
||||
/// into bytes that are not HTML.
|
||||
@Test
|
||||
func `page to be served pre-compressed, and only once`() async throws {
|
||||
try await app(
|
||||
staticFilesPath: staticFilesPath
|
||||
).test(.router) { client in
|
||||
try await client.execute(
|
||||
uri: "/",
|
||||
method: .get,
|
||||
headers: [.acceptEncoding: "gzip, deflate, br"]
|
||||
) { response in
|
||||
#expect(response.status == .ok)
|
||||
#expect(response.headers[values: .contentEncoding] == ["gzip"])
|
||||
|
||||
var body = response.body
|
||||
let decoded = try body.decompress(with: .gzip())
|
||||
|
||||
#expect(String(buffer: decoded).hasPrefix("<!DOCTYPE html>"))
|
||||
// Complete before the first byte is written, so it is sized rather than chunked.
|
||||
#expect(response.headers[.contentLength] == String(response.body.readableBytes))
|
||||
#expect(response.body.readableBytes < decoded.readableBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The compressed copy is only handed to a client that asked for it; everyone else gets the rendered bytes.
|
||||
@Test
|
||||
func `page to be served uncompressed when gzip is not accepted`() async throws {
|
||||
try await app(
|
||||
staticFilesPath: staticFilesPath
|
||||
).test(.router) { client in
|
||||
try await client.execute(
|
||||
uri: "/",
|
||||
method: .get,
|
||||
headers: [.acceptEncoding: "identity"]
|
||||
) { response in
|
||||
#expect(response.status == .ok)
|
||||
#expect(response.headers[.contentEncoding] == nil)
|
||||
#expect(String(buffer: response.body).hasPrefix("<!DOCTYPE html>"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func `error page to be served when not found`() async throws {
|
||||
try await app(
|
||||
|
||||
Reference in New Issue
Block a user