Project

General

Profile

Download (1.53 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / test / vcr_setup.rb @ d9498f54

1
require 'rubygems'
2
require 'vcr'
3
require 'hammer_cli'
4

    
5
def configure_vcr(mode = :none)
6
  if ENV['record'] == 'false' && mode == :none
7
    fail "Record flag is not applicable for mode 'none', please use with 'mode=all'"
8
  end
9

    
10
  VCR.configure do |c|
11
    c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
12
    c.hook_into :webmock
13

    
14
    if ENV['record'] == 'false' && mode != :none
15
      server = HammerCLI::Settings.get(:csv, :host) ||
16
          HammerCLI::Settings.get(:katello, :host) ||
17
          HammerCLI::Settings.get(:foreman, :host)
18
      uri = URI.parse(server)
19
      c.ignore_hosts uri.host
20
    end
21

    
22
    c.default_cassette_options = {
23
      :record => mode,
24
      :match_requests_on => [:method, :path, :params, :body_json],
25
      :decode_compressed_response => true
26
    }
27

    
28
    # rubocop:disable HandleExceptions
29
    begin
30
      c.register_request_matcher :body_json do |request_1, request_2|
31
        begin
32
          json_1 = JSON.parse(request_1.body)
33
          json_2 = JSON.parse(request_2.body)
34

    
35
          json_1 == json_2
36
        rescue
37
          #fallback incase there is a JSON parse error
38
          request_1.body == request_2.body
39
        end
40
      end
41
    rescue
42
      #ignore the warning thrown about this matcher already being resgistered
43
    end
44

    
45
    begin
46
      c.register_request_matcher :params do |request_1, request_2|
47
        URI(request_1.uri).query == URI(request_2.uri).query
48
      end
49
    rescue
50
      #ignore the warning thrown about this matcher already being resgistered
51
    end
52
    # rubocop:enable HandleExceptions
53
  end
54
end