|
| 1 | +import XCTest |
| 2 | +import MacroTestUtilities |
| 3 | +import class http.IncomingMessage |
| 4 | +import enum NIOHTTP1.HTTPMethod |
| 5 | +@testable import express |
| 6 | + |
| 7 | +final class ExactMatchTests: XCTestCase { |
| 8 | + |
| 9 | + // MARK: - Method routes do exact matching |
| 10 | + |
| 11 | + func testGetRootDoesNotMatchSubpath() throws { |
| 12 | + let route = Route(id: "root") |
| 13 | + var didCallRoute = false |
| 14 | + route.get("/") { _, _, _ in didCallRoute = true } |
| 15 | + |
| 16 | + let req = IncomingMessage(method: .GET, url: "/foo") |
| 17 | + let res = TestServerResponse() |
| 18 | + |
| 19 | + var didCallNext = false |
| 20 | + try route.handle(request: req, response: res) { ( args : Any... ) in |
| 21 | + didCallNext = true |
| 22 | + } |
| 23 | + XCTAssertFalse(didCallRoute, "should not match /foo") |
| 24 | + XCTAssertTrue(didCallNext, "should call next") |
| 25 | + } |
| 26 | + |
| 27 | + func testGetRootMatchesExactRoot() throws { |
| 28 | + let route = Route(id: "root") |
| 29 | + var didCallRoute = false |
| 30 | + route.get("/") { _, _, _ in didCallRoute = true } |
| 31 | + |
| 32 | + let req = IncomingMessage(method: .GET, url: "/") |
| 33 | + let res = TestServerResponse() |
| 34 | + |
| 35 | + var didCallNext = false |
| 36 | + try route.handle(request: req, response: res) { ( args : Any... ) in |
| 37 | + didCallNext = true |
| 38 | + } |
| 39 | + XCTAssertTrue(didCallRoute, "should match /") |
| 40 | + XCTAssertFalse(didCallNext, "should not call next") |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - use() still does prefix matching |
| 44 | + |
| 45 | + func testUseRootMatchesSubpath() throws { |
| 46 | + let route = Route(id: "root") |
| 47 | + var didCallRoute = false |
| 48 | + route.use("/") { _, _, _ in didCallRoute = true } |
| 49 | + |
| 50 | + let req = IncomingMessage(method: .GET, url: "/foo") |
| 51 | + let res = TestServerResponse() |
| 52 | + |
| 53 | + try route.handle(request: req, response: res) { ( args : Any... ) in } |
| 54 | + XCTAssertTrue(didCallRoute, "use('/') should match /foo (prefix)") |
| 55 | + } |
| 56 | + |
| 57 | + // MARK: - all() does exact matching |
| 58 | + |
| 59 | + func testAllRootDoesNotMatchSubpath() throws { |
| 60 | + let route = Route(id: "root") |
| 61 | + var didCallRoute = false |
| 62 | + route.all("/") { _, _, _ in didCallRoute = true } |
| 63 | + |
| 64 | + let req = IncomingMessage(method: .POST, url: "/foo") |
| 65 | + let res = TestServerResponse() |
| 66 | + |
| 67 | + var didCallNext = false |
| 68 | + try route.handle(request: req, response: res) { ( args : Any... ) in |
| 69 | + didCallNext = true |
| 70 | + } |
| 71 | + XCTAssertFalse(didCallRoute, "all('/') should not match /foo") |
| 72 | + XCTAssertTrue(didCallNext, "should call next") |
| 73 | + } |
| 74 | + |
| 75 | + func testAllRootMatchesExactRoot() throws { |
| 76 | + let route = Route(id: "root") |
| 77 | + var didCallRoute = false |
| 78 | + route.all("/") { _, _, _ in didCallRoute = true } |
| 79 | + |
| 80 | + let req = IncomingMessage(method: .POST, url: "/") |
| 81 | + let res = TestServerResponse() |
| 82 | + |
| 83 | + var didCallNext = false |
| 84 | + try route.handle(request: req, response: res) { ( args : Any... ) in |
| 85 | + didCallNext = true |
| 86 | + } |
| 87 | + XCTAssertTrue(didCallRoute, "all('/') should match /") |
| 88 | + XCTAssertFalse(didCallNext, "should not call next") |
| 89 | + } |
| 90 | + |
| 91 | + func testAllMatchesAnyMethod() throws { |
| 92 | + let route = Route(id: "root") |
| 93 | + var callCount = 0 |
| 94 | + route.all("/api") { _, _, _ in callCount += 1 } |
| 95 | + |
| 96 | + for method: HTTPMethod in [ .GET, .POST, .PUT, .DELETE ] { |
| 97 | + let req = IncomingMessage(method: method, url: "/api") |
| 98 | + let res = TestServerResponse() |
| 99 | + try route.handle(request: req, response: res) { _ in } |
| 100 | + } |
| 101 | + XCTAssertEqual(callCount, 4, "all should match every method") |
| 102 | + } |
| 103 | + |
| 104 | + func testAllWithWildcardMatchesSubpath() throws { |
| 105 | + let route = Route(id: "root") |
| 106 | + var didCallRoute = false |
| 107 | + route.all("/api/*") { _, _, _ in didCallRoute = true } |
| 108 | + |
| 109 | + let req = IncomingMessage(method: .GET, url: "/api/users/42") |
| 110 | + let res = TestServerResponse() |
| 111 | + |
| 112 | + try route.handle(request: req, response: res) { _ in } |
| 113 | + XCTAssertTrue(didCallRoute, "all with wildcard should match subpaths") |
| 114 | + } |
| 115 | + |
| 116 | + // MARK: - Wildcard still allows extra segments |
| 117 | + |
| 118 | + func testGetWildcardMatchesDeepPath() throws { |
| 119 | + let route = Route(id: "root") |
| 120 | + var didCallRoute = false |
| 121 | + route.get("/todos/*") { _, _, _ in didCallRoute = true } |
| 122 | + |
| 123 | + let req = IncomingMessage(method: .GET, url: "/todos/1/details") |
| 124 | + let res = TestServerResponse() |
| 125 | + |
| 126 | + try route.handle(request: req, response: res) { ( args : Any... ) in } |
| 127 | + XCTAssertTrue(didCallRoute, "wildcard should match extra segments") |
| 128 | + } |
| 129 | + |
| 130 | + // MARK: - Variable patterns exact match |
| 131 | + |
| 132 | + func testGetWithVariableDoesNotMatchExtra() throws { |
| 133 | + let route = Route(id: "root") |
| 134 | + var didCallRoute = false |
| 135 | + route.get("/users/:id") { _, _, _ in didCallRoute = true } |
| 136 | + |
| 137 | + let req = IncomingMessage(method: .GET, url: "/users/42/profile") |
| 138 | + let res = TestServerResponse() |
| 139 | + |
| 140 | + var didCallNext = false |
| 141 | + try route.handle(request: req, response: res) { ( args : Any... ) in |
| 142 | + didCallNext = true |
| 143 | + } |
| 144 | + XCTAssertFalse(didCallRoute, "should not match extra /profile") |
| 145 | + XCTAssertTrue(didCallNext, "should call next") |
| 146 | + } |
| 147 | + |
| 148 | + func testGetWithVariableMatchesExact() throws { |
| 149 | + let route = Route(id: "root") |
| 150 | + var didCallRoute = false |
| 151 | + route.get("/users/:id") { _, _, _ in didCallRoute = true } |
| 152 | + |
| 153 | + let req = IncomingMessage(method: .GET, url: "/users/42") |
| 154 | + let res = TestServerResponse() |
| 155 | + |
| 156 | + try route.handle(request: req, response: res) { ( args : Any... ) in } |
| 157 | + XCTAssertTrue(didCallRoute, "should match /users/42") |
| 158 | + } |
| 159 | + |
| 160 | + // MARK: - Mounted routes with exact matching |
| 161 | + |
| 162 | + func testMountedGetExactMatch() throws { |
| 163 | + let outerRoute = Route(id: "outer") |
| 164 | + var didCallRoute = false |
| 165 | + outerRoute.route("/admin") |
| 166 | + .get("/view") { _, _, _ in didCallRoute = true } |
| 167 | + |
| 168 | + let req = IncomingMessage(method: .GET, url: "/admin/view/extra") |
| 169 | + let res = TestServerResponse() |
| 170 | + |
| 171 | + var didCallNext = false |
| 172 | + try outerRoute.handle(request: req, response: res) { ( args : Any... ) in |
| 173 | + didCallNext = true |
| 174 | + } |
| 175 | + XCTAssertFalse(didCallRoute, "should not match /admin/view/extra") |
| 176 | + XCTAssertTrue(didCallNext, "should call next") |
| 177 | + } |
| 178 | + |
| 179 | + static var allTests = [ |
| 180 | + ( "testGetRootDoesNotMatchSubpath", testGetRootDoesNotMatchSubpath ), |
| 181 | + ( "testGetRootMatchesExactRoot", testGetRootMatchesExactRoot ), |
| 182 | + ( "testUseRootMatchesSubpath", testUseRootMatchesSubpath ), |
| 183 | + ( "testAllRootDoesNotMatchSubpath", testAllRootDoesNotMatchSubpath ), |
| 184 | + ( "testAllRootMatchesExactRoot", testAllRootMatchesExactRoot ), |
| 185 | + ( "testAllMatchesAnyMethod", testAllMatchesAnyMethod ), |
| 186 | + ( "testAllWithWildcardMatchesSubpath", testAllWithWildcardMatchesSubpath), |
| 187 | + ( "testGetWildcardMatchesDeepPath", testGetWildcardMatchesDeepPath ), |
| 188 | + ( "testGetWithVariableMatchesExact", testGetWithVariableMatchesExact ), |
| 189 | + ( "testMountedGetExactMatch", testMountedGetExactMatch ), |
| 190 | + ( "testGetWithVariableDoesNotMatchExtra", |
| 191 | + testGetWithVariableDoesNotMatchExtra ) |
| 192 | + ] |
| 193 | +} |
0 commit comments