Skip to content

Commit

Permalink
added changes for support for httr2 for vcr and webmockr
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Sep 4, 2024
1 parent 7c99da6 commit 3a29fa9
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions 03-mocking.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ knitr::opts_chunk$set(
)
```

# (PART) webmockr details {-}
# (PART) webmockr details {-}

# Mocking HTTP Requests {#mocking}

Expand Down Expand Up @@ -79,7 +79,7 @@ webmockr::enable()
```

`webmockr` will now by default not allow real HTTP requests from the http
libraries that adapters are loaded for (`crul` and `httr`).
libraries that adapters are loaded for (`crul`, `httr`, `httr2`).

You can optionally allow real requests via `webmockr_allow_net_connect()`, and
disallow real requests via `webmockr_disable_net_connect()`. You can check
Expand Down
2 changes: 1 addition & 1 deletion 04-webmockr-stubs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ x$get('get', query = list(a = "b"))


There are two ways to deal with mocking writing to disk. First, you can create a file
with the data you'd like in that file, then tell crul or httr where that file is.
with the data you'd like in that file, then tell crul, httr, or httr2 where that file is.
Second, you can simply give webmockr a file path (that doesn't exist yet) and some
data, and webmockr can take care of putting the data in the file.

Expand Down
4 changes: 4 additions & 0 deletions 08-vcr-usage.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ out$parse()
This also works with `httr`. The only difference is that you write to disk
with a function `httr::write_disk(path)` rather than a parameter.

::: {.alert .alert-dismissible .alert-info}
Writing to disk with `{httr2}` does not yet work with `{vcr}` - see <https://github.com/ropensci/vcr/issues/270>
:::

Note that when you write to disk when using `vcr`, the cassette is slightly
changed. Instead of holding the http response body itself, the cassette
has the file path with the response body.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ render:
Rscript -e "bookdown::render_book('index.Rmd')"

serve:
Rscript -e "bookdown::serve_book()"
Rscript -e "bookdown::serve_book(in_session = FALSE)"
3 changes: 2 additions & 1 deletion _common.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dir.create("rmd-fragments")

get_vcr_doc <- function(path) {
webmockr::disable()
fs::dir_create(dirname(file.path("rmd-fragments", path)), recurse = TRUE)
fragment_api <- gh::gh(
"/repos/:owner/:repo/contents/:path",
Expand All @@ -13,4 +14,4 @@ get_vcr_doc <- function(path) {
file = file.path("rmd-fragments", path)
)
}
options(knitr.duplicate.label = "allow")
options(knitr.duplicate.label = "allow")
6 changes: 3 additions & 3 deletions intro-pkgs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ What are the challenges of HTTP testing?
* While `{webmockr}` is in use, real HTTP interactions are not allowed. Therefore you need to stub all possible HTTP requests happening via your code. You'll get error messages for HTTP requests not covered by any stub.
* There is no recording interactions to disk at all, just mocked responses given as the user specifies in the R session.

`{webmockr}` works with both the `{crul}` package and the `{httr}` package.
`{webmockr}` works with `{crul}`, `{httr}`, and `{httr2}`.

`{webmockr}` is quite low-level and not the first tool you'll use directly in your day-to-day HTTP testing.
You may never use it directly but if you use `{vcr}` it's one of its foundations.
Expand All @@ -49,7 +49,7 @@ Therefore tests work independently of an internet connection.

`{vcr}` was inspired by the [Ruby vcr gem](https://relishapp.com/vcr/vcr/docs).

`{vcr}` works for packages using `{httr}` or `{crul}`.
`{vcr}` works for packages using `{httr}`, `{httr2}` or `{crul}`.

::: {.alert .alert-dismissible .alert-info}
[Direct link to `{vcr}` (& `{webmockr}`) demo](#vcr)
Expand Down Expand Up @@ -99,7 +99,7 @@ Because it runs a fake web service, you can even interact with said web service
[Direct link to `{webfakes}` demo](#webfakes)
:::

## testthat
## testthat

`{testthat}`, maintained by Hadley Wickham, is not a package specifically for HTTP testing; it is a package for general-purpose unit testing of R packages.
In this book we will assume that is what you use, because of its popularity.
Expand Down
4 changes: 2 additions & 2 deletions wholegames-comparison.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Of course, the choice of strategy in the demo is a bit subjective, but we hope i
A first message that's important to us: if you're learning about HTTP testing and using it in a branch of your own package sounds daunting, create a minimal package for playing!
:::

## What HTTP client can you use (curl, httr, crul)
## What HTTP client can you use (curl, httr, httr2, crul)

* httptest only works with httr (the most popular HTTP R client);
* vcr (& webmockr) works with both httr and crul (the two "high-level" HTTP R clients);
* vcr (& webmockr) works with httr, httr2, and crul (the three "high-level" HTTP R clients);
* webfakes works with any R HTTP client, even base R if you wish.

## Sustainability of the packages
Expand Down

0 comments on commit 3a29fa9

Please sign in to comment.