nealrichardson / httptest2

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

Error when using `with_mock_dir` and responses with empty body #35

Closed jmaspons closed 2 months ago

jmaspons commented 3 months ago

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 https://github.com/nealrichardson/httptest2/blob/cff21ee6e0a38e4b43826493d4afc4862d37a25e/R/capture-requests.R#L128

With simplify = FALSE it works because it has the check https://github.com/nealrichardson/httptest2/blob/cff21ee6e0a38e4b43826493d4afc4862d37a25e/R/capture-requests.R#L155

I can do a PR if you want.

nealrichardson commented 2 months ago

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