Files
ccn/Services/Website/Tests/Library/Cases/Internal/Enumerations/StaticFileTests.swift
T
javier 847058d642 Optimizations for the Website service (#9)
This PR contains the work done to provide optimizations to the current service, such as a health-check endpoint, pre-renders static HTML pages, and hardens the error page's CSP.

To provide further details about the work:

* Added the `HealthController` controller serving GET `/health` with a static JSON payload.
* Added the `CachedHTMLResponse` response, which renders a static HTMLDocument to bytes once and reuses them per request (no Content-Length, so responses stay compressible).
* Integrated the response into the `RootController` and the  `NotFoundMiddleware` middleware to avoid re-rendering on hot paths.
* Added a `RouterMethods.addRoutes(_:)` extension and switched the router in App+build to use it.
* Moved the inline style from the `ErrorPage` page into a dedicated style file so the CSP needs no inline-style escape hatch.
* Fixed the `IndexPage` page path inconsistencies.
* Written the `README` file.

Reviewed-on: rock-n-code/loud-amsterdam#9
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2026-06-28 14:18:04 +00:00

138 lines
2.7 KiB
Swift

import Foundation
import Testing
@testable import WebsiteCore
@Suite("StaticFile enumeration")
struct StaticFileTests {
// MARK: Type aliases
typealias File = StaticFile
typealias FileExtension = StaticFile.Extension
// MARK: Computed tests
@Test(arguments: zip(
File.allCases,
Self.contentTypes
))
func `content type`(
for file: File,
expects contentType: String
) {
#expect(file.contentType == contentType)
}
@Test(arguments: zip(
File.allCases,
Self.fileExtensions
))
func `file extension`(
for file: File,
expects `extension`: FileExtension
) {
#expect(file.fileExtension == `extension`)
}
@Test(arguments: zip(
File.allCases,
Self.fileNames
))
func `file name`(
for file: File,
expects fileName: String
) {
#expect(file.fileName == fileName)
}
@Test(arguments: zip(
File.allCases,
Self.relativePaths
))
func `relative path`(
for file: File,
expects relativePath: String
) {
#expect(file.relativePath == relativePath)
}
// MARK: Method tests
@Test(arguments: [
"",
".",
"Resources/Static"
])
func `path relative to`(
_ basePath: String
) {
for file in File.allCases {
let pathRelativeToBasePath = file.path(relativeTo: basePath)
if basePath.isEmpty {
#expect(pathRelativeToBasePath == file.relativePath)
} else {
#expect(pathRelativeToBasePath == "\(basePath)/\(file.relativePath)")
}
}
}
// MARK: CaseIterable tests
@Test
func `all cases`() {
#expect(File.allCases.count == 8)
}
}
// MARK: - Helpers
private extension StaticFileTests {
// MARK: Constants
static let contentTypes: [String] = [
"text/javascript",
"text/css",
"image/vnd.microsoft.icon",
"image/png",
"image/svg+xml",
"text/plain",
"application/manifest+json",
"text/css"
]
static let fileExtensions: [FileExtension] = [
.js,
.css,
.ico,
.png,
.svg,
.txt,
.webmanifest,
.css
]
static let fileNames: [String] = [
"app",
"error",
"favicon",
"icon",
"icon",
"robots",
"site",
"style"
]
static let relativePaths: [String] = [
"js/app.js",
"css/error.css",
"favicon.ico",
"icon.png",
"icon.svg",
"robots.txt",
"site.webmanifest",
"css/style.css"
]
}