set_redactor {httptest2}R Documentation

Set a response redactor

Description

A redactor is a function that alters the response content being written out in the capture_requests() context, allowing you to remove sensitive values, such as authentication tokens, as well as any other modification or truncation of the response body. By default, the redact_cookies() function will be used to purge standard auth methods, but set_redactor() allows you to provide a different one.

Usage

set_redactor(FUN)

Arguments

FUN

A function or expression that modifies httr2_response objects. Specifically, a valid input is one of:

  • A function taking a single argument, the httr2_response, and returning a valid httr2_response object.

  • A formula as shorthand for an anonymous function with . as the "response" argument, as in the purrr package. That is, instead of function (response) redact_headers(response, "X-Custom-Header"), you can use ~ redact_headers(., "X-Custom-Header")

  • A list of redacting functions/formulas, which will be executed in sequence on the response

  • NULL, to override the default redact_cookies().

Details

Alternatively, you can put a redacting function in inst/httptest2/redact.R in your package, and any time your package is loaded (as in when running tests or building vignettes), the function will be used automatically.

Value

Invisibly, the redacting function, validated and perhaps modified. Formulas and function lists are turned into proper functions. NULL as input returns the force() function.

See Also

For further details on how to redact responses, see vignette("redacting", package = "httptest2").

Examples

# Shorten UUIDs in response body/URLs to their first 6 digits:
set_redactor(function(resp) gsub_response(resp, "([0-9a-f]{6}[0-9a-f]{26}", "\\1"))
# Restore the default
set_redactor(redact_cookies)

[Package httptest2 version 1.1.0 Index]