These functions can be used to temporarily mock responses from the Connect server, which is useful for writing tests that verify the behaviour of viewer-based credentials.

with_mocked_connect_responses(
  code,
  mock = NULL,
  token = NULL,
  error = FALSE,
  env = caller_env()
)

local_mocked_connect_responses(
  mock = NULL,
  token = NULL,
  error = FALSE,
  env = caller_env()
)

Arguments

code

Code to execute in the temporary environment.

mock

A function, a list, or NULL.

  • NULL disables mocking and returns httr2 to regular operation.

  • A list of responses will be returned in sequence. After all responses have been used up, will return 503 server errors.

  • For maximum flexibility, you can supply a function that that takes a single argument, req, and returns either NULL (if it doesn't want to handle the request) or a response (if it does).

token

When not NULL, return this token from the Connect server.

error

When TRUE, return an error from the Connect server.

env

Environment to use for scoping changes.

Value

with_mocked_connect_responses() returns the result of evaluating code.

Examples

with_mocked_connect_responses(
  connect_viewer_token(),
  token = "test"
)
#> <httr2_token>
#>token_type       : "Bearer"
#>access_token     : <REDACTED>
#>issued_token_type: "urn:ietf:params:oauth:token-type:access_token"