Permalink
Browse files

Changed the HTTP Stubbing Adapters to support the match_requests_on

option.
  • Loading branch information...
1 parent f31f771 commit 2f34a5c44cea675ef843aa50ceb1a503f920c283 @myronmarston myronmarston committed Aug 11, 2010
View
@@ -2,7 +2,9 @@ require 'rubygems'
require 'rake'
require "rspec/core/rake_task"
-RSpec::Core::RakeTask.new(:spec)
+RSpec::Core::RakeTask.new(:spec) do |t|
+ t.spec_opts = ['--format', 'documentation']
+end
desc "Run all examples using rcov"
RSpec::Core::RakeTask.new :rcov => :cleanup_rcov_files do |t|
View
@@ -99,7 +99,7 @@ def load_recorded_interactions
recorded_interactions.replace(@original_recorded_interactions)
end
- VCR.http_stubbing_adapter.stub_requests(recorded_interactions)
+ VCR.http_stubbing_adapter.stub_requests(recorded_interactions, match_requests_on)
end
@@struct_cache = Hash.new do |hash, attributes|
@@ -19,15 +19,19 @@ def http_connections_allowed=(value)
::FakeWeb.allow_net_connect = value
end
- def stub_requests(http_interactions)
+ def stub_requests(http_interactions, match_attributes = RequestMatcher::DEFAULT_MATCH_ATTRIBUTES)
requests = Hash.new([])
http_interactions.each do |i|
- requests[[i.request.method, i.request.uri]] += [i.response]
+ requests[i.request.matcher(match_attributes)] += [i.response]
end
- requests.each do |request, responses|
- ::FakeWeb.register_uri(request.first, request.last, responses.map{ |r| response_hash(r) })
+ requests.each do |request_matcher, responses|
+ ::FakeWeb.register_uri(
+ request_matcher.method || :any,
+ request_matcher.uri,
+ responses.map{ |r| response_hash(r) }
+ )
end
end
@@ -21,17 +21,23 @@ def http_connections_allowed=(value)
::WebMock::Config.instance.allow_net_connect = value
end
- def stub_requests(recorded_responses)
+ def stub_requests(http_interactions, match_attributes = RequestMatcher::DEFAULT_MATCH_ATTRIBUTES)
requests = Hash.new([])
- # TODO: use the entire request signature, but make it configurable.
- recorded_responses.each do |rr|
- requests[[rr.method, rr.uri]] += [rr.response]
+ http_interactions.each do |i|
+ requests[i.request.matcher(match_attributes)] += [i.response]
end
- requests.each do |request, responses|
- ::WebMock.stub_request(request.first, request.last).
- to_return(responses.map{ |r| response_hash(r) })
+ requests.each do |request_matcher, responses|
+ stub = ::WebMock.stub_request(request_matcher.method || :any, request_matcher.uri)
+
+ with_hash = {}
+ with_hash[:body] = request_matcher.body if request_matcher.match_requests_on?(:body)
+ with_hash[:headers] = request_matcher.headers if request_matcher.match_requests_on?(:headers)
+
+ stub = stub.with(with_hash) if with_hash.size > 0
+
+ stub.to_return(responses.map{ |r| response_hash(r) })
end
end
View
@@ -144,11 +144,24 @@ def cassette_body(name, options = {})
VCR::Cassette.new(:name, :record => record_mode)
end
- it "#{load_interactions ? 'loads' : 'does not load'} the recorded interactions from the library yml file" do
- VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec")
- cassette = VCR::Cassette.new('example', :record => record_mode)
+ if load_interactions
+
+ [true, false].each do |ignore_localhost|
+ expected_uri_hosts = %w(example.com)
+ expected_uri_hosts += VCR::LOCALHOST_ALIASES unless ignore_localhost
+
+ it "#{ ignore_localhost ? 'does not load' : 'loads' } localhost interactions from the cassette file when http_stubbing_adapter.ignore_localhost is set to #{ignore_localhost}" do
+ VCR.http_stubbing_adapter.stub!(:ignore_localhost?).and_return(ignore_localhost)
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec")
+ cassette = VCR::Cassette.new('with_localhost_requests', :record => record_mode)
+ cassette.recorded_interactions.map { |i| URI.parse(i.uri).host }.should =~ expected_uri_hosts
+ end
+ end
+
+ it "loads the recorded interactions from the library yml file" do
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec")
+ cassette = VCR::Cassette.new('example', :record => record_mode)
- if load_interactions
cassette.should have(3).recorded_interactions
i1, i2, i3 = *cassette.recorded_interactions
@@ -164,35 +177,34 @@ def cassette_body(name, options = {})
i3.request.method.should == :get
i3.request.uri.should == 'http://example.com:80/'
i3.response.body.should =~ /Another example\.com response/
- else
- cassette.should have(0).recorded_interactions
end
- end
- it "#{load_interactions ? 'stubs' : 'does not stub'} the recorded requests with the http stubbing adapter" do
- VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec")
+ it "stubs the recorded requests with the http stubbing adapter" do
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec")
+ VCR.http_stubbing_adapter.should_receive(:stub_requests).with([an_instance_of(VCR::HTTPInteraction)]*3, anything)
+ cassette = VCR::Cassette.new('example', :record => record_mode)
+ end
- if load_interactions
- VCR.http_stubbing_adapter.should_receive(:stub_requests).with([an_instance_of(VCR::HTTPInteraction)]*3)
- else
- VCR.http_stubbing_adapter.should_receive(:stub_requests).never
+ it "passes the :match_request_on option to #stub_requests" do
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec")
+ VCR.http_stubbing_adapter.should_receive(:stub_requests).with(anything, [:body, :headers])
+ cassette = VCR::Cassette.new('example', :record => record_mode, :match_requests_on => [:body, :headers])
end
- cassette = VCR::Cassette.new('example', :record => record_mode)
- end
+ else
- if load_interactions
- [true, false].each do |ignore_localhost|
- expected_uri_hosts = %w(example.com)
- expected_uri_hosts += VCR::LOCALHOST_ALIASES unless ignore_localhost
+ it "does not stub the recorded requests with the http stubbing adapter" do
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec")
+ VCR.http_stubbing_adapter.should_not_receive(:stub_requests)
+ cassette = VCR::Cassette.new('example', :record => record_mode)
+ end
- it "#{ ignore_localhost ? 'does not load' : 'loads' } localhost interactions from the cassette file when http_stubbing_adapter.ignore_localhost is set to #{ignore_localhost}" do
- VCR.http_stubbing_adapter.stub!(:ignore_localhost?).and_return(ignore_localhost)
- VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec")
- cassette = VCR::Cassette.new('with_localhost_requests', :record => record_mode)
- cassette.recorded_interactions.map { |i| URI.parse(i.uri).host }.should =~ expected_uri_hosts
- end
+ it "does not load the recorded interactions from the library yml file" do
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/cassette_spec")
+ cassette = VCR::Cassette.new('example', :record => record_mode)
+ cassette.should have(0).recorded_interactions
end
+
end
end
end
@@ -0,0 +1,155 @@
+---
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://example.com:80/method
+ body:
+ headers:
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: post method response
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :get
+ uri: http://example.com:80/method
+ body:
+ headers:
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: get method response
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://example1.com:80/host
+ body:
+ headers:
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: example1.com host response
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://example2.com:80/host
+ body:
+ headers:
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: example2.com host response
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://example.com:80/uri1
+ body:
+ headers:
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: uri1 response
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://example.com:80/uri2
+ body:
+ headers:
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: uri2 response
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://example.com:80/
+ body: param=val1
+ headers:
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: val1 body response
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://example.com:80/
+ body: param=val2
+ headers:
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: val2 body response
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://example.com:80/
+ body:
+ headers:
+ x-http-header1:
+ - val1
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: val1 header response
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://example.com:80/
+ body:
+ headers:
+ x-http-header1:
+ - val2
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"24ec5-1b6-4059a80bfd280\""
+ body: val2 header response
+ http_version: "1.1"
@@ -2,7 +2,7 @@
describe VCR::HttpStubbingAdapters::FakeWeb do
it_should_behave_like 'an http stubbing adapter'
- it_should_behave_like 'an http stubbing adapter that supports Net::HTTP'
+ it_should_behave_like 'an http stubbing adapter that supports Net::HTTP', :method, :uri, :host
describe '#check_version!' do
disable_warnings
@@ -1,23 +1,25 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe VCR::HttpStubbingAdapters::WebMock do
+ supported_match_attributes = [:method, :uri, :host, :body, :headers]
+
it_should_behave_like 'an http stubbing adapter'
- it_should_behave_like 'an http stubbing adapter that supports Net::HTTP'
+ it_should_behave_like 'an http stubbing adapter that supports Net::HTTP', *supported_match_attributes
context "using patron" do
- it_should_behave_like 'an http stubbing adapter that supports some HTTP library' do
+ it_should_behave_like 'an http stubbing adapter that supports some HTTP library', *supported_match_attributes do
include PatronAdapter
end
end unless RUBY_PLATFORM =~ /java/
context "using httpclient" do
- it_should_behave_like 'an http stubbing adapter that supports some HTTP library' do
+ it_should_behave_like 'an http stubbing adapter that supports some HTTP library', *supported_match_attributes do
include HTTPClientAdapter
end
end
context "using em-http-request" do
- it_should_behave_like 'an http stubbing adapter that supports some HTTP library' do
+ it_should_behave_like 'an http stubbing adapter that supports some HTTP library', *supported_match_attributes do
include EmHTTPRequestAdapter
end
end unless RUBY_PLATFORM =~ /java/
Oops, something went wrong.

0 comments on commit 2f34a5c

Please sign in to comment.