Skip to content

Commit d8dc616

Browse files
authored
Merge pull request #479 from michaelenger/wildcard-end-route
Added support for using ** as a catch-all at the end of a route
2 parents 718f82c + 6739e9e commit d8dc616

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ All notable changes to this project will be documented in this file. Changes not
1919
# [Unreleased]
2020

2121
## Added
22-
22+
- Add support for using `**` as a catch-all at the end of a route. ([#479](https://github.com/httpswift/swifter/pull/479)) by [@michaelenger](https://github.com/michaelenger)
2323
- Set `Content-Type` to HttpBody and Text HttpResponse. ([#474](https://github.com/httpswift/swifter/pull/474)) by [@mtgto](https://github.com/mtgto)
2424

2525
## Fixed

Xcode/Sources/HttpRouter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ open class HttpRouter {
151151
}
152152

153153
if let startStarNode = node.nodes["**"] {
154+
if startStarNode.isEndOfRoute {
155+
// ** at the end of a route works as a catch-all
156+
matchedNodes.append(startStarNode)
157+
return
158+
}
159+
154160
let startStarNodeKeys = startStarNode.nodes.keys
155161
currentIndex += 1
156162
while currentIndex < count, let pathToken = pattern[currentIndex].removingPercentEncoding {

Xcode/Tests/SwifterTestsHttpRouter.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ class SwifterTestsHttpRouter: XCTestCase {
8585
XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
8686
}
8787

88+
func testHttpRouterMultiplePathSegmentWildcardTail() {
89+
90+
router.register(nil, path: "/a/b/**", handler: { _ in
91+
return .ok(.htmlBody("OK"))
92+
})
93+
94+
XCTAssertNil(router.route(nil, path: "/"))
95+
XCTAssertNil(router.route(nil, path: "/a"))
96+
XCTAssertNotNil(router.route(nil, path: "/a/b/c/d/e/f/g"))
97+
XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
98+
}
99+
88100
func testHttpRouterEmptyTail() {
89101

90102
router.register(nil, path: "/a/b/", handler: { _ in

0 commit comments

Comments
 (0)