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
|
|
|
|
|
import Logging
|
2026-06-27 12:16:50 +00:00
|
|
|
import NIOCore
|
2026-06-27 02:26:33 +00:00
|
|
|
import Testing
|
|
|
|
|
|
|
|
|
|
@testable import Website
|
2026-06-27 10:59:32 +00:00
|
|
|
@testable import WebsiteCore
|
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-06-28 05:49:32 +00:00
|
|
|
|
|
|
|
|
private let textExtensions: [StaticFile.Extension] = [
|
|
|
|
|
.css,
|
|
|
|
|
.js,
|
|
|
|
|
.txt
|
|
|
|
|
]
|
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 {
|
|
|
|
|
try await app.test(.router) { client in
|
|
|
|
|
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-06-27 10:59:32 +00:00
|
|
|
@Test(arguments: StaticFile.allCases)
|
|
|
|
|
func `static files to be served`(
|
|
|
|
|
staticFile file: StaticFile
|
|
|
|
|
) async throws {
|
2026-06-27 02:26:33 +00:00
|
|
|
try await app.test(.router) { client in
|
|
|
|
|
try await client.execute(
|
2026-06-27 10:59:32 +00:00
|
|
|
uri: "/\(file.relativePath)",
|
2026-06-27 02:26:33 +00:00
|
|
|
method: .get
|
|
|
|
|
) { response in
|
2026-06-27 10:59:32 +00:00
|
|
|
#expect(response.status == .ok)
|
|
|
|
|
#expect(response.headers[.contentType] == file.contentType)
|
2026-06-28 05:49:32 +00:00
|
|
|
|
|
|
|
|
let cacheControl = try #require(response.headers[.cacheControl])
|
|
|
|
|
|
|
|
|
|
#expect(cacheControl.contains("public") == true)
|
|
|
|
|
#expect(cacheControl.contains("max-age=") == true)
|
|
|
|
|
|
|
|
|
|
if textExtensions.contains(file.fileExtension) {
|
|
|
|
|
#expect(cacheControl.contains("must-revalidate") == true)
|
|
|
|
|
}
|
2026-06-27 02:26:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-27 10:59:32 +00:00
|
|
|
|
2026-06-27 12:16:50 +00:00
|
|
|
@Test
|
|
|
|
|
func `error page to be served when not found`() async throws {
|
|
|
|
|
try await app.test(.router) { client in
|
|
|
|
|
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-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: Computed
|
2026-06-27 12:16:50 +00:00
|
|
|
|
2026-06-28 05:07:19 +00:00
|
|
|
var app: some ApplicationProtocol {
|
|
|
|
|
get async {
|
|
|
|
|
await application(
|
|
|
|
|
reader: reader(
|
|
|
|
|
staticFilesPath: staticFilesPath
|
|
|
|
|
)
|
|
|
|
|
)
|
2026-06-27 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 05:07:19 +00:00
|
|
|
// MARK: Methods
|
|
|
|
|
|
2026-06-27 10:59:32 +00:00
|
|
|
func reader(
|
|
|
|
|
staticFilesPath: String
|
|
|
|
|
) -> ConfigReader {
|
|
|
|
|
ConfigReader(providers: [
|
|
|
|
|
InMemoryProvider(values: [
|
2026-06-28 05:07:19 +00:00
|
|
|
.HTTP.host: "127.0.0.1",
|
|
|
|
|
.HTTP.port: "0",
|
|
|
|
|
.Log.level: "trace",
|
2026-06-27 10:59:32 +00:00
|
|
|
.Path.staticFiles: .init(stringLiteral: staticFilesPath),
|
|
|
|
|
])
|
|
|
|
|
])
|
|
|
|
|
}
|
2026-06-27 12:16:50 +00:00
|
|
|
|
2026-06-27 02:26:33 +00:00
|
|
|
}
|