160 lines
4.2 KiB
Swift
160 lines
4.2 KiB
Swift
import Hummingbird
|
|
import HummingbirdTesting
|
|
import NIOCore
|
|
import Testing
|
|
|
|
@testable import Infrastructure
|
|
|
|
@Suite(
|
|
"addController method",
|
|
.tags(.`extension`)
|
|
)
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|