This PR contains the work done to replace the use of static _HTML_ files with type-safe HTML rendered server-side via **Elementary** through **Hummingbird**. To provide further details about the work done: * Added the **Elementary** dependencies. * Added the `IndexPage` and `ErrorPage` pages, ported from the old HTML boilerplate; removed the static files. * Added the `RootController` controller serving GET / using the `IndexPage` page, wired into the router. * Reworked the `NotFoundMiddleware` middleare to render `ErrorPage` page directly; non-notFound errors still propagate. * the `FileMiddleware` middleware no longer searches for any static `index.html` file. * Simplified the `StaticFile` enumeration, dropped unused constants and now-unnecessary throws. Reviewed-on: rock-n-code/loud-amsterdam#5 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
134 lines
2.7 KiB
Swift
134 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 == 7)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private extension StaticFileTests {
|
|
|
|
// MARK: Constants
|
|
|
|
static let contentTypes: [String] = [
|
|
"text/javascript",
|
|
"image/vnd.microsoft.icon",
|
|
"image/png",
|
|
"image/svg+xml",
|
|
"text/plain",
|
|
"application/manifest+json",
|
|
"text/css"
|
|
]
|
|
static let fileExtensions: [FileExtension] = [
|
|
.js,
|
|
.ico,
|
|
.png,
|
|
.svg,
|
|
.txt,
|
|
.webmanifest,
|
|
.css
|
|
]
|
|
static let fileNames: [String] = [
|
|
"app",
|
|
"favicon",
|
|
"icon",
|
|
"icon",
|
|
"robots",
|
|
"site",
|
|
"style"
|
|
]
|
|
static let relativePaths: [String] = [
|
|
"js/app.js",
|
|
"favicon.ico",
|
|
"icon.png",
|
|
"icon.svg",
|
|
"robots.txt",
|
|
"site.webmanifest",
|
|
"css/style.css"
|
|
]
|
|
|
|
}
|