nealrichardson / httptest2

Utilities for testing R 📦s that use httr2
https://enpiar.com/httptest2/
Other
26 stars 6 forks source link

Mock file path not altered #19

Closed mpadge closed 2 years ago

mpadge commented 2 years ago

First off @nealrichardson, thanks so much for this awesome package! I've just started switching all my pkgs over to httr2 + httptest2, and this package make mock testing so easy. Awesome work!

Now to business: The following reprex demonstrates that, although gsub_response() should also modify the request via #8, the file path in httr::req_body_file() is not modified:

f <- tempfile ("data-", fileext = ".dat")
saveRDS (datasets::Orange, f)
u <- "https://my.upload.site"
req <- httr2::request (u) |>
    httr2::req_method ("PUT") |>
    httr2::req_headers ("Content-Type" = "application/octet-stream") |>
    httr2::req_body_file (path = f)

req$body$data
#> [1] "/tmp/RtmpW0CH4X/data-417e1b18af5c.dat"
#> attr(,"class")
#> [1] "httr2_path"

httptest2::gsub_response (
    req$body,
    paste0 ("[A-Za-z0-9]*", .Platform$file.sep, "data\\-"),
    paste0 ("tempdir", .Platform$file.sep, "data-"))
#> $data
#> [1] "/tmp/RtmpW0CH4X/data-417e1b18af5c.dat"
#> attr(,"class")
#> [1] "httr2_path"
#> 
#> $type
#> [1] "raw-file"
#> 
#> $content_type
#> [1] ""
#> 
#> $params
#> list()
#> 
#> $url
#> character(0)

Created on 2022-05-24 by the reprex package (v2.0.1)

The files output by httptest2 then have a part of the tempdir hash in their names, and so are not able to be reproduced, leading to test failures. In other words, a function issuing the req given above fails mock tests because the paths differ each time the test is run. I'd really appreciate any help you could offer. Thanks!

mpadge commented 2 years ago

Whoops, my mistake - I'm also including my own internal hash in the body text, and that is changing between calls. Problem is with me, and not this package. Sorry 'bout that, and keep up the good work!