Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VCR making real HTTP requests on Playback #611

Open
saagunda opened this issue Sep 21, 2016 · 4 comments
Open

VCR making real HTTP requests on Playback #611

saagunda opened this issue Sep 21, 2016 · 4 comments

Comments

@saagunda
Copy link

The URI hostname that gets recorded is an AWS box. And whenever this box is down, my tests fail on playback mode.

@mcfiredrill
Copy link
Collaborator

Hi, thanks for reporting this.
Are you able to share any sort of reproducible test case?

@saagunda
Copy link
Author

@mcfiredrill Can you let me know what exactly would you need from me? The vcr.rb configurations or the test result output.

@mcfiredrill
Copy link
Collaborator

@saagunda

If you cannot share the test itself, could you write a new small test that reproduces it? The test file, along with the vcr configuration and the recorded vcr cassette would help alot.

@saagunda
Copy link
Author

saagunda commented Oct 12, 2016

This is my vcr config

require 'cucumber/rails'

def live_mode?
  ENV['LIVE_TESTS'] == '1'
end

def record_mode?
  ENV['RECORD'] == '1'
end

if live_mode?
  WebMock.allow_net_connect!
else
  # Dont make real http connections in test. Instead, expect VCR to be used.
  WebMock.disable_net_connect!
end

VCR.configure do |c|
  # Only make actual connections if live tests are enabled.
  c.default_cassette_options = { :record => (ENV['LIVE_TESTS'] == '1') ? :all : :new_episodes }
  c.hook_into :webmock

  # If we are running live tests but not recording their responses, use a cassettes
  # directory that's not checked into git to avoid saving bad responses, polluting
  # history, etc.
  if live_mode? && !record_mode?
    c.cassette_library_dir = 'tmp/cassettes'
  else
    c.cassette_library_dir = 'features/cassettes'
  end

  c.allow_http_connections_when_no_cassette = true
  c.ignore_localhost = true
  c.debug_logger = File.open("tmp/vcr.log", "w")
end

AfterStep('@live-only') do
  pending "This scenario can only run properly when LIVE_TESTS is enabled." unless live_mode?
end

Before('@vcr') do |scenario|
  if scenario.respond_to? :scenario_outline
    feature = scenario.scenario_outline.feature
  else
    feature = scenario.feature
  end

  cassette_name = "#{feature.name.split("\n").first}/#{scenario.name}"

  if record_mode? # Clean out the old one, since VCR will otherwise append.
    `rm 'features/cassettes/#{cassette_name.gsub(' ','_').gsub(',','')}.yml'`
  end

  aperture_matcher = lambda do |request_1, request_2|
    normalized_path_parts = lambda do |request|
      # Remove any part of the path that's numeric, and normalize.
      request.parsed_uri.path.split('/').reject(&:empty?).reject { |part| part.to_i > 0 }.compact.uniq.sort
    end

    normalized_path_parts.call(request_1) == normalized_path_parts.call(request_2)
  end

  VCR.insert_cassette(cassette_name, :erb => true, :allow_playback_repeats => true, :match_requests_on => [:method, aperture_matcher])
  #VCR.insert_cassette(cassette_name, :erb => true, :match_requests_on => [:method, :host, :uri, VCR.request_matchers.uri_without_param(:_)])
end

After('@vcr') do
  if record_mode? && live_mode?
    #pause to allow last requests to finish recording
    sleep(25)
  end
  VCR.eject_cassette
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants