Skip to content

Commit

Permalink
Add case-insensitive routing (#2354)
Browse files Browse the repository at this point in the history
* * Adds Application.Routes.caseInsensitive to allow case insensitive routes (defaults to false)
* DefaultResponder will configure TrieRouter appropriately if set to true
* Appropriate test

* Docblock

Co-authored-by: Tanner <tannernelson@gmail.com>
  • Loading branch information
tdotclare and tanner0101 committed Jun 24, 2020
1 parent 2d8a8c8 commit 9d57bde
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Sources/Vapor/Responder/DefaultResponder.swift
Expand Up @@ -12,7 +12,10 @@ internal struct DefaultResponder: Responder {

/// Creates a new `ApplicationResponder`
public init(routes: Routes, middleware: [Middleware] = []) {
let router = TrieRouter(CachedRoute.self)
let options = routes.caseInsenstive ?
Set(arrayLiteral: TrieRouter<CachedRoute>.ConfigurationOption.caseInsensitive) : []
let router = TrieRouter(CachedRoute.self, options: options)

for route in routes.all {
// Make a copy of the route to cache middleware chaining.
let cached = CachedRoute(
Expand Down
4 changes: 4 additions & 0 deletions Sources/Vapor/Routing/Routes.swift
Expand Up @@ -3,6 +3,9 @@ public final class Routes: RoutesBuilder, CustomStringConvertible {

/// Default value used by `HTTPBodyStreamStrategy.collect` when `maxSize` is `nil`.
public var defaultMaxBodySize: ByteCount
/// Default routing behavior of `DefaultResponder` is case-sensitive; configure to `true` prior to
/// Application start handle `Constant` `PathComponents` in a case-insensitive manner.
public var caseInsenstive: Bool

public var description: String {
return self.all.description
Expand All @@ -11,6 +14,7 @@ public final class Routes: RoutesBuilder, CustomStringConvertible {
public init() {
self.all = []
self.defaultMaxBodySize = "16kb"
self.caseInsenstive = false
}

public func add(_ route: Route) {
Expand Down
19 changes: 19 additions & 0 deletions Tests/VaporTests/RouteTests.swift
Expand Up @@ -56,6 +56,25 @@ final class RouteTests: XCTestCase {
XCTAssertEqual(res.body.string, "foo")
}
}

func testInsensitiveRoutes() throws {
let app = Application(.testing)
defer { app.shutdown() }

app.routes.caseInsenstive = true

app.routes.get("foo") { req -> String in
return "foo"
}

try app.testable().test(.GET, "/foo") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "foo")
}.test(.GET, "/FOO") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "foo")
}
}

func testAnyResponse() throws {
let app = Application(.testing)
Expand Down

0 comments on commit 9d57bde

Please sign in to comment.