This PR contains the work done to support the static files serving for the **Website** service, and also included the essential boilerplate assets from the **HTML5 boilerplate** project. To provide further details about the work done: * Serving: Added the `FileMiddleware` middlewqare to the router; `path`, `server name`, and `log level` now read from config with defaults. * Library: Added the `StaticFile` enumeration with a `contentType` property, plus typed config-key/value constants and the `Configuration` dependency on WebsiteCore. * Assets: Added the **HTML5 boilerplate** (HTML, CSS, JS, icons, manifest, robots) to the *Resources/Static* folder. * Docker: Stage the Resources directory as read-only. * Tests: Added a test plan, and a shared Xcode scheme. Reviewed-on: rock-n-code/loud-amsterdam#3 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
142 lines
2.8 KiB
Swift
142 lines
2.8 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 == 9)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private extension StaticFileTests {
|
|
|
|
// MARK: Constants
|
|
|
|
static let contentTypes: [String] = [
|
|
"text/javascript",
|
|
"text/html",
|
|
"image/vnd.microsoft.icon",
|
|
"image/png",
|
|
"image/svg+xml",
|
|
"text/html",
|
|
"text/plain",
|
|
"application/manifest+json",
|
|
"text/css"
|
|
]
|
|
static let fileExtensions: [FileExtension] = [
|
|
.js,
|
|
.html,
|
|
.ico,
|
|
.png,
|
|
.svg,
|
|
.html,
|
|
.txt,
|
|
.webmanifest,
|
|
.css
|
|
]
|
|
static let fileNames: [String] = [
|
|
"app",
|
|
"404",
|
|
"favicon",
|
|
"icon",
|
|
"icon",
|
|
"index",
|
|
"robots",
|
|
"site",
|
|
"style"
|
|
]
|
|
static let relativePaths: [String] = [
|
|
"js/app.js",
|
|
"404.html",
|
|
"favicon.ico",
|
|
"icon.png",
|
|
"icon.svg",
|
|
"index.html",
|
|
"robots.txt",
|
|
"site.webmanifest",
|
|
"css/style.css"
|
|
]
|
|
|
|
}
|