Skip to content

Commit

Permalink
Got things to work for when a cassette records multiple requests made…
Browse files Browse the repository at this point in the history
… to the same URL with the same HTTP verb, but different responses. We have to register an array of responses with fakeweb.
  • Loading branch information
myronmarston committed Mar 4, 2010
1 parent 6abe551 commit e0993a2
Show file tree
Hide file tree
Showing 11 changed files with 515 additions and 147 deletions.
@@ -0,0 +1,85 @@
---
- !ruby/struct:VCR::RecordedResponse
method: :get
uri: http://example.com:80/
response: !ruby/object:Net::HTTPOK
body: |
<HTML>
<HEAD>
<TITLE>Example Web Page</TITLE>
</HEAD>
<body>
<p>
This is not the real response from example.com
</p>
</BODY>
</HTML>
body_exist: true
code: "200"
header:
etag:
- "\"24ec5-1b6-4059a80bfd280\""
last-modified:
- Tue, 15 Nov 2005 13:24:10 GMT
connection:
- Keep-Alive
content-type:
- text/html; charset=UTF-8
date:
- Thu, 25 Feb 2010 15:22:32 GMT
server:
- Apache/2.2.3 (CentOS)
content-length:
- "438"
age:
- "2643"
accept-ranges:
- bytes
http_version: "1.1"
message: OK
read: true
socket:
- !ruby/struct:VCR::RecordedResponse
method: :get
uri: http://example.com:80/
response: !ruby/object:Net::HTTPOK
body: |
<HTML>
<HEAD>
<TITLE>Example Web Page</TITLE>
</HEAD>
<body>
<p>
This is another fake response from example.com
</p>
</BODY>
</HTML>
body_exist: true
code: "200"
header:
etag:
- "\"24ec5-1b6-4059a80bfd280\""
last-modified:
- Tue, 15 Nov 2005 13:24:10 GMT
connection:
- Keep-Alive
content-type:
- text/html; charset=UTF-8
date:
- Thu, 25 Feb 2010 15:22:32 GMT
server:
- Apache/2.2.3 (CentOS)
content-length:
- "438"
age:
- "2643"
accept-ranges:
- bytes
http_version: "1.1"
message: OK
read: true
socket:
@@ -0,0 +1,85 @@
---
- !ruby/struct:VCR::RecordedResponse
method: :get
uri: http://example.com:80/
response: !ruby/object:Net::HTTPOK
body: |
<HTML>
<HEAD>
<TITLE>Example Web Page</TITLE>
</HEAD>
<body>
<p>
This is not the real response from example.com
</p>
</BODY>
</HTML>
body_exist: true
code: "200"
header:
etag:
- "\"24ec5-1b6-4059a80bfd280\""
last-modified:
- Tue, 15 Nov 2005 13:24:10 GMT
connection:
- Keep-Alive
content-type:
- text/html; charset=UTF-8
date:
- Thu, 25 Feb 2010 15:22:32 GMT
server:
- Apache/2.2.3 (CentOS)
content-length:
- "438"
age:
- "2643"
accept-ranges:
- bytes
http_version: "1.1"
message: OK
read: true
socket:
- !ruby/struct:VCR::RecordedResponse
method: :get
uri: http://example.com:80/
response: !ruby/object:Net::HTTPOK
body: |
<HTML>
<HEAD>
<TITLE>Example Web Page</TITLE>
</HEAD>
<body>
<p>
This is another fake response from example.com
</p>
</BODY>
</HTML>
body_exist: true
code: "200"
header:
etag:
- "\"24ec5-1b6-4059a80bfd280\""
last-modified:
- Tue, 15 Nov 2005 13:24:10 GMT
connection:
- Keep-Alive
content-type:
- text/html; charset=UTF-8
date:
- Thu, 25 Feb 2010 15:22:32 GMT
server:
- Apache/2.2.3 (CentOS)
content-length:
- "438"
age:
- "2643"
accept-ranges:
- bytes
http_version: "1.1"
message: OK
read: true
socket:
@@ -0,0 +1,85 @@
---
- !ruby/struct:VCR::RecordedResponse
:method: :get
:uri: http://example.com:80/
:response: !ruby/object:Net::HTTPOK
body: |
<HTML>
<HEAD>
<TITLE>Example Web Page</TITLE>
</HEAD>
<body>
<p>
This is not the real response from example.com
</p>
</BODY>
</HTML>
body_exist: true
code: "200"
header:
server:
- Apache/2.2.3 (Red Hat)
last-modified:
- Tue, 15 Nov 2005 13:24:10 GMT
etag:
- "\"b300b4-1b6-4059a80bfd280\""
accept-ranges:
- bytes
content-type:
- text/html; charset=UTF-8
connection:
- Keep-Alive
date:
- Thu, 28 Jan 2010 07:14:48 GMT
age:
- "1696"
content-length:
- "438"
http_version: "1.1"
message: OK
read: true
socket:
- !ruby/struct:VCR::RecordedResponse
:method: :get
:uri: http://example.com:80/
:response: !ruby/object:Net::HTTPOK
body: |
<HTML>
<HEAD>
<TITLE>Example Web Page</TITLE>
</HEAD>
<body>
<p>
This is another fake response from example.com
</p>
</BODY>
</HTML>
body_exist: true
code: "200"
header:
server:
- Apache/2.2.3 (Red Hat)
last-modified:
- Tue, 15 Nov 2005 13:24:10 GMT
etag:
- "\"b300b4-1b6-4059a80bfd280\""
accept-ranges:
- bytes
content-type:
- text/html; charset=UTF-8
connection:
- Keep-Alive
date:
- Thu, 28 Jan 2010 07:14:48 GMT
age:
- "1696"
content-length:
- "438"
http_version: "1.1"
message: OK
read: true
socket:
12 changes: 11 additions & 1 deletion features/replay_recorded_response.feature
Expand Up @@ -50,4 +50,14 @@ Feature: Replay recorded response
And we have a "temp/not_the_real_response" file with no previously recorded response for "http://example.com/foo"
When I make HTTP get requests to "http://example.com" and "http://example.com/foo" within the "temp/not_the_real_response" unregistered cassette
Then the response for "http://example.com" should match /This is not the real response from example\.com/
And the response for "http://example.com/foo" should match /The requested URL \/foo was not found/
And the response for "http://example.com/foo" should match /The requested URL \/foo was not found/

