Skip to content

Commit

Permalink
Fix build_mock_url() for requests with multipart body with string v…
Browse files Browse the repository at this point in the history
…alues
  • Loading branch information
jmaspons committed Apr 29, 2024
1 parent b1107e0 commit d446a73
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# httptest2 1.1.0.9000

* `build_mock_url()` now works with multipart body request containing strings (#40, @jmaspons)

# httptest2 1.1.0

* `request` is now removed when saving `httr2_response` objects. In earlier versions of `httr2`, requests were not included in responses, but in httr2 1.0.0, [they were added](https://github.com/r-lib/httr2/pull/359) in order to improve error messages. *If you recorded any responses with httr2 >= 1.0 and httptest2 prior to this version, you may have leaked auth secrets: this would happen if your requests included auth information (as in an `Authentication` header), and the response was saved in a .R file, not simplified to .json or other response-body-only formats. Please inspect your recorded responses and invalidate any tokens that were exposed.*
Expand Down
6 changes: 4 additions & 2 deletions R/build-mock-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ get_string_request_body <- function(req) {
if (inherits(x, "form_file")) {
# hash the file contents
paste("File:", digest(x$path, serialize = FALSE, file = TRUE))
} else {
# assume form_data
} else if (inherits(x, "form_data")){
rawToChar(x$value)
} else {
# assume string
x
}
})
b <- paste(c(
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-capture-requests.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
d <- tempfile()
dl_file <- tempfile()
webp_file <- tempfile()
file_path <- tempfile()
writeLines(letters[1:6], file_path)

# The webfake URL will be something like 127.0.0.1:port, and port may vary
# so the mock paths will be different every time it runs
Expand Down Expand Up @@ -31,6 +33,14 @@ test_that("We can record a series of requests (a few ways)", {
req_perform(path = webp_file)
r8 <<- request(httpbin$url("/status/202")) %>% req_perform()
r9 <<- request(httpbin$url("/status/200")) %>% req_perform()
r10 <<- request(httpbin$url("/post")) %>%
req_body_multipart(
file = curl::form_file(file_path),
string = "some text",
form_data = curl::form_data("form data")
) %>%
req_method("POST") %>%
req_perform()
stop_capturing()
})

Expand All @@ -41,6 +51,7 @@ test_that("We can record a series of requests (a few ways)", {
"httpbin.org/get.json",
"httpbin.org/image/webp.R", # Not a simplifiable format, so .R
"httpbin.org/image/webp.R-FILE", # The `write_disk` location
"httpbin.org/post-40b03d-POST.json",
"httpbin.org/put-PUT.json", # Not a GET, but returns 200
"httpbin.org/response-headers-ac4928.json",
"httpbin.org/status/200.txt", # empty 200 response "text/plain", so .txt
Expand Down Expand Up @@ -94,6 +105,14 @@ test_that("We can then load the mocks it stores", {
req_perform(path = mock_webp_file)
m8 <- request(httpbin$url("/status/202")) %>% req_perform()
m9 <- request(httpbin$url("/status/200")) %>% req_perform()
m10 <- request(httpbin$url("/post")) %>%
req_body_multipart(
file = curl::form_file(file_path),
string = "some text",
form_data = curl::form_data("form data")
) %>%
req_method("POST") %>%
req_perform()
})
})
expect_identical(resp_body_json(m1), resp_body_json(r1))
Expand All @@ -114,6 +133,7 @@ test_that("We can then load the mocks it stores", {
expect_equal(resp_status(m9), 200)
expect_equal(resp_content_type(m9), "text/plain")
expect_false(resp_has_body(m9))
expect_identical(resp_body_json(m10), resp_body_json(r10))
})

test_that("write_disk mocks can be reloaded even if the mock directory moves", {
Expand Down

0 comments on commit d446a73

Please sign in to comment.