Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New server option: decode_url #107

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!"
)
})
Loading