forked from vcr/vcr
/
vcr_steps.rb
109 lines (94 loc) · 4.34 KB
/
vcr_steps.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'tmpdir'
module VCRHelpers
def have_expected_response(url, regex_str)
simple_matcher("a response from #{url} that matches /#{regex_str}/") do |responses|
regex = /#{regex_str}/i
responses = responses.select { |r| URI.parse(r.uri) == URI.parse(url) }
responses.detect { |r| r.response.body =~ regex }
end
end
def recorded_responses_for(cassette_name)
yaml_file = File.join(VCR::Config.cache_dir, "#{cassette_name}.yml")
yaml = File.open(yaml_file, 'r') { |f| f.read }
responses = YAML.load(yaml)
end
end
World(VCRHelpers)
Given /^we do not have a "([^\"]*)" cassette$/ do |cassette_name|
fixture_file = File.join(VCR::Config.cache_dir, "#{cassette_name}.yml")
File.exist?(fixture_file).should be_false
end
Given /^we have a "([^\"]*)" file with (a|no) previously recorded response for "([^\"]*)"$/ do |file_name, a_or_no, url|
fixture_file = File.join(VCR::Config.cache_dir, "#{file_name}.yml")
File.exist?(fixture_file).should be_true
responses = File.open(fixture_file, 'r') { |f| YAML.load(f.read) }
should_method = a_or_no == 'a' ? :should : :should_not
responses.map{ |r| URI.parse(r.uri) }.send(should_method, include(URI.parse(url)))
end
Given /^the "([^\"]*)" cache file has a response for "([^\"]*)" that matches \/(.+)\/$/ do |cassette_name, url, regex_str|
Given %{we have a "#{cassette_name}" file with a previously recorded response for "#{url}"}
Then %{the "#{cassette_name}" cache file should have a response for "#{url}" that matches /#{regex_str}/}
end
Given /^this scenario is tagged with the vcr cassette tag: "([^\"]+)"$/ do |tag|
VCR.current_cucumber_scenario.should be_tagged_with(tag)
VCR::CucumberTags.tags.should include(tag)
end
Given /^the previous scenario was tagged with the vcr cassette tag: "([^\"]*)"$/ do |tag|
last_scenario = VCR.completed_cucumber_scenarios.last
last_scenario.should_not be_nil
last_scenario.should be_tagged_with(tag)
VCR::CucumberTags.tags.should include(tag)
end
When /^I make an(.*)? HTTP (?:get|post) request to "([^\"]*)"$/ do |request_type, url|
@http_requests ||= Hash.new([])
uri = URI.parse(url)
path = uri.path.to_s == '' ? '/' : uri.path
begin
case request_type
when /asynchronous/
result = Net::HTTP.new(uri.host, uri.port).request_get(path) { |r| r.read_body { } }
result.body.should be_a(Net::ReadAdapter)
when /recursive/
result = Net::HTTP.new(uri.host, uri.port).post(path, nil)
else
result = Net::HTTP.get_response(uri)
end
rescue => e
result = e
end
@http_requests[url] += [result]
end
When /^I make(?: an)?(.*)? HTTP (get|post) requests? to "([^\"]*)"(?: and "([^\"]*)")? within the "([^\"]*)" ?(#{VCR::Cassette::VALID_RECORD_MODES.join('|')})? cassette$/ do |request_type, method, url1, url2, cassette_name, record_mode|
record_mode ||= :unregistered
record_mode = record_mode.to_sym
urls = [url1, url2].select { |u| u.to_s.size > 0 }
VCR.with_cassette(cassette_name, :record => record_mode) do
urls.each do |url|
When %{I make an#{request_type} HTTP #{method} request to "#{url}"}
end
end
end
Then /^the "([^\"]*)" cache file should have a response for "([^\"]*)" that matches \/(.+)\/$/ do |cassette_name, url, regex_str|
responses = recorded_responses_for(cassette_name)
responses.should have_expected_response(url, regex_str)
end
Then /^the "([^\"]*)" cache file should have exactly (\d+) response$/ do |cassette_name, response_count|
responses = recorded_responses_for(cassette_name)
responses.should have(response_count.to_i).responses
end
Then /^I can test the scenario cassette's recorded responses in the next scenario, after the cassette has been destroyed$/ do
# do nothing...
end
Then /^the HTTP get request to "([^\"]*)" should result in a fakeweb error$/ do |url|
@http_requests[url][0].should be_instance_of(FakeWeb::NetConnectNotAllowedError)
end
Then /^(?:the )?response(?: (\d+))? for "([^\"]*)" should match \/(.+)\/$/ do |response_num, url, regex_str|
response_num = response_num.to_i || 0
response_num -= 1 if response_num > 0 # translate to 0-based array index.
regex = /#{regex_str}/i
@http_requests[url][response_num].body.should =~ regex
end
Then /^there should not be a "([^\"]*)" cache file$/ do |cassette_name|
yaml_file = File.join(VCR::Config.cache_dir, "#{cassette_name}.yml")
File.exist?(yaml_file).should be_false
end