2026-06-27 02:26:33 +00:00
|
|
|
import Configuration
|
2026-06-27 10:59:32 +00:00
|
|
|
import Foundation
|
2026-06-27 02:26:33 +00:00
|
|
|
import Hummingbird
|
|
|
|
|
import HummingbirdTesting
|
2026-07-23 01:04:37 +00:00
|
|
|
import Infrastructure
|
2026-06-27 12:16:50 +00:00
|
|
|
import NIOCore
|
2026-06-27 02:26:33 +00:00
|
|
|
import Testing
|
|
|
|
|
|
|
|
|
|
@testable import Website
|
2026-07-11 09:41:21 +00:00
|
|
|
@testable import WebsiteLibrary
|
2026-06-27 02:26:33 +00:00
|
|
|
|
2026-06-27 10:59:32 +00:00
|
|
|
@Suite("App executable")
|
2026-06-27 02:26:33 +00:00
|
|
|
struct AppTests {
|
2026-06-27 10:59:32 +00:00
|
|
|
|
|
|
|
|
// MARK: Constants
|
2026-07-23 01:04:37 +00:00
|
|
|
|
|
|
|
|
// Stylesheets and scripts are referenced through fingerprinted URLs, so they are served immutable.
|
|
|
|
|
private let immutableExtensions: [AssetExtension] = [
|
2026-06-28 05:49:32 +00:00
|
|
|
.css,
|
2026-07-23 01:04:37 +00:00
|
|
|
.js
|
2026-06-28 05:49:32 +00:00
|
|
|
]
|
2026-06-27 10:59:32 +00:00
|
|
|
|
|
|
|
|
// Absolute path to the package's "Resources/Static" folder, derived from this
|
|
|
|
|
// file's location so the static files resolve regardless of the working directory.
|
|
|
|
|
private let staticFilesPath = URL(fileURLWithPath: #filePath)
|
|
|
|
|
.deletingLastPathComponent() // Tests/App
|
|
|
|
|
.deletingLastPathComponent() // Tests
|
|
|
|
|
.deletingLastPathComponent() // package root
|
|
|
|
|
.appendingPathComponent(.Path.staticResources)
|
|
|
|
|
.path
|
2026-06-27 12:16:50 +00:00
|
|
|
|
2026-06-27 10:59:32 +00:00
|
|
|
// MARK: Functional tests
|
|
|
|
|
|
2026-06-27 12:16:50 +00:00
|
|
|
@Test
|
|
|
|
|
func `landing page to be served at root`() async throws {
|
2026-06-28 11:35:54 +00:00
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
2026-06-27 12:16:50 +00:00
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
2026-06-28 05:07:19 +00:00
|
|
|
let body = String(buffer: response.body)
|
|
|
|
|
|
2026-06-27 12:16:50 +00:00
|
|
|
#expect(response.status == .ok)
|
2026-06-28 05:49:32 +00:00
|
|
|
#expect(response.headers[.contentType] == "text/html; charset=utf-8")
|
2026-06-28 05:07:19 +00:00
|
|
|
#expect(body.contains("Hello world!"))
|
2026-06-27 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 01:04:37 +00:00
|
|
|
@Test
|
|
|
|
|
func `landing page to answer a head request`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .head
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.contentType] == "text/html; charset=utf-8")
|
|
|
|
|
#expect(response.body.readableBytes == 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-11 09:15:58 +00:00
|
|
|
@Test
|
|
|
|
|
func `health check to be served at the health path`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/health",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
let body = String(buffer: response.body)
|
|
|
|
|
|
|
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.contentType] == "application/json")
|
|
|
|
|
#expect(body == #"{"status":"ok"}"#)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `readiness check to be served at the readiness path`() async throws {
|
|
|
|
|
// Live mode runs the application's service group, so the `Fluent` service starts before the
|
|
|
|
|
// request and shuts its connection pool down after — the router-only mode never would.
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.live) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/health/ready",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
let body = String(buffer: response.body)
|
|
|
|
|
|
|
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.contentType] == "application/json")
|
|
|
|
|
#expect(body == #"{"status":"ready"}"#)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-27 10:59:32 +00:00
|
|
|
@Test(arguments: StaticFile.allCases)
|
|
|
|
|
func `static files to be served`(
|
|
|
|
|
staticFile file: StaticFile
|
|
|
|
|
) async throws {
|
2026-06-28 11:35:54 +00:00
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
2026-07-19 02:08:59 +00:00
|
|
|
for fileExtension in file.fileExtensions {
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/\(file.relativePath(for: fileExtension))",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.contentType] == fileExtension.contentType)
|
2026-06-28 05:49:32 +00:00
|
|
|
|
2026-07-19 02:08:59 +00:00
|
|
|
let cacheControl = try #require(response.headers[.cacheControl])
|
|
|
|
|
|
|
|
|
|
#expect(cacheControl.contains("public") == true)
|
|
|
|
|
#expect(cacheControl.contains("max-age=") == true)
|
|
|
|
|
|
2026-07-23 01:04:37 +00:00
|
|
|
if immutableExtensions.contains(fileExtension) {
|
|
|
|
|
#expect(cacheControl.contains("immutable") == true)
|
|
|
|
|
} else if fileExtension == .txt {
|
2026-07-19 02:08:59 +00:00
|
|
|
#expect(cacheControl.contains("must-revalidate") == true)
|
|
|
|
|
}
|
2026-06-28 05:49:32 +00:00
|
|
|
}
|
2026-06-27 02:26:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-27 10:59:32 +00:00
|
|
|
|
2026-07-23 01:04:37 +00:00
|
|
|
@Test
|
|
|
|
|
func `versioned asset URL to be served`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/css/shared.css?v=0123456789abcdef",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.contentType] == "text/css")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `landing page to reference fingerprinted assets`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
let body = String(buffer: response.body)
|
|
|
|
|
|
|
|
|
|
#expect(body.contains("/css/shared.css?v="))
|
|
|
|
|
#expect(body.contains("/js/shared.js?v="))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `landing page to revalidate with an entity tag`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
let eTag = try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.headers[.cacheControl] == "public, no-cache")
|
|
|
|
|
|
|
|
|
|
return try #require(response.headers[.eTag])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get,
|
|
|
|
|
headers: [.ifNoneMatch: eTag]
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .notModified)
|
|
|
|
|
#expect(response.body.readableBytes == 0)
|
|
|
|
|
#expect(response.headers[.eTag] == eTag)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `responses to vary on language and encoding`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
let vary = try #require(response.headers[.vary])
|
|
|
|
|
|
|
|
|
|
#expect(vary.contains("Accept-Language"))
|
|
|
|
|
#expect(vary.contains("Accept-Encoding"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 06:15:05 +00:00
|
|
|
@Test
|
|
|
|
|
func `response to be compressed when the client supports it`() async throws {
|
2026-06-28 11:35:54 +00:00
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
2026-06-28 06:15:05 +00:00
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get,
|
|
|
|
|
headers: [.acceptEncoding: "gzip"]
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.contentEncoding] == "gzip")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `response to not be compressed when the client does not support it`() async throws {
|
2026-06-28 11:35:54 +00:00
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
2026-06-28 06:15:05 +00:00
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.contentEncoding] == nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-27 12:16:50 +00:00
|
|
|
@Test
|
|
|
|
|
func `error page to be served when not found`() async throws {
|
2026-06-28 11:35:54 +00:00
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
2026-06-27 12:16:50 +00:00
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/this-path-does-not-exist",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
2026-06-28 05:07:19 +00:00
|
|
|
let body = String(buffer: response.body)
|
|
|
|
|
|
2026-06-27 12:16:50 +00:00
|
|
|
#expect(response.status == .notFound)
|
2026-06-28 05:49:32 +00:00
|
|
|
#expect(response.headers[.contentType] == "text/html; charset=utf-8")
|
2026-06-28 05:07:19 +00:00
|
|
|
#expect(body.contains("Page Not Found"))
|
2026-06-27 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 01:04:37 +00:00
|
|
|
@Test
|
|
|
|
|
func `error page to reference fingerprinted assets`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/this-path-does-not-exist",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
let body = String(buffer: response.body)
|
|
|
|
|
|
2026-08-01 23:07:04 +02:00
|
|
|
#expect(body.contains("/css/not-found.css?v="))
|
2026-07-23 01:04:37 +00:00
|
|
|
#expect(body.contains("/js/shared.js?v="))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `error page to be served without revalidation headers`() async throws {
|
|
|
|
|
// A `304 Not Modified` only ever stands in for a success, so the error page must not
|
|
|
|
|
// invite revalidation with an entity tag or a cache policy.
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/this-path-does-not-exist",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .notFound)
|
|
|
|
|
#expect(response.headers[.eTag] == nil)
|
|
|
|
|
#expect(response.headers[.cacheControl] == nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `error page to vary on language and encoding`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/this-path-does-not-exist",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
let vary = try #require(response.headers[.vary])
|
|
|
|
|
|
|
|
|
|
#expect(vary.contains("Accept-Language"))
|
|
|
|
|
#expect(vary.contains("Accept-Encoding"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `landing page to revalidate a conditional head request`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
let eTag = try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
try #require(response.headers[.eTag])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .head,
|
|
|
|
|
headers: [.ifNoneMatch: eTag]
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .notModified)
|
|
|
|
|
#expect(response.body.readableBytes == 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 11:35:54 +00:00
|
|
|
@Test
|
|
|
|
|
func `security headers to be applied to the landing page`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.contentSecurityPolicy] == String.Security.contentSecurityPolicy)
|
|
|
|
|
#expect(response.headers[.xContentTypeOptions] == String.Security.contentTypeOptions)
|
|
|
|
|
#expect(response.headers[.frameOptions] == String.Security.frameOptions)
|
|
|
|
|
#expect(response.headers[.referrerPolicy] == String.Security.referrerPolicy)
|
|
|
|
|
#expect(response.headers[.permissionsPolicy] == String.Security.permissionsPolicy)
|
|
|
|
|
#expect(response.headers[.strictTransportSecurity] == nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `security headers to be applied to the error page`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/this-path-does-not-exist",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .notFound)
|
|
|
|
|
#expect(response.headers[.contentSecurityPolicy] == String.Security.contentSecurityPolicy)
|
|
|
|
|
#expect(response.headers[.xContentTypeOptions] == String.Security.contentTypeOptions)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func `strict-transport-security to be applied when configured`() async throws {
|
|
|
|
|
try await app(
|
|
|
|
|
staticFilesPath: staticFilesPath,
|
|
|
|
|
strictTransportSecurity: "max-age=31536000; includeSubDomains"
|
|
|
|
|
).test(.router) { client in
|
|
|
|
|
try await client.execute(
|
|
|
|
|
uri: "/",
|
|
|
|
|
method: .get
|
|
|
|
|
) { response in
|
|
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.strictTransportSecurity] == "max-age=31536000; includeSubDomains")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-27 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
|
|
private extension AppTests {
|
2026-06-27 12:16:50 +00:00
|
|
|
|
2026-06-28 05:07:19 +00:00
|
|
|
// MARK: Methods
|
|
|
|
|
|
2026-06-28 11:35:54 +00:00
|
|
|
func app(
|
|
|
|
|
staticFilesPath: String,
|
|
|
|
|
strictTransportSecurity: String? = nil
|
2026-08-04 16:17:32 +02:00
|
|
|
) async throws -> some ApplicationProtocol {
|
|
|
|
|
try await application(
|
2026-06-28 11:35:54 +00:00
|
|
|
reader: reader(
|
|
|
|
|
staticFilesPath: staticFilesPath,
|
|
|
|
|
strictTransportSecurity: strictTransportSecurity
|
|
|
|
|
)
|
|
|
|
|
)
|
2026-06-27 10:59:32 +00:00
|
|
|
}
|
2026-06-27 12:16:50 +00:00
|
|
|
|
2026-06-28 11:35:54 +00:00
|
|
|
func reader(
|
|
|
|
|
staticFilesPath: String,
|
|
|
|
|
strictTransportSecurity: String? = nil
|
|
|
|
|
) -> ConfigReader {
|
|
|
|
|
ConfigReader(providers: [{
|
|
|
|
|
if let strictTransportSecurity {
|
|
|
|
|
InMemoryProvider(values: [
|
|
|
|
|
.HTTP.host: "127.0.0.1",
|
|
|
|
|
.HTTP.port: "0",
|
|
|
|
|
.Log.level: "trace",
|
|
|
|
|
.Path.staticFiles: .init(stringLiteral: staticFilesPath),
|
|
|
|
|
.Security.strictTransportSecurity: .init(stringLiteral: strictTransportSecurity)
|
|
|
|
|
])
|
|
|
|
|
} else {
|
|
|
|
|
InMemoryProvider(values: [
|
|
|
|
|
.HTTP.host: "127.0.0.1",
|
|
|
|
|
.HTTP.port: "0",
|
|
|
|
|
.Log.level: "trace",
|
|
|
|
|
.Path.staticFiles: .init(stringLiteral: staticFilesPath),
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
}()])
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-27 02:26:33 +00:00
|
|
|
}
|