Skip to content

Pass URL encoded string as parameter in path #106

@alexverse

Description

@alexverse

I want to test an endpoint that accepts URL-encoded file paths ("/hello/:file_path"). However, the app$get() method throws a 404-error.

I'm also sharing a real-world example from the GitLab API, where the file_path parameter is URL encoded.

library(testthat)
library(webfakes)
library(urltools)
library(httr2, warn.conflicts = FALSE)
library(glue)

app <- webfakes::new_app()
app$get("/hello/:file_path", function(req, res) {
  res$send(paste0("Return content of ", req$params$file_path, "!"))
})
web <- webfakes::local_app_process(app)

test_that("can use hello API", {
  url <- web$url("/hello")
  file_path <- urltools::url_encode("path/to/file.json")

  #This works OK
  # file_path <- "foo"
 
  response <- httr2::request(url) %>%
    httr2::req_url_path_append(file_path) %>%
    httr2::req_perform() %>%
    httr2::resp_body_string()
  
  expected_resp <- glue::glue("Return content of {file_path}!")
  expect_equal(response, expected_resp)
})
#> ── Error: can use hello API ────────────────────────────────────────────────────
#> <httr2_http_404/httr2_http/httr2_error/rlang_error/error/condition>
#> Error in `httr2::req_perform(.)`: HTTP 404 Not Found.
#> Backtrace:
#>     ▆
#>  1. ├─... %>% httr2::resp_body_string()
#>  2. ├─httr2::resp_body_string(.)
#>  3. │ └─httr2:::check_response(resp)
#>  4. │   └─httr2:::is_response(resp)
#>  5. └─httr2::req_perform(.)
#>  6.   └─httr2:::handle_resp(req, resp, error_call = error_call)
#>  7.     └─httr2:::resp_abort(resp, req, body, call = error_call)
#>  8.       └─rlang::abort(...)
#> Error:
#> ! Test failed

Created on 2025-01-09 with reprex v2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions