This PR contains the work done to introduce a new Web Swift package that provides a reusable, declarative way to register route controllers on a Hummingbird router, then adopt it in the Website service.
To provide further details about the work:
* Web package
* The `RouterController` protocol — a Sendable protocol to group controllers behind one `routes` property.
* The `RouteCollectionBuilder` — a result builder that collects controllers' route collections into a stack, with full support for optionals, conditionals, and arrays.
* The `addController(_:)` method — a `RouterMethods` extension letting controllers be listed declaratively and adding each one's routes at the router root.
* Website service
* Added Web as a dependency of the _WebsiteLibrary_ target.
* Conformed the `RootController` and `HealthController`controllers to the `RouterController` protocol.
* Updated the `App+Build` extension to to use the cleaner `router.addController { … }` instead.
Reviewed-on: rock-n-code/loud-amsterdam#17
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
156 lines
4.2 KiB
Swift
156 lines
4.2 KiB
Swift
import Hummingbird
|
|
import HummingbirdTesting
|
|
import Testing
|
|
|
|
@testable import Web
|
|
|
|
@Suite("addController method")
|
|
struct RouterMethodsTests {
|
|
|
|
// MARK: Functional tests
|
|
|
|
@Test
|
|
func `adds the routes of every listed controller`() async throws {
|
|
let router = Router()
|
|
|
|
router.addController {
|
|
StubController(path: "first")
|
|
StubController(path: "second")
|
|
}
|
|
|
|
try await Application(router: router).test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/first",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == .ok)
|
|
#expect(String(buffer: response.body) == "first")
|
|
}
|
|
|
|
try await client.execute(
|
|
uri: "/second",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == .ok)
|
|
#expect(String(buffer: response.body) == "second")
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test(arguments: [true, false])
|
|
func `adds a controller behind a condition only when the condition holds`(
|
|
condition: Bool
|
|
) async throws {
|
|
let router = Router()
|
|
|
|
router.addController {
|
|
StubController(path: "always")
|
|
|
|
if condition {
|
|
StubController(path: "conditional")
|
|
}
|
|
}
|
|
|
|
try await Application(router: router).test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/always",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == .ok)
|
|
}
|
|
|
|
try await client.execute(
|
|
uri: "/conditional",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == (condition ? .ok : .notFound))
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test(arguments: [true, false])
|
|
func `adds only the taken branch of a condition`(
|
|
takesFirst: Bool
|
|
) async throws {
|
|
let router = Router()
|
|
|
|
router.addController {
|
|
if takesFirst {
|
|
StubController(path: "first")
|
|
} else {
|
|
StubController(path: "second")
|
|
}
|
|
}
|
|
|
|
try await Application(router: router).test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/first",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == (takesFirst ? .ok : .notFound))
|
|
}
|
|
|
|
try await client.execute(
|
|
uri: "/second",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == (takesFirst ? .notFound : .ok))
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func `adds a controller for every iteration of a loop`() async throws {
|
|
let paths = ["one", "two", "three"]
|
|
let router = Router()
|
|
|
|
router.addController {
|
|
for path in paths {
|
|
StubController(path: path)
|
|
}
|
|
}
|
|
|
|
try await Application(router: router).test(.router) { client in
|
|
for path in paths {
|
|
try await client.execute(
|
|
uri: "/\(path)",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == .ok)
|
|
#expect(String(buffer: response.body) == path)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func `returns the router so calls can be chained`() async throws {
|
|
let router = Router()
|
|
|
|
router
|
|
.addController {
|
|
StubController(path: "first")
|
|
}
|
|
.addController {
|
|
StubController(path: "second")
|
|
}
|
|
|
|
try await Application(router: router).test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/first",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == .ok)
|
|
}
|
|
|
|
try await client.execute(
|
|
uri: "/second",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == .ok)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|