ReqVCR.Test (ReqVCR v0.2.0)
View SourceReqVCR testing conveniences.
This module provides utilities to mock HTTP requests and responses using VCR tapes. It integrates with Req.Test
to allow recording and replaying HTTP interactions.
Usage
Recording a VCR Tape
To record a VCR tape, you need to set the :vcr
option with :mode
set to :record
and specify a :tape_name
. This will record the HTTP interaction to the specified tape.
opts = [vcr: [mode: :record, tape_name: "example-tape.json"]]
{:ok, response} = vcr_request("https://example.com", opts)
assert %{action: :record, recorded: true} = Req.Response.get_private(response, :vcr)
Replaying a VCR Tape
To replay a VCR tape, you can use the plug_vcr_replay/1
function. This function sets up a mock that will respond with the recorded interaction from the specified tape.
opts = [
plug: plug_vcr_replay("example-tape.json"),
vcr: [mode: :record, tape_name: "example-tape.json"]
]
{:ok, response} = vcr_request("https://example.com", opts)
assert %{action: :replay, source: :mock} = Req.Response.get_private(response, :vcr)
Replaying Multiple VCR Tapes
You can also replay multiple VCR tapes sequentially by passing a list of tape names to plug_vcr_replay/1
.
vcr_configs = [
{"https://example.com/endpoint1", "tape1.json"},
{"https://example.com/endpoint2", "tape2.json"}
]
plug_vcr_replays =
vcr_configs
|> Enum.map(&elem(&1, 1))
|> plug_vcr_replay()
for {endpoint, tape_name} <- vcr_configs do
opts = [
plug: plug_vcr_replays,
vcr: [mode: :record, tape_name: tape_name]
]
{:ok, response} = vcr_request(endpoint, opts)
assert %{action: :replay, source: :mock} == Req.Response.get_private(response, :vcr)
end
Summary
Functions
Sets up an expectation to replay a VCR tape.
Sets up a stub to replay a VCR tape.
This function makes a request with VCR attached. It takes a URL and an optional list of VCR options.
This function gets the VCR config from a Req response.
Check if a VCR tape exists.
Convenience macro to run a function with a VCR tape.
Functions
Sets up an expectation to replay a VCR tape.
This function sets up a mock that will respond with the recorded interaction from the specified tape(s). It uses Req.Test.expect/2
to create the expectation.
Options
tape_names
: The name of the VCR tape or a list of VCR tape names to replay.opts
: Optional keyword list of options.
Sets up a stub to replay a VCR tape.
This function sets up a mock that will respond with the recorded interaction
from the specified tape(s). It uses Req.Test.stub/2
to create the stub.
Options
tape_names
: The name of the VCR tape or a list of VCR tape names to replay.opts
: Optional keyword list of options.
This function makes a request with VCR attached. It takes a URL and an optional list of VCR options.
This function gets the VCR config from a Req response.
Check if a VCR tape exists.
Example
iex> vcr_tape_exists?("tape.json") true
iex> vcr_tape_exists?("missing-tape.json") false
Convenience macro to run a function with a VCR tape.
This macro is useful for running a block of code with a VCR tape.
It takes a tape_name
and an optional list of VCR options.