Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using with_mock_dir and responses with empty body #35

Closed
jmaspons opened this issue Mar 26, 2024 · 1 comment · Fixed by #37
Closed

Error when using with_mock_dir and responses with empty body #35

jmaspons opened this issue Mar 26, 2024 · 1 comment · Fixed by #37

Comments

@jmaspons
Copy link
Contributor

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.

@nealrichardson
Copy link
Owner

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

jmaspons added a commit to jmaspons/httptest2 that referenced this issue Apr 16, 2024
If the response has no body, write and empty file
FIX nealrichardson#35
jmaspons added a commit to jmaspons/httptest2 that referenced this issue Apr 25, 2024
If the response has no body, write and empty file
FIX nealrichardson#35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants