Renamed the Web package as Infrastructure (#24)

Reviewed-on: rock-n-code/loud-amsterdam#24
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit is contained in:
2026-07-22 21:26:55 +00:00
committed by javier
parent 242cb92bc5
commit a868275347
15 changed files with 26 additions and 42 deletions
@@ -0,0 +1,156 @@
import Hummingbird
import HummingbirdTesting
import NIOCore
import Testing
@testable import Infrastructure
@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)
}
}
}
}
@@ -0,0 +1,30 @@
import Hummingbird
import Infrastructure
/// A controller serving its path back as plain text, used to observe route registration.
struct StubController {
// MARK: Properties
/// The path the controller serves, also returned as the response body.
let path: String
}
// MARK: - RouterController
extension StubController: RouterController {
// MARK: Properties
var routes: RouteCollection<BasicRequestContext> {
let routes = RouteCollection(context: BasicRequestContext.self)
routes.get(.init(path)) { _, _ in
self.path
}
return routes
}
}