Project

General

Profile

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

runcible / test / vcr_setup.rb @ c022bb6f

1
require 'rubygems'
2
require 'vcr'
3

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

    
9
  if mode != :none
10
    unless system("sudo cp -rf #{File.dirname(__FILE__)}/fixtures/repositories /var/www/")
11
      fail "Cannot copy repository fixtures to /var/www, ensure sudo access"
12
    end
13
    unless system("sudo tar zxf /var/www/repositories/ostree/*.tgz -C /var/www/repositories/ostree")
14
      fail "Could not extract the ostree repo"
15
    end
16

    
17
  end
18

    
19
  VCR.configure do |c|
20
    c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
21
    c.hook_into :webmock
22

    
23
    if ENV['record'] == 'false' && mode != :none
24
      uri = URI.parse(TestRuncible.server.config[:url])
25
      c.ignore_hosts uri.host
26
    end
27

    
28
    c.default_cassette_options = {
29
      :record => mode,
30
      :match_requests_on => [:method, :path, :params, :body_json],
31
      :decode_compressed_response => true
32
    }
33

    
34
    begin
35
      c.register_request_matcher :body_json do |request_1, request_2|
36
        begin
37
          json_1 = JSON.parse(request_1.body)
38
          json_2 = JSON.parse(request_2.body)
39

    
40
          json_1 == json_2
41
        rescue
42
          #fallback incase there is a JSON parse error
43
          request_1.body == request_2.body
44
        end
45
      end
46
    rescue
47
      #ignore the warning thrown about this matcher already being resgistered
48
    end
49

    
50
    begin
51
      c.register_request_matcher :params do |request_1, request_2|
52
        URI(request_1.uri).query == URI(request_2.uri).query
53
      end
54
    rescue
55
      #ignore the warning thrown about this matcher already being resgistered
56
    end
57
  end
58
end