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. Cassettes

Freezing Time

PreviousAllow Unused HTTP InteractionsNextOnce

Last updated 5 years ago

Was this helpful?

When dealing with an HTTP API that includes time-based compontents in the request (e.g. for signed S3 requests), it can be useful on playback to freeze time to what it originally was when the cassette was recorded so that the request is always the same each time your test is run.

While VCR doesn't directly support time freezing, it does expose VCR::Cassette#originally_recorded_at, which you can easily use with a library like to freeze time.

Note: VCR::Cassette#originally_recorded_at will return nil when the cassette is recording for the first time, so you'll probably want to use an expression like cassette.originally_recorded_at || Time.now so that it will work when recording or when playing back.

Previously recorded responses are replayed

Given a previously recorded cassette file "cassettes/example.yml" with:

--- 
http_interactions: 
- request: 
    method: get
    uri: http://example.com/events/since/2013-09-23T17:00:30Z
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Length: 
      - "20"
    body: 
      encoding: UTF-8
      string: Some Event
    http_version: "1.1"
  recorded_at: Mon, 23 Sep 2013 17:00:30 GMT
recorded_with: VCR 2.0.0

Given a file named "freeze_time.rb" with:

require 'time'
require 'timecop'
require 'vcr'

VCR.configure do |vcr|
  vcr.cassette_library_dir = 'cassettes'
  vcr.hook_into :webmock
end

VCR.use_cassette('example') do |cassette|
  Timecop.freeze(cassette.originally_recorded_at || Time.now) do
    path = "/events/since/#{Time.now.getutc.iso8601}"
    response = Net::HTTP.get_response('example.com', path)
    puts "Response: #{response.body}"
  end
end

When I run ruby freeze_time.rb

Then it should pass with "Response: Some Event".

timecop