- A couple of fixes to the docs, in response to CRAN request
- Update a test expectation due to the addition of a
name
field incurl::form_file()
in version 5.0.
with_mock_dir()
uses the requester set byset_requester()
(#70, @maelle).capture_requests()
can be used without loadinglibrary(httptest)
(#77, @kforner).stop_mocking()
correctly untraces all functions (#79, @kforner).get_current_requester()
is now included within the package exports to match the behavior ofget_current_redactor()
(#81, @stephenashton-dhsc).
- Function alias
with_mock_API
, deprecated in 3.0.0, has been removed. - Update some tests for an upcoming release of
testthat
.
expect_header()
now emits a single warning with all of the headers that are included with the call (instead of one warning per header). This makes catching multiple headers easier and prevents excess warnings when using testthat 3e (#38, @jonkeane)- Quiet an extraneous message about untracing
curl::form_file()
(#35, @dmenne) - Numerous internal testing changes to ensure compatibility with testthat 3e
- Add
with_mock_dir()
that allows to automatically switch between recording and playing back for a test (#48, @maelle). - Mocking PUT and POST with a body consisting of only
httr::upload_file()
no longer leaves a file connection open. - Mock files with special characters in the filename are now correctly found (#33, @natbprice)
- Many improvements to interactive test running and mock recording, including that
.mockPaths()
will prefer to usetests/testthat
if it exists (i.e. when running interactively in the package root directory) (#52) andstart_vignette()
similarly prefers thevignettes
directory (#55). - Function aliases
with_fake_HTTP
andbuildMockURL
, deprecated in 3.0.0, have been removed. - Testing: switch continuous integration to use GitHub Actions (#36, @jonkeane); rename
helper.R
tosetup.R
pertestthat
's latest recommendations (#44, @maelle)
- (Re)load package redactors when loading a package interactively with
pkgload::load_all()
, formerly ofdevtools
(#15) expect_header()
now defaults toignore.case = TRUE
because HTTP header names are case insensitive.- Support mocking of file uploads via
httr::upload_file
on all platforms (#25) - Remove deprecated
redact
andverbose
arguments tocapture_requests
- Patch for compatibility with the upcoming 1.4.0 release of
httr
.
use_httptest()
for convenience when setting up a new package- Warn when capturing requests if the
httr
request function errors and no response file is written (#16) - Support recording with
capture_requests()
when directly callingGET
et al. interactively with thehttr
package attached (#17) - Support regular expression matching of URLs and request bodies in
expect_GET()
et al. (#19)
capture_requests()
no longer includes the "request" object inside the recorded response when writing.R
verbose responses. As of 3.0.0,with_mock_api()
inserts the current request when loading mock files, so it was being overwritten anyway. This eliminates some (though not all) of the need for redacting responses. As a result, the redacting functionsredact_oauth()
andredact_http_auth()
have been removed because they only acted on theresponse$request
, which is now dropped entirely.capture_requests()
will record simplified response bodies for a range of Content-Types whensimplify = TRUE
(the default). Previously, only.json
(Content-Type: application/json
) was recorded as a simple text files; now,.html
,.xml
,.txt
,.csv
, and.tsv
are supported.- When recording with
simplify = TRUE
, HTTP responses with204 No Content
status are now written as empty files with.204
extension. This saves around 2K of disk space per file. with_mock_api()
now can also load these newly supported file types.- Bare JSON files written by
capture_requests()
are now "prettified" (i.e. multiline, nice indentation). capture_requests()
now records responses fromhttr::RETRY()
(#13)
- Store package-level vignette setup and teardown code, called inside
start_vignette()
andend_vignette()
, ininst/httptest/start-vignette.R
andinst/httptest/end-vignette.R
, respectively. Like with the package redactors and request preprocessors, these are automatically executed whenever your package is loaded andstart/end_vignette
is called. This makes it easy to write multiple vignettes without having to copy and paste as much setup code. Seevignette("vignettes")
for details.
gsub_response()
now applies over the URL in aLocation
header, if found.- Add
options(httptest.max.print)
to allow you the ability to specify a length to which to truncate the request body printed in the error message for requests inwith_mock_api()
. Useful for debugging mock files not found when there are large request bodies. - Add
options(httptest.debug)
, which ifTRUE
prints more details about which functions are being traced (bybase::trace()
) and when they're called. - Deprecate the "verbose" argument to
capture_requests()
: useoptions(httptest.verbose)
instead.
- Write vignettes and other R Markdown documents that communicate with a remote API using
httptest
. Add a code chunk at the beginning of the document includingstart_vignette()
. The first time you run the document, the real API responses are recorded to a subfolder in yourvignettes
directory. Subsequent vignette builds use these recorded responses, allowing the document to regenerate without a network connection or API credentials. If your document needs to handle changes of server state, as when you make an API request that creates a record on the server, add a call tochange_state()
. Seevignette("vignettes")
for more discussion and links to examples. - Packages can now have a default redacting function, such that whenever the package is loaded,
capture_requests()
will apply that function to any responses it records. This ensures that you never forget to sanitize your API responses if you need to use a custom function. To take advantage of this feature, put afunction (response) {...}
in a file atinst/httptest/redact.R
in your package. See the updatedvignette("redacting", package = "httptest")
for more. - You can also now provide a function to preprocess mock requests. This can be particularly for shortening URLs---and thus the mock file paths---because of CRAN-mandated constraints on file path lengths ("non-portable file paths"). This machinery works very similar to redacting responses when recording them, except it operates on
request
objects inside ofwith_mock_api()
. To use it, either pass afunction (request) {...}
toset_requester()
in your R session, or to define one for the package, put afunction (request) {...}
in a file atinst/httptest/request.R
.gsub_request()
is particularly useful here.vignette("redacting", package = "httptest")
has further details.
- Standardize exported functions on
snake_case
rather thancamelCase
to better align withhttr
andtestthat
(except for.mockPaths()
, which followsbase::.libPaths()
). Exported functions that have been renamed have retained their old aliases in this release, but they are to be deprecated. use_mock_api()
andblock_requests()
enable the request altering behavior ofwith_mock_api()
andwithout_internet()
, respectively, without the enclosing context. (use_mock_api
is called insidestart_vignette()
.) To turn off mocking, callstop_mocking()
.- Internal change: mocking contexts no longer use
testthat::with_mock()
and instead usetrace()
. capture_requests()
/start_capturing()
now allow you to call.mockPaths()
while actively recording so that you can record server state changes to a different mock "layer". Previously, the recording path was fixed when the context was initialized.- The
redact
argument tocapture_requests()
/start_capturing()
is deprecated in favor ofset_redactor()
. This function can take afunction (response) {...}
; a formula as shorthand for an anonymous function with.
as the "response" argument, as in thepurrr
package; a list of functions that will be chained together; orNULL
to disable the defaultredact_auth()
. redact_headers()
andwithin_body_text()
no longer return redacting functions. Instead, they takeresponse
as their first argument. This makes them more natural to use and chain together in custom redacting functions. To instead return a function as before, seeas.redactor()
.gsub_response()
is a new redactor that does regular-expression replacement (viabase::gsub()
) within a response's body text and URL..mockPaths()
only keeps unique path values, consistent withbase::.libPaths()
.- Option
"httptest.verbose"
to govern some extra debug messaging (automatically turned off bystart_vignette()
) - Fix a bug where
write_disk
responses that were recorded in one location and moved to another directory could not be loaded
- Ensure forward compatibility with a change in
deparse()
in the development version of R (r73699).
- Add
redact_oauth()
to purgehttr::Token()
objects from requests (#9).redact_oauth()
is built in toredact_auth()
, the default redactor, so no action is required to start using it.
- Remove support for mocking
utils::download.file()
, astestthat
no longer permits it. Usehttr::GET(url, config = write_disk(filename))
instead, whichhttptest
now more robustly supports incapture_requests()
.
- Add redacting functions (
redact_auth()
,redact_cookies()
,redact_http_auth()
,redact_headers()
,within_body_text()
) that can be specified incapture_requests()
so that sensitive information like tokens and ids can be purged from recorded response files. The default redacting function isredact_auth()
, which wraps several of them. Seevignette("redacting", package = "httptest")
for more. - When loading a JSON mock response, the current "request" object is now included in the response returned, as is the case with real responses.
- Remove the file size limitation for mock files loaded in
with_mock_api()
skip_if_disconnected()
now also wrapstestthat::skip_on_cran()
so that tests that require a real network connection don't cause a flaky test failure on CRAN
- Fix for compatibility with upcoming release of httr that affected non-GET requests that did not contain any request body.
with_mock_api()
andwithout_internet()
handle multipart and urlencoded form data in mocked HTTP requests.buildMockURL()
escapes URL characters that are not valid in file names on all R platforms (whichR CMD check
would warn about).capture_requests()
now has averbose
argument, which, ifTRUE
, prints a message with the file path where each captured request is written.capture_requests()
takes the first element in.mockPaths()
as its default "path" argument. The default is unchanged since.mockPaths()
by default returns the current working directory, just as the "path" default was, but if you set a different mock path for reading mocks,capture_requests()
will write there as well.
capture_requests()
now writes non-JSON-content-type and non-200-status responses as full "response" objects in .R files.with_mock_api()
now looks for .R mocks if a .json mock isn't found. This allows all requests and all responses, not just JSON content, to be mocked.- New
.mockPaths()
function, in the model of.libPaths()
, which allows you to specify alternate directories in which to search for mock API responses. - Documentation enriched and
vignette("httptest")
added.
- New context
capture_requests()
to collect the responses from real requests and store them as mock files with_trace()
convenience wrapper aroundtrace
/untrace
mockDownload()
now processes request URLs asmock_request()
does
- Add support in
with_mock_api()
for loading request fixtures for all HTTP verbs, not only GET (#4). Include request body in the mock file path hashing. buildMockURL()
can accept either a 'request' object or a character URL- Bump mock payload max size up to 128K
- Support full URLs, not just file paths, in
with_mock_api()
(#1)
expect_header()
to assert that a HTTP request has a header- Always prune the leading ":///" that appears in
with_mock_api()
if the URL has a querystring