Skip to content

Commit

Permalink
Ensures that mount()'d routes have a leading slash.
Browse files Browse the repository at this point in the history
This also adds some small tests for correct handling of both leading and
trailing slashes in mount paths.

Co-authored-by: Winston Chang <winston@stdout.org>
  • Loading branch information
atheriel and wch committed Nov 1, 2019
1 parent 52f2950 commit 2d5fb16
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ plumber 0.5.0

### Bug fixes

* Fix possible bugs due to mounted routers without leading slashes (@atheriel, [#476](https://github.com/trestletech/plumber/issues/476) [#501](https://github.com/trestletech/plumber/pull/501)).

* Fix bug preventing error handling when a serializer fails (@antoine-sachet, [#490](https://github.com/rstudio/plumber/pull/490))

* Fix URL-decoding of query parameters and URL-encoding/decoding of cookies. Both now use `httpuv::decodeURIComponent` instead of `httpuv::decodeURI`. (@antoine-sachet, [#462](https://github.com/trestletech/plumber/pull/462))
Expand Down
8 changes: 7 additions & 1 deletion R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,13 @@ plumber <- R6Class(
httpuv::runServer(host, port, self)
},
mount = function(path, router){
path <- sub("([^/])$", "\\1/", path)
# Ensure that the path has both a leading and trailing slash.
if (!startsWith(path, "/")) {
path <- paste0("/", path)
}
if (!endsWith(path, "/")) {
path <- paste0(path, "/")
}

private$mnts[[path]] <- router
},
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,17 @@ test_that("mounts can be read correctly", {

stat <- PlumberStatic$new(".")
pr$mount("/static", stat)
pr$mount("missing-slashes", stat)
pr$mount("/both-slashes/", stat)
pr$mount("trailing-slash/", stat)
pr$mount("/extra-slash//", stat)

expect_length(pr$routes, 3)
expect_length(pr$routes, 7)
expect_s3_class(pr$mounts[["/static/"]], "plumberstatic")
expect_s3_class(pr$mounts[["/missing-slashes/"]], "plumberstatic")
expect_s3_class(pr$mounts[["/both-slashes/"]], "plumberstatic")
expect_s3_class(pr$mounts[["/trailing-slash/"]], "plumberstatic")
expect_s3_class(pr$mounts[["/extra-slash//"]], "plumberstatic")
expect_s3_class(pr$mounts[["/mysubpath/"]], "plumber")
})

Expand Down

0 comments on commit 2d5fb16

Please sign in to comment.