Skip to content

Commit

Permalink
Merge pull request #479 from michaelenger/wildcard-end-route
Browse files Browse the repository at this point in the history
Added support for using ** as a catch-all at the end of a route
  • Loading branch information
Vkt0r authored Aug 31, 2021
2 parents 718f82c + 6739e9e commit d8dc616
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ All notable changes to this project will be documented in this file. Changes not
# [Unreleased]

## Added

- 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)
- Set `Content-Type` to HttpBody and Text HttpResponse. ([#474](https://github.com/httpswift/swifter/pull/474)) by [@mtgto](https://github.com/mtgto)

## Fixed
Expand Down
6 changes: 6 additions & 0 deletions Xcode/Sources/HttpRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ open class HttpRouter {
}

if let startStarNode = node.nodes["**"] {
if startStarNode.isEndOfRoute {
// ** at the end of a route works as a catch-all
matchedNodes.append(startStarNode)
return
}

let startStarNodeKeys = startStarNode.nodes.keys
currentIndex += 1
while currentIndex < count, let pathToken = pattern[currentIndex].removingPercentEncoding {
Expand Down
12 changes: 12 additions & 0 deletions Xcode/Tests/SwifterTestsHttpRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ class SwifterTestsHttpRouter: XCTestCase {
XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
}

func testHttpRouterMultiplePathSegmentWildcardTail() {

router.register(nil, path: "/a/b/**", handler: { _ in
return .ok(.htmlBody("OK"))
})

XCTAssertNil(router.route(nil, path: "/"))
XCTAssertNil(router.route(nil, path: "/a"))
XCTAssertNotNil(router.route(nil, path: "/a/b/c/d/e/f/g"))
XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
}

func testHttpRouterEmptyTail() {

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

0 comments on commit d8dc616

Please sign in to comment.