Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 18, 2023
1 parent e5789eb commit 0e08471
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Tests/HummingbirdTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,41 @@ final class RouterTests: XCTestCase {
}
}

func testPartialCapture() throws {
let app = HBApplication(testing: .embedded)
app.router
.get("/files/file.:ext:/:name:.jpg") { request -> String in
XCTAssertEqual(request.parameters.count, 2)
let ext = try request.parameters.require("ext")
let name = try request.parameters.require("name")
return "\(name).\(ext)"
}
try app.XCTStart()
defer { app.XCTStop() }

try app.XCTExecute(uri: "/files/file.doc/test.jpg", method: .GET) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), "test.doc")
}
}

func testPartialWildcard() throws {
let app = HBApplication(testing: .embedded)
app.router
.get("/files/file.*/*.jpg") { _ -> HTTPResponseStatus in
return .ok
}
try app.XCTStart()
defer { app.XCTStop() }

try app.XCTExecute(uri: "/files/file.doc/test.jpg", method: .GET) { response in
XCTAssertEqual(response.status, .ok)
}
try app.XCTExecute(uri: "/files/file.doc/test.png", method: .GET) { response in
XCTAssertEqual(response.status, .notFound)
}
}

/// Test we have a request id and that it increments with each request
func testRequestId() throws {
let app = HBApplication(testing: .embedded)
Expand Down
2 changes: 2 additions & 0 deletions Tests/HummingbirdTests/TrieRouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ class TrieRouterTests: XCTestCase {
func testRecursiveWildcardWithPrefix() {
let trie = RouterPathTrie<String>()
trie.addEntry("Test/**", value: "true")
trie.addEntry("Test2/:test/**", value: "true")
XCTAssertNil(trie.getValueAndParameters("/notTest/hello"))
XCTAssertNil(trie.getValueAndParameters("/Test/")?.value, "true")
XCTAssertEqual(trie.getValueAndParameters("/Test/one")?.value, "true")
XCTAssertEqual(trie.getValueAndParameters("/Test/one/two")?.value, "true")
XCTAssertEqual(trie.getValueAndParameters("/Test/one/two/three")?.value, "true")
XCTAssertEqual(trie.getValueAndParameters("/Test/")?.parameters?.getCatchAll(), nil)
XCTAssertEqual(trie.getValueAndParameters("/Test/one/two")?.parameters?.getCatchAll(), "one/two")
XCTAssertEqual(trie.getValueAndParameters("/Test2/one/two")?.parameters?.getCatchAll(), "two")
}

func testPrefixWildcard() {
Expand Down

0 comments on commit 0e08471

Please sign in to comment.