Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ropensci-books/http-testing
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.5
Choose a base ref
...
head repository: ropensci-books/http-testing
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 4 commits
  • 9 files changed
  • 3 contributors

Commits on Feb 2, 2024

  1. add badge

    maelle committed Feb 2, 2024
    Copy the full SHA
    e3b05c5 View commit details
  2. use HTML badge in book itself

    maelle committed Feb 2, 2024
    Copy the full SHA
    7c99da6 View commit details

Commits on Sep 6, 2024

  1. Copy the full SHA
    cc48fd8 View commit details
  2. Update 08-vcr-usage.Rmd

    Co-authored-by: Maëlle Salmon <[email protected]>
    sckott and maelle committed Sep 6, 2024
    Copy the full SHA
    9da38dd View commit details
Showing with 18 additions and 10 deletions.
  1. +2 −2 03-mocking.Rmd
  2. +1 −1 04-webmockr-stubs.Rmd
  3. +4 −0 08-vcr-usage.Rmd
  4. +1 −1 Makefile
  5. +1 −0 README.md
  6. +2 −1 _common.R
  7. +2 −0 index.Rmd
  8. +3 −3 intro-pkgs.Rmd
  9. +2 −2 wholegames-comparison.Rmd
4 changes: 2 additions & 2 deletions 03-mocking.Rmd
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ knitr::opts_chunk$set(
)
```

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

# Mocking HTTP Requests {#mocking}

@@ -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
2 changes: 1 addition & 1 deletion 04-webmockr-stubs.Rmd
Original file line number Diff line number Diff line change
@@ -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.

4 changes: 4 additions & 0 deletions 08-vcr-usage.Rmd
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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)"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.10608848.svg)](https://doi.org/10.5281/zenodo.10608848)

HTTP testing in R book
======================
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",
@@ -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")
2 changes: 2 additions & 0 deletions index.Rmd
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@ description: "Best practice and tips for testing packages interfacing web resour
url: 'https\://books.ropensci.org/http-testing/'
---

<a href="https://doi.org/10.5281/zenodo.10608848"><img src="https://zenodo.org/badge/DOI/10.5281/zenodo.10608848.svg" alt="DOI"></a>

# Preamble

Are you working on an R package accessing resources on the web, be it a cat facts API, a scientific data source or your system for Customer relationship management?
6 changes: 3 additions & 3 deletions intro-pkgs.Rmd
Original file line number Diff line number Diff line change
@@ -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.
@@ -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)
@@ -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.
4 changes: 2 additions & 2 deletions wholegames-comparison.Rmd
Original file line number Diff line number Diff line change
@@ -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