Skip to content

Commit

Permalink
Merge pull request #107 from r-lib/feature/decode-url
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi authored Jan 10, 2025
2 parents 0ec7a0e + f4fca40 commit dd62a8f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ Config/Needs/website: tidyverse/tidytemplate
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# webfakes (development version)

* New server option: `decode_url`. If set to `FALSE`, then the web server
will not URL-decode the URL (#106).

# webfakes 1.3.1

No changes.
Expand Down
10 changes: 8 additions & 2 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ server_start <- function(opts = server_opts()) {

# These are not configurable currently
"request_timeout_ms" = "100000",
"enable_auth_domain_check" = "no"
"enable_auth_domain_check" = "no",
"decode_url" = if (opts$decode_url) "yes" else "no"
)

srv <- call_with_cleanup(c_server_start, options)
Expand Down Expand Up @@ -59,6 +60,10 @@ server_start <- function(opts = server_opts()) {
#' @param throttle Limit download speed for clients. If not `Inf`,
#' then it is the maximum number of bytes per second, that is sent to
#' as connection.
#' @param decode_url Whether the server should automatically decode
#' URL-encodded URLs. If `TRUE` (the default), `/foo%2fbar` will be
#' converted to `/foo/bar` automatically. If `FALSE`, URLs as not
#' URL-decoded.
#' @return List of options that can be passed to `webfakes_app$listen()`
#' (see [new_app()]), and [new_app_process()].
#'
Expand Down Expand Up @@ -87,7 +92,8 @@ server_opts <- function(remote = FALSE, port = NULL, num_threads = 1,
access_log_file = remote,
error_log_file = TRUE,
tcp_nodelay = FALSE,
throttle = Inf) {
throttle = Inf,
decode_url = TRUE) {

log_dir <- Sys.getenv("WEBFAKES_LOG_DIR", file.path(tempdir(), "webfakes"))
if (isTRUE(access_log_file)) {
Expand Down
8 changes: 7 additions & 1 deletion man/server_opts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions tests/testthat/test-decode-url.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

test_that("decode_url option", {
app <- webfakes::new_app()
app$get(
webfakes::new_regexp("^/hello/(?<path>.*)$"),
function(req, res) {
res$send(paste0("Return content of ", req$params$path, "!"))
}
)
web <- webfakes::local_app_process(app)

resp <- curl::curl_fetch_memory(web$url("/hello/foo%2fbar/suffix"))
expect_equal(resp$status_code, 200L)
expect_equal(
rawToChar(resp$content),
"Return content of foo/bar/suffix!"
)

app <- webfakes::new_app()
app$get(
webfakes::new_regexp("^/hello/(?<path>.*)$"),
function(req, res) {
res$send(paste0("Return content of ", req$params$path, "!"))
}
)
web <- webfakes::local_app_process(
app,
opts = server_opts(remote = TRUE, decode_url = FALSE)
)

resp <- curl::curl_fetch_memory(web$url("/hello/foo%2fbar/suffix"))
expect_equal(resp$status_code, 200L)
expect_equal(
rawToChar(resp$content),
"Return content of foo%2fbar/suffix!"
)
})

0 comments on commit dd62a8f

Please sign in to comment.