nealrichardson / httptest2

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

Mock file can't be source due to `<` character #30

Closed asadow closed 11 months ago

asadow commented 11 months ago

Is this occurring on anyone else's side? I have cache = <environment> just near the end of my mock file. If I replace it with rlang::caller_env(), then the file can be sourced and a test will pass.

End of my mock file: cache = <environment>, class = "httr2_response")

nealrichardson commented 11 months ago

Looks like cache was just added to the dev version of httr2, not yet released: https://github.com/r-lib/httr2/commit/6b085735f37a34130c5c27213faec78825ebc86e#diff-95a582a44f505a6f9643d2348d2a0c0c5a9499602f36fa9a3943380195b2fde6R94

We should add response$cache <- NULL around here so that we don't include it in saved responses because the environment won't serialize (and is a cache anyway so not strictly necessary). Would you like to submit a PR?

asadow commented 11 months ago

cache = NULL in the mock file gives an error:

#> Error in `req_perform()`:
#> ! Failed to parse error body with method defined in `req_error()`.
#> Caused by error in `env_has()`:
#> ! `env` must be an environment, not `NULL`.

Setting cache = rlang::caller_env() fixes it. Does adding response$cache <- rlang::caller_env() make sense?

nealrichardson commented 11 months ago

I'll have to look into it more then, may have to create a cache env when loading the mock too or something.

In the meantime, I recommend staying on the released version of httr2, which doesn't have this issue.