From 0e0847108ef4498afb62ab1cc8edae3be401b98c Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sat, 18 Mar 2023 16:52:59 +0000 Subject: [PATCH] Add more tests --- Tests/HummingbirdTests/RouterTests.swift | 35 ++++++++++++++++++++ Tests/HummingbirdTests/TrieRouterTests.swift | 2 ++ 2 files changed, 37 insertions(+) diff --git a/Tests/HummingbirdTests/RouterTests.swift b/Tests/HummingbirdTests/RouterTests.swift index 98ab1180c..366e44dc4 100644 --- a/Tests/HummingbirdTests/RouterTests.swift +++ b/Tests/HummingbirdTests/RouterTests.swift @@ -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) diff --git a/Tests/HummingbirdTests/TrieRouterTests.swift b/Tests/HummingbirdTests/TrieRouterTests.swift index 14e54caea..2b30ba38e 100644 --- a/Tests/HummingbirdTests/TrieRouterTests.swift +++ b/Tests/HummingbirdTests/TrieRouterTests.swift @@ -68,6 +68,7 @@ class TrieRouterTests: XCTestCase { func testRecursiveWildcardWithPrefix() { let trie = RouterPathTrie() 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") @@ -75,6 +76,7 @@ class TrieRouterTests: XCTestCase { 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() {