nealrichardson/httptest2

Error when using `with_mock_dir` and responses with empty body

jmaspons opened this issue · 1 comments

Thanks for httptest2! I'm using it to test https://github.com/jmaspons/osmapiR and I found an error (v. 1.0.0):

remotes::install_github("jmaspons/osmapiR")  ## Sorry, I have no other calls at hand to reproduce the error
httptest2::with_mock_dir("mock_set_prefs_user", r <- osmapiR::osm_set_preferences_user(key = "test-pref", value = "value"))
#> Error in `resp_body_raw()`:
#> ! Can't retrieve empty body.
#> Run `rlang::last_trace()` to see where the error occurred.
rlang::last_trace()
#> <error/rlang_error>
#> Error in `resp_body_raw()`:
#> ! Can't retrieve empty body.
#> ---
#> Backtrace:
#>      ▆
#>    1. ├─httptest2::with_mock_dir(...)
#>   2. │ ├─httptest2:::with_mock_path(...)
#>   3. │ │ └─base::eval.parent(expr)
#>   4. │ │   └─base::eval(expr, p)
#>   5. │ └─httptest2::capture_requests(expr, simplify = simplify)
#>   6. │   └─base::eval.parent(expr)
#>   7. │     └─base::eval(expr, p)
#>   8. ├─testthat::expect_null(...)
#>   9. │ └─testthat::quasi_label(enquo(object), label, arg = "object")
#>  10. │   └─rlang::eval_bare(expr, quo_get_env(quo))
#>  11. └─osmapiR::osm_set_preferences_user(key = "test-pref", value = "value")
#>  12.   └─httr2::req_perform(req) at osmapiR/R/osmapi_user_data.R:628:3
#>  13.     ├─base::.doTrace(...) at osmapiR/R/osmapi_user_data.R:628:3
#>  14.     │ └─base::eval.parent(exprObj)
#>  15.     │   └─base::eval(expr, p)
#>  16.     │     └─base::eval(expr, p)
#>  17.     └─httptest2::save_response(...)
#>  18.       └─httr2::resp_body_string(response)
#>  19.         └─httr2::resp_body_raw(resp)
#> Run rlang::last_trace(drop = FALSE) to see 2 hidden frames.

I think it's missing a check for length(response$body) in

if (simplify && status == 200 && ct %in% names(CONTENT_TYPE_TO_EXT)) {

With simplify = FALSE it works because it has the check

if (is.raw(response$body) && ct %in% text_types && length(response$body)) {

I can do a PR if you want.

Sure, I think I see what you're saying. A PR would be appreciated, thanks!