vcr 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +27 -0
- data/CHANGELOG.md +13 -0
- data/Gemfile +24 -0
- data/Gemfile.lock +119 -0
- data/README.md +3 -6
- data/Rakefile +70 -0
- data/benchmarks/http_stubbing_libraries.rb +59 -0
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/regex_cassette.yml +43 -0
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette1.yml +43 -0
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette2.yml +63 -0
- data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette3.yml +85 -0
- data/features/fixtures/vcr_cassettes/1.9.1/erb_cassette.yml +36 -0
- data/features/fixtures/vcr_cassettes/1.9.1/match_requests_on.yml +35 -0
- data/features/fixtures/vcr_cassettes/1.9.1/nested_replay_cassette.yml +32 -0
- data/features/fixtures/vcr_cassettes/1.9.1/not_the_real_response.yml +43 -0
- data/features/fixtures/vcr_cassettes/1.9.1/replay_localhost_cassette.yml +32 -0
- data/features/fixtures/vcr_cassettes/not_1.9.1/cucumber_tags/regex_cassette.yml +43 -0
- data/features/fixtures/vcr_cassettes/not_1.9.1/cucumber_tags/replay_cassette1.yml +43 -0
- data/features/fixtures/vcr_cassettes/not_1.9.1/cucumber_tags/replay_cassette2.yml +47 -0
- data/features/fixtures/vcr_cassettes/not_1.9.1/cucumber_tags/replay_cassette3.yml +85 -0
- data/features/fixtures/vcr_cassettes/not_1.9.1/erb_cassette.yml +36 -0
- data/features/fixtures/vcr_cassettes/not_1.9.1/match_requests_on.yml +35 -0
- data/features/fixtures/vcr_cassettes/not_1.9.1/nested_replay_cassette.yml +24 -0
- data/features/fixtures/vcr_cassettes/not_1.9.1/not_the_real_response.yml +43 -0
- data/features/fixtures/vcr_cassettes/not_1.9.1/replay_localhost_cassette.yml +32 -0
- data/features/http_client.feature +16 -0
- data/features/net_http.feature +27 -0
- data/features/record_response.feature +66 -0
- data/features/replay_recorded_response.feature +96 -0
- data/features/step_definitions/http_client_steps.rb +7 -0
- data/features/step_definitions/net_http_steps.rb +37 -0
- data/features/step_definitions/vcr_steps.rb +240 -0
- data/features/support/env.rb +104 -0
- data/features/webmock.feature +26 -0
- data/lib/vcr.rb +2 -2
- data/lib/vcr/extensions/net_http.rb +0 -8
- data/lib/vcr/http_stubbing_adapters/common.rb +39 -0
- data/lib/vcr/http_stubbing_adapters/fakeweb.rb +74 -70
- data/lib/vcr/http_stubbing_adapters/webmock.rb +61 -62
- data/lib/vcr/version.rb +1 -1
- data/spec/cassette_spec.rb +280 -0
- data/spec/config_spec.rb +76 -0
- data/spec/cucumber_tags_spec.rb +55 -0
- data/spec/deprecations_spec.rb +52 -0
- data/spec/extensions/net_http_response_spec.rb +85 -0
- data/spec/extensions/net_http_spec.rb +77 -0
- data/spec/fixtures/1.9.1/0_3_1_cassette.yml +29 -0
- data/spec/fixtures/1.9.1/cassette_spec/erb_with_no_vars.yml +32 -0
- data/spec/fixtures/1.9.1/cassette_spec/erb_with_vars.yml +32 -0
- data/spec/fixtures/1.9.1/cassette_spec/example.yml +110 -0
- data/spec/fixtures/1.9.1/cassette_spec/with_localhost_requests.yml +86 -0
- data/spec/fixtures/1.9.1/example_net_http.yml +14 -0
- data/spec/fixtures/1.9.1/example_net_http_request.yml +12 -0
- data/spec/fixtures/1.9.1/example_net_http_response.yml +25 -0
- data/spec/fixtures/1.9.1/fake_example.com_responses.yml +106 -0
- data/spec/fixtures/1.9.1/match_requests_on.yml +185 -0
- data/spec/fixtures/not_1.9.1/0_3_1_cassette.yml +29 -0
- data/spec/fixtures/not_1.9.1/cassette_spec/erb_with_no_vars.yml +32 -0
- data/spec/fixtures/not_1.9.1/cassette_spec/erb_with_vars.yml +32 -0
- data/spec/fixtures/not_1.9.1/cassette_spec/example.yml +110 -0
- data/spec/fixtures/not_1.9.1/cassette_spec/with_localhost_requests.yml +86 -0
- data/spec/fixtures/not_1.9.1/example_net_http.yml +14 -0
- data/spec/fixtures/not_1.9.1/example_net_http_request.yml +12 -0
- data/spec/fixtures/not_1.9.1/example_net_http_response.yml +25 -0
- data/spec/fixtures/not_1.9.1/fake_example.com_responses.yml +106 -0
- data/spec/fixtures/not_1.9.1/match_requests_on.yml +185 -0
- data/spec/http_stubbing_adapters/fakeweb_spec.rb +35 -0
- data/spec/http_stubbing_adapters/webmock_spec.rb +35 -0
- data/spec/request_matcher_spec.rb +194 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/structs_spec.rb +121 -0
- data/spec/support/disable_warnings.rb +12 -0
- data/spec/support/http_library_adapters.rb +272 -0
- data/spec/support/http_stubbing_adapter.rb +100 -0
- data/spec/support/pending_on_heroku.rb +14 -0
- data/spec/support/temp_cassette_library_dir.rb +16 -0
- data/spec/support/vcr_localhost_server.rb +53 -0
- data/spec/vcr_spec.rb +198 -0
- data/spec/version_spec.rb +11 -0
- data/vcr.gemspec +43 -0
- metadata +134 -73
- data/lib/vcr/http_stubbing_adapters/base.rb +0 -36
- data/lib/vcr/version.rbc +0 -572
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup
|
4
|
+
|
5
|
+
require 'patron' unless RUBY_PLATFORM =~ /java/
|
6
|
+
require 'httpclient'
|
7
|
+
require 'em-http-request' unless RUBY_PLATFORM =~ /java/
|
8
|
+
require 'vcr'
|
9
|
+
require 'vcr/http_stubbing_adapters/fakeweb'
|
10
|
+
require 'vcr/http_stubbing_adapters/webmock'
|
11
|
+
require 'rspec'
|
12
|
+
|
13
|
+
# Ruby 1.9.1 has a different yaml serialization format.
|
14
|
+
YAML_SERIALIZATION_VERSION = RUBY_VERSION == '1.9.1' ? '1.9.1' : 'not_1.9.1'
|
15
|
+
|
16
|
+
# Requires supporting files with custom matchers and macros, etc,
|
17
|
+
# in ./support/ and its subdirectories.
|
18
|
+
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
config.extend TempCassetteLibraryDir
|
22
|
+
config.extend DisableWarnings
|
23
|
+
|
24
|
+
config.color_enabled = true
|
25
|
+
config.debug = RUBY_PLATFORM != 'java'
|
26
|
+
|
27
|
+
config.before(:each) do
|
28
|
+
VCR::Config.default_cassette_options = { :record => :new_episodes }
|
29
|
+
VCR::Config.http_stubbing_library = :fakeweb
|
30
|
+
|
31
|
+
WebMock.allow_net_connect!
|
32
|
+
WebMock.reset_webmock
|
33
|
+
|
34
|
+
FakeWeb.allow_net_connect = true
|
35
|
+
FakeWeb.clean_registry
|
36
|
+
end
|
37
|
+
|
38
|
+
config.filter_run :focus => true
|
39
|
+
config.run_all_when_everything_filtered = true
|
40
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
shared_examples_for "a header normalizer" do
|
4
|
+
let(:instance) do
|
5
|
+
with_headers('Some_Header' => 'value1', 'aNother' => ['a', 'b'], 'third' => [], 'FOURTH' => nil)
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'normalizes the hash to lower case keys and arrays of values' do
|
9
|
+
instance.headers.should == {
|
10
|
+
'some_header' => ['value1'],
|
11
|
+
'another' => ['a', 'b'],
|
12
|
+
'third' => [],
|
13
|
+
'fourth' => []
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'set nil header to an empty hash' do
|
18
|
+
with_headers(nil).headers.should == {}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe VCR::Request do
|
23
|
+
describe '#matcher' do
|
24
|
+
it 'returns a matcher with the given request' do
|
25
|
+
req = VCR::Request.new
|
26
|
+
req.matcher([:uri]).request.should == req
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns a matcher with the given match_attributes' do
|
30
|
+
req = VCR::Request.new
|
31
|
+
req.matcher([:uri, :headers]).match_attributes.to_a.should =~ [:uri, :headers]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.from_net_http_request' do
|
36
|
+
let(:net_http) { YAML.load(File.read(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/example_net_http.yml")) }
|
37
|
+
let(:request) { YAML.load(File.read(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/example_net_http_request.yml")) }
|
38
|
+
subject { described_class.from_net_http_request(net_http, request) }
|
39
|
+
|
40
|
+
before(:each) do
|
41
|
+
VCR.http_stubbing_adapter.should respond_to(:request_uri)
|
42
|
+
VCR.http_stubbing_adapter.stub!(:request_uri)
|
43
|
+
end
|
44
|
+
|
45
|
+
it { should be_instance_of(VCR::Request) }
|
46
|
+
its(:method) { should == :post }
|
47
|
+
its(:body) { should == 'id=7' }
|
48
|
+
its(:headers) { should == { "accept" => ["*/*"], "content-type" => ["application/x-www-form-urlencoded"] } }
|
49
|
+
|
50
|
+
it 'sets the uri using the http_stubbing_adapter.request_uri' do
|
51
|
+
VCR.http_stubbing_adapter.should_receive(:request_uri).with(net_http, request).and_return('foo/bar')
|
52
|
+
subject.uri.should == 'foo/bar'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def with_headers(headers)
|
57
|
+
described_class.new(:get, 'http://example.com/', nil, headers)
|
58
|
+
end
|
59
|
+
it_should_behave_like 'a header normalizer'
|
60
|
+
end
|
61
|
+
|
62
|
+
describe VCR::ResponseStatus do
|
63
|
+
describe '.from_net_http_response' do
|
64
|
+
let(:response) { YAML.load(File.read(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/example_net_http_response.yml")) }
|
65
|
+
subject { described_class.from_net_http_response(response) }
|
66
|
+
|
67
|
+
it { should be_instance_of(described_class) }
|
68
|
+
its(:code) { should == 200 }
|
69
|
+
its(:message) { should == 'OK' }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe VCR::Response do
|
74
|
+
describe '.from_net_http_response' do
|
75
|
+
let(:response) { YAML.load(File.read(File.dirname(__FILE__) + "/fixtures/#{YAML_SERIALIZATION_VERSION}/example_net_http_response.yml")) }
|
76
|
+
subject { described_class.from_net_http_response(response) }
|
77
|
+
|
78
|
+
it { should be_instance_of(described_class) }
|
79
|
+
its(:body) { should == 'The response from example.com' }
|
80
|
+
its(:http_version) { should == '1.1' }
|
81
|
+
its(:headers) { should == {
|
82
|
+
"last-modified" => ['Tue, 15 Nov 2005 13:24:10 GMT'],
|
83
|
+
"connection" => ['close'],
|
84
|
+
"etag" => ["\"24ec5-1b6-4059a80bfd280\""],
|
85
|
+
"content-type" => ["text/html; charset=UTF-8"],
|
86
|
+
"date" => ['Wed, 31 Mar 2010 02:43:26 GMT'],
|
87
|
+
"server" => ['Apache/2.2.3 (CentOS)'],
|
88
|
+
"content-length" => ['438'],
|
89
|
+
"accept-ranges" => ['bytes']
|
90
|
+
} }
|
91
|
+
|
92
|
+
it 'assigns the status using VCR::ResponseStatus.from_net_http_response' do
|
93
|
+
VCR::ResponseStatus.should respond_to(:from_net_http_response)
|
94
|
+
VCR::ResponseStatus.should_receive(:from_net_http_response).with(response).and_return(:the_status)
|
95
|
+
subject.status.should == :the_status
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def with_headers(headers)
|
100
|
+
described_class.new(:status, headers, nil, '1.1')
|
101
|
+
end
|
102
|
+
it_should_behave_like 'a header normalizer'
|
103
|
+
|
104
|
+
it "ensures the body is serialized to yaml as a raw string" do
|
105
|
+
body = "My String"
|
106
|
+
body.instance_variable_set(:@foo, 7)
|
107
|
+
instance = described_class.new(:status, {}, body, :version)
|
108
|
+
instance.body.to_yaml.should == "My String".to_yaml
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe VCR::HTTPInteraction do
|
113
|
+
%w( uri method ).each do |attr|
|
114
|
+
it "delegates :#{attr} to the request signature" do
|
115
|
+
sig = mock('request signature')
|
116
|
+
sig.should_receive(attr).and_return(:the_value)
|
117
|
+
instance = described_class.new(sig, nil)
|
118
|
+
instance.send(attr).should == :the_value
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,272 @@
|
|
1
|
+
HTTP_LIBRARY_ADAPTERS = {}
|
2
|
+
|
3
|
+
HTTP_LIBRARY_ADAPTERS['net/http'] = Module.new do
|
4
|
+
def self.http_library_name; 'Net::HTTP'; end
|
5
|
+
|
6
|
+
def get_body_string(response); response.body; end
|
7
|
+
|
8
|
+
def get_header(header_key, response)
|
9
|
+
response.get_fields(header_key)
|
10
|
+
end
|
11
|
+
|
12
|
+
def make_http_request(method, url, body = nil, headers = {})
|
13
|
+
uri = URI.parse(url)
|
14
|
+
Net::HTTP.new(uri.host, uri.port).send_request(method.to_s.upcase, uri.path, body, headers)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
HTTP_LIBRARY_ADAPTERS['patron'] = Module.new do
|
19
|
+
def self.http_library_name; 'Patron'; end
|
20
|
+
|
21
|
+
def get_body_string(response); response.body; end
|
22
|
+
|
23
|
+
def get_header(header_key, response)
|
24
|
+
response.headers[header_key]
|
25
|
+
end
|
26
|
+
|
27
|
+
def make_http_request(method, url, body = nil, headers = {})
|
28
|
+
Patron::Session.new.request(method, url, headers, :data => body || '')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
HTTP_LIBRARY_ADAPTERS['httpclient'] = Module.new do
|
33
|
+
def self.http_library_name; 'HTTP Client'; end
|
34
|
+
|
35
|
+
def get_body_string(response)
|
36
|
+
string = response.body.content
|
37
|
+
string.respond_to?(:read) ? string.read : string
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_header(header_key, response)
|
41
|
+
response.header[header_key]
|
42
|
+
end
|
43
|
+
|
44
|
+
def make_http_request(method, url, body = nil, headers = {})
|
45
|
+
HTTPClient.new.request(method, url, nil, body, headers)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
HTTP_LIBRARY_ADAPTERS['em-http-request'] = Module.new do
|
50
|
+
def self.http_library_name; 'EM HTTP Request'; end
|
51
|
+
|
52
|
+
def get_body_string(response)
|
53
|
+
response.response
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_header(header_key, response)
|
57
|
+
response.response_header[header_key.upcase.gsub('-', '_')].split(', ')
|
58
|
+
end
|
59
|
+
|
60
|
+
def make_http_request(method, url, body = nil, headers = {})
|
61
|
+
http = nil
|
62
|
+
EventMachine.run do
|
63
|
+
http = EventMachine::HttpRequest.new(url).send(method, :body => body, :head => headers)
|
64
|
+
http.callback { EventMachine.stop }
|
65
|
+
end
|
66
|
+
http
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
NET_CONNECT_NOT_ALLOWED_ERROR = /You can use VCR to automatically record this request and replay it later/
|
71
|
+
|
72
|
+
module HttpLibrarySpecs
|
73
|
+
def test_http_library(library, supported_request_match_attributes)
|
74
|
+
# patron and em-http-client cannot be installed on jruby
|
75
|
+
return if %w[patron em-http-request].include?(library) && RUBY_PLATFORM == 'java'
|
76
|
+
|
77
|
+
unless adapter_module = HTTP_LIBRARY_ADAPTERS[library]
|
78
|
+
raise ArgumentError.new("No http library adapter module could be found for #{library}")
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "using #{adapter_module.http_library_name}" do
|
82
|
+
include adapter_module
|
83
|
+
|
84
|
+
# Necessary for ruby 1.9.2. On 1.9.2 we get an error when we use super,
|
85
|
+
# so this gives us another alias we can use for the original method.
|
86
|
+
alias make_request make_http_request
|
87
|
+
|
88
|
+
describe '#stub_requests using specific match_attributes' do
|
89
|
+
before(:each) { subject.http_connections_allowed = false }
|
90
|
+
let(:interactions) { YAML.load(File.read(File.join(File.dirname(__FILE__), '..', 'fixtures', YAML_SERIALIZATION_VERSION, 'match_requests_on.yml'))) }
|
91
|
+
|
92
|
+
@supported_request_match_attributes = supported_request_match_attributes
|
93
|
+
def self.matching_on(attribute, valid, invalid, &block)
|
94
|
+
supported_request_match_attributes = @supported_request_match_attributes
|
95
|
+
|
96
|
+
describe ":#{attribute}" do
|
97
|
+
let(:perform_stubbing) { subject.stub_requests(interactions, [attribute]) }
|
98
|
+
|
99
|
+
if supported_request_match_attributes.include?(attribute)
|
100
|
+
before(:each) { perform_stubbing }
|
101
|
+
module_eval(&block)
|
102
|
+
|
103
|
+
valid.each do |val, response|
|
104
|
+
it "returns the expected response for a #{val.inspect} request" do
|
105
|
+
get_body_string(make_http_request(val)).should == response
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'raises an error for another method' do
|
110
|
+
expect { make_http_request(invalid) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
|
111
|
+
end
|
112
|
+
else
|
113
|
+
it 'raises an error indicating matching requests on this attribute is not supported' do
|
114
|
+
expect { perform_stubbing }.to raise_error(/does not support matching requests on #{attribute}/)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
matching_on :method, { :get => "get method response", :post => "post method response" }, :put do
|
121
|
+
def make_http_request(http_method)
|
122
|
+
make_request(http_method, 'http://some-wrong-domain.com/', nil, {})
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
matching_on :host, { 'example1.com' => 'example1.com host response', 'example2.com' => 'example2.com host response' }, 'example3.com' do
|
127
|
+
def make_http_request(host)
|
128
|
+
make_request(:get, "http://#{host}/some/wrong/path", nil, {})
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
matching_on :path, { '/path1' => 'path1 response', '/path2' => 'path2 response' }, '/path3' do
|
133
|
+
def make_http_request(path)
|
134
|
+
make_request(:get, "http://some.wrong.domain.com#{path}?p=q", nil, {})
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
matching_on :uri, { 'http://example.com/uri1' => 'uri1 response', 'http://example.com/uri2' => 'uri2 response' }, 'http://example.com/uri3' do
|
139
|
+
def make_http_request(uri)
|
140
|
+
make_request(:get, uri, nil, {})
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
matching_on :body, { 'param=val1' => 'val1 body response', 'param=val2' => 'val2 body response' }, 'param=val3' do
|
145
|
+
def make_http_request(body)
|
146
|
+
make_request(:put, "http://wrong-domain.com/wrong/path", body, {})
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
matching_on :headers, {{ 'X-HTTP-HEADER1' => 'val1' } => 'val1 header response', { 'X-HTTP-HEADER1' => 'val2' } => 'val2 header response' }, { 'X-HTTP-HEADER1' => 'val3' } do
|
151
|
+
def make_http_request(headers)
|
152
|
+
make_request(:get, "http://wrong-domain.com/wrong/path", nil, headers)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.test_real_http_request(http_allowed)
|
158
|
+
if http_allowed
|
159
|
+
|
160
|
+
it 'allows real http requests' do
|
161
|
+
get_body_string(make_http_request(:get, 'http://example.com/foo')).should =~ /The requested URL \/foo was not found/
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'records new http requests' do
|
165
|
+
VCR.should_receive(:record_http_interaction) do |interaction|
|
166
|
+
URI.parse(interaction.request.uri).to_s.should == URI.parse('http://example.com/foo').to_s
|
167
|
+
interaction.request.method.should == :get
|
168
|
+
interaction.response.status.code.should == 404
|
169
|
+
interaction.response.status.message.should == 'Not Found'
|
170
|
+
interaction.response.body.should =~ /The requested URL \/foo was not found/
|
171
|
+
end
|
172
|
+
|
173
|
+
make_http_request(:get, 'http://example.com/foo')
|
174
|
+
end
|
175
|
+
|
176
|
+
else
|
177
|
+
it 'does not allow real HTTP requests or record them' do
|
178
|
+
VCR.should_receive(:record_http_interaction).never
|
179
|
+
lambda { make_http_request(:get, 'http://example.com/foo') }.should raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_request_stubbed(method, url, expected)
|
185
|
+
subject.request_stubbed?(VCR::Request.new(method, url), [:method, :uri]).should == expected
|
186
|
+
end
|
187
|
+
|
188
|
+
[true, false].each do |http_allowed|
|
189
|
+
context "when #http_connections_allowed is set to #{http_allowed}" do
|
190
|
+
before(:each) { subject.http_connections_allowed = http_allowed }
|
191
|
+
|
192
|
+
it "returns #{http_allowed} for #http_connections_allowed?" do
|
193
|
+
subject.http_connections_allowed?.should == http_allowed
|
194
|
+
end
|
195
|
+
|
196
|
+
test_real_http_request(http_allowed)
|
197
|
+
|
198
|
+
unless http_allowed
|
199
|
+
describe '.ignore_localhost =' do
|
200
|
+
let(:localhost_response) { 'A localhost response!' }
|
201
|
+
let(:localhost_server) { VCR::LocalhostServer::STATIC_SERVERS[localhost_response] }
|
202
|
+
|
203
|
+
VCR::LOCALHOST_ALIASES.each do |localhost_alias|
|
204
|
+
describe 'when set to true' do
|
205
|
+
extend PendingOnHeroku
|
206
|
+
before(:each) { subject.ignore_localhost = true }
|
207
|
+
|
208
|
+
it "allows requests to #{localhost_alias}" do
|
209
|
+
get_body_string(make_http_request(:get, "http://#{localhost_alias}:#{localhost_server.port}/")).should == localhost_response
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
describe 'when set to false' do
|
214
|
+
before(:each) { subject.ignore_localhost = false }
|
215
|
+
|
216
|
+
it "does not allow requests to #{localhost_alias}" do
|
217
|
+
expect { make_http_request(:get, "http://#{localhost_alias}:#{localhost_server.port}/") }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
context 'when some requests are stubbed, after setting a checkpoint' do
|
225
|
+
before(:each) do
|
226
|
+
subject.create_stubs_checkpoint(:my_checkpoint)
|
227
|
+
@recorded_interactions = YAML.load(File.read(File.join(File.dirname(__FILE__), '..', 'fixtures', YAML_SERIALIZATION_VERSION, 'fake_example.com_responses.yml')))
|
228
|
+
subject.stub_requests(@recorded_interactions, VCR::RequestMatcher::DEFAULT_MATCH_ATTRIBUTES)
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'returns true from #request_stubbed? for the requests that are stubbed' do
|
232
|
+
test_request_stubbed(:post, 'http://example.com', true)
|
233
|
+
test_request_stubbed(:get, 'http://example.com/foo', true)
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'returns false from #request_stubbed? for requests that are not stubbed' do
|
237
|
+
test_request_stubbed(:post, 'http://example.com/foo', false)
|
238
|
+
test_request_stubbed(:get, 'http://google.com', false)
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'gets the stubbed responses when multiple post requests are made to http://example.com, and does not record them' do
|
242
|
+
VCR.should_receive(:record_http_interaction).never
|
243
|
+
get_body_string(make_http_request(:post, 'http://example.com/', { 'id' => '7' })).should == 'example.com post response with id=7'
|
244
|
+
get_body_string(make_http_request(:post, 'http://example.com/', { 'id' => '3' })).should == 'example.com post response with id=3'
|
245
|
+
end
|
246
|
+
|
247
|
+
it 'gets the stubbed responses when requests are made to http://example.com/foo, and does not record them' do
|
248
|
+
VCR.should_receive(:record_http_interaction).never
|
249
|
+
get_body_string(make_http_request(:get, 'http://example.com/foo')).should == 'example.com get response with path=foo'
|
250
|
+
end
|
251
|
+
|
252
|
+
it "correctly handles stubbing multiple values for the same header" do
|
253
|
+
get_header('Set-Cookie', make_http_request(:get, 'http://example.com/two_set_cookie_headers')).should =~ ['bar=bazz', 'foo=bar']
|
254
|
+
end
|
255
|
+
|
256
|
+
context 'when we restore our previous check point' do
|
257
|
+
before(:each) { subject.restore_stubs_checkpoint(:my_checkpoint) }
|
258
|
+
|
259
|
+
test_real_http_request(http_allowed)
|
260
|
+
|
261
|
+
it 'returns false from #request_stubbed?' do
|
262
|
+
test_request_stubbed(:get, 'http://example.com/foo', false)
|
263
|
+
test_request_stubbed(:post, 'http://example.com', false)
|
264
|
+
test_request_stubbed(:get, 'http://google.com', false)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|