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 a small test for this behaviour.
  • Loading branch information
atheriel committed Oct 22, 2019
1 parent 52f2950 commit dc13db6
Show file tree
Hide file tree
Showing 3 changed files with 7 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/270) [#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
3 changes: 2 additions & 1 deletion R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ 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.
path <- sub("^/?(.*)([^/])$", "/\\1\\2/", path)

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

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

expect_length(pr$routes, 3)
expect_length(pr$routes, 4)
expect_s3_class(pr$mounts[["/static/"]], "plumberstatic")
expect_s3_class(pr$mounts[["/missing-slash/"]], "plumberstatic")
expect_s3_class(pr$mounts[["/mysubpath/"]], "plumber")
})

Expand Down

0 comments on commit dc13db6

Please sign in to comment.