Learn R Programming

httptest2: Test Helpers for 'httr2'

httptest2 makes it easy to write tests for code and packages that wrap web APIs. Testing code that communicates with remote servers can otherwise be painful: things like authentication, server state, and network flakiness can make testing seem too costly to bother with. The httptest2 package enables you to test all of the logic on the R sides of the API in your package without requiring access to the remote service.

Importantly, it provides contexts that mock the network connection and tools for recording real requests for future offline use as fixtures, both in tests and in vignettes. The package also includes additional expectations to assert that HTTP requests were---or were not---made.

Using these tools, you can test that code is making the intended requests and that it handles the expected responses correctly, all without depending on a connection to a remote API. The ability to save responses and load them offline also enables you to write package vignettes and other dynamic documents that can be distributed without access to a live server.

This package is an adaptation of httptest to work with httr2. Most features work exactly as they do in httptest; see the NEWS.md for the initial release for a summary of what has changed.

Installing

httptest2 can be installed from CRAN with

install.packages("httptest2")

The pre-release version of the package can be pulled from GitHub using the remotes package:

# install.packages("remotes")
remotes::install_github("nealrichardson/httptest2")

Getting started

To start using httptest2 with your package, run use_httptest2() in the root of your package directory. This will

  • add httptest2 to "Suggests" in the DESCRIPTION file
  • add library(httptest2) to tests/testthat/setup.R, which testthat loads before running tests

Then, you're ready to start using the tools that httptest2 provides. For an overview of how to get started, see vignette("httptest2"), and check out vignette("faq") for some common questions. See also the package reference for a list of all of the test contexts and expectations provided in the package.

Copy Link

Version

Install

install.packages('httptest2')

Monthly Downloads

3,038

Version

1.2.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Neal Richardson

Last Published

June 27th, 2025

Functions in httptest2 (1.2.0)

as.redactor

Wrap a redacting expression as a proper function
gsub_response

Find and replace within a response object
capture_requests

Record API responses as mock files
change_state

Handle a change of server state
expect_verb

Expectations for mocked HTTP requests
expect_request_header

Test that an HTTP request is made with a header
set_redactor

Set a response redactor
.mockPaths

Set an alternate directory for mock API fixtures
with_mock_api

Serve a mock API from files
redact

Remove sensitive content from HTTP responses
with_mock_dir

Use or create mock files depending on their existence
use_httptest2

Use 'httptest2' in your tests
start_vignette

Set mocking/capturing state for a vignette
save_response

Write out a captured response
httptest2

httptest2: A Test Environment for HTTP Requests
get_current_redactor

Fetch the active redacting function
find_mock_file

Go through mock paths to find the local mock file location
build_mock_url

Convert a request to a mock file path
with_trace

Wrapper around 'trace' to untrace when finished
without_internet

Make all HTTP requests raise an error