VCR
v-6.0.0
v-6.0.0
  • Introduction
  • Upgrade
  • Changelog
  • About These Examples
  • License
  • Contributing
  • Cassettes
    • Cassette Format
    • Naming
    • Error for HTTP Request Made When No Cassette Is in Use
    • Dynamic ERB Cassettes
    • Automatic Re-Recording
    • Exclusive Cassette
    • Update Content Length Header
    • Decode Compressed Response
    • Allow Unused HTTP Interactions
    • Freezing Time
  • Record modes
    • Once
    • New Episodes
    • None
    • All
    • Record on Error
  • Configuration
    • Cassette Library Dir
    • Hook Into
    • Default Cassette Options
    • Ignore Request
    • Filter Sensitive Data
    • Allow HTTP Connections When No Cassette
    • Debug Logging
    • Preserve Exact Body Bytes
    • URI Parser
    • Query Parser
  • Hooks
    • Before Record Hook
    • Before Playback Hook
    • Before HTTP Request Hook
    • After HTTP Request Hook
    • Around HTTP Request Hook
  • Request matching
    • Introduction
    • Matching on Method
    • Matching on URI
    • Matching on Host
    • Matching on Path
    • Matching on Query String
    • Matching on Body
    • Matching on Headers
    • Identical Requests Are Replayed in Sequence
    • Register and Use a Custom Matcher
    • URI Without Param(s)
    • Playback Repeats
    • Matching on Body as JSON
  • Test frameworks
    • Usage With Test::Unit
    • Usage With RSpec Metadata
    • Usage With Cucumber
  • Middleware
    • Rack
    • Faraday Middleware
  • HTTP Libraries
    • Net::HTTP
    • EM HTTP Request
Powered by GitBook
On this page

Was this helpful?

  1. Request matching

Introduction

In order to properly replay previously recorded requests, VCR must match new HTTP requests to a previously recorded one. By default, it matches on HTTP method and URI, since that is usually deterministic and fully identifies the resource and action for typical RESTful APIs.

You can customize how VCR matches requests using the :match_requests_on cassette option. Specify an array of attributes to match on. Supported attributes are:

  • :method - The HTTP method (i.e. GET, POST, PUT or DELETE) of the request.

  • :uri - The full URI of the request.

  • :host - The host of the URI. You can use this (alone, or in combination

    with :path) as an alternative to :uri to cause VCR to match using a regex

    that matches the host.

  • :path - The path of the URI. You can use this (alone, or in combination

    with :host) as an alternative to :uri to cause VCR to match using a regex

    that matches the path.

  • :query - The query string values of the URI. The query string ordering does

    not affect matching results (it's order-agnostic).

  • :body - The body of the request.

  • :headers - The request headers.

You can also register a custom request matcher. This particularly comes in handy for dealing with APIs that use non-deterministic URIs (i.e. by including a timestamp as a query parameter or whatever).

When a cassette contains multiple HTTP interactions that match a request based on the configured :match_requests_on setting, the responses are sequenced: the first matching request will get the first response, the second matching request will get the second response, etc.

PreviousAround HTTP Request HookNextMatching on Method

Last updated 4 years ago

Was this helpful?