@replay_cassette3
Scenario: Replay multiple different recorded responses for requests to the same URL
Given this scenario is tagged with the vcr cassette tag: "@replay_cassette3"
And the "cucumber_tags/replay_cassette3" cache file has a response for "http://example.com" that matches /This is not the real response from example\.com/
And the "cucumber_tags/replay_cassette3" cache file has a response for "http://example.com" that matches /This is another fake response from example\.com/
When I make an HTTP get request to "http://example.com"
And I make an HTTP get request to "http://example.com"
Then response 1 for "http://example.com" should match /This is not the real response from example\.com/
And response 2 for "http://example.com" should match /This is another fake response from example\.com/
17 changes: 9 additions & 8 deletions features/step_definitions/vcr_steps.rb
Expand Up @@ -4,9 +4,8 @@ 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
response = responses.detect { |r| URI.parse(r.uri) == URI.parse(url) }
response.should_not be_nil
response.response.body.should =~ regex
responses = responses.select { |r| URI.parse(r.uri) == URI.parse(url) }
responses.detect { |r| r.response.body =~ regex }
end
end

Expand Down Expand Up @@ -49,7 +48,7 @@ def recorded_responses_for(cassette_name)
end

When /^I make an(.*)? HTTP (?:get|post) request to "([^\"]*)"$/ do |request_type, url|
@http_requests ||= {}
@http_requests ||= Hash.new([])
uri = URI.parse(url)
path = uri.path.to_s == '' ? '/' : uri.path
begin
Expand All @@ -65,7 +64,7 @@ def recorded_responses_for(cassette_name)
rescue => e
result = e
end
@http_requests[url] = result
@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|
Expand Down Expand Up @@ -94,12 +93,14 @@ def recorded_responses_for(cassette_name)
end

Then /^the HTTP get request to "([^\"]*)" should result in a fakeweb error$/ do |url|
@http_requests[url].should be_instance_of(FakeWeb::NetConnectNotAllowedError)
@http_requests[url][0].should be_instance_of(FakeWeb::NetConnectNotAllowedError)
end

Then /^the response for "([^\"]*)" should match \/(.+)\/$/ do |url, regex_str|
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].body.should =~ regex
@http_requests[url][response_num].body.should =~ regex
end

Then /^there should not be a "([^\"]*)" cache file$/ do |cassette_name|
Expand Down
2 changes: 1 addition & 1 deletion features/support/env.rb
Expand Up @@ -51,5 +51,5 @@ class << self

VCR.cucumber_tags do |t|
t.tags '@record_cassette1', '@record_cassette2', :record => :unregistered
t.tags '@replay_cassette1', '@replay_cassette2', :record => :none
t.tags '@replay_cassette1', '@replay_cassette2', '@replay_cassette3', :record => :none
end
10 changes: 9 additions & 1 deletion lib/vcr/cassette.rb
Expand Up @@ -67,8 +67,16 @@ def load_recorded_responses
recorded_responses.replace(@original_recorded_responses)
end

register_responses_with_fakeweb
end

def register_responses_with_fakeweb
requests = Hash.new([])
recorded_responses.each do |rr|
FakeWeb.register_uri(rr.method, rr.uri, { :response => rr.response })
requests[[rr.method, rr.uri]] += [rr.response]
end
requests.each do |request, responses|
FakeWeb.register_uri(request.first, request.last, responses.map{ |r| { :response => r } })
end
end

Expand Down

0 comments on commit e0993a2

Please sign in to comment.