webmock 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/LICENSE +281 -0
- data/README.md +170 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/lib/webmock.rb +18 -0
- data/lib/webmock/adapters/rspec.rb +23 -0
- data/lib/webmock/adapters/rspec/matchers.rb +19 -0
- data/lib/webmock/adapters/rspec/request_profile_matcher.rb +37 -0
- data/lib/webmock/adapters/rspec/webmock_matcher.rb +45 -0
- data/lib/webmock/adapters/test_unit.rb +25 -0
- data/lib/webmock/config.rb +7 -0
- data/lib/webmock/errors.rb +5 -0
- data/lib/webmock/http_lib_adapters/net_http.rb +129 -0
- data/lib/webmock/request_execution_verifier.rb +22 -0
- data/lib/webmock/request_profile.rb +67 -0
- data/lib/webmock/request_registry.rb +47 -0
- data/lib/webmock/request_stub.rb +26 -0
- data/lib/webmock/response.rb +31 -0
- data/lib/webmock/url.rb +46 -0
- data/lib/webmock/util/hash_counter.rb +12 -0
- data/lib/webmock/utility.rb +65 -0
- data/lib/webmock/webmock.rb +58 -0
- data/spec/net_http_spec.rb +33 -0
- data/spec/other_net_http_libs_spec.rb +37 -0
- data/spec/request_execution_verifier_spec.rb +51 -0
- data/spec/request_profile_spec.rb +166 -0
- data/spec/request_registry_spec.rb +114 -0
- data/spec/request_stub_spec.rb +54 -0
- data/spec/response_spec.rb +59 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +58 -0
- data/spec/util/hash_counter_spec.rb +24 -0
- data/spec/utility_spec.rb +70 -0
- data/spec/vendor/right_http_connection-1.2.4/History.txt +59 -0
- data/spec/vendor/right_http_connection-1.2.4/Manifest.txt +7 -0
- data/spec/vendor/right_http_connection-1.2.4/README.txt +54 -0
- data/spec/vendor/right_http_connection-1.2.4/Rakefile +103 -0
- data/spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb +160 -0
- data/spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb +435 -0
- data/spec/vendor/right_http_connection-1.2.4/setup.rb +1585 -0
- data/spec/vendor/samuel-0.2.1/.document +5 -0
- data/spec/vendor/samuel-0.2.1/.gitignore +5 -0
- data/spec/vendor/samuel-0.2.1/LICENSE +20 -0
- data/spec/vendor/samuel-0.2.1/README.rdoc +70 -0
- data/spec/vendor/samuel-0.2.1/Rakefile +62 -0
- data/spec/vendor/samuel-0.2.1/VERSION +1 -0
- data/spec/vendor/samuel-0.2.1/lib/samuel.rb +52 -0
- data/spec/vendor/samuel-0.2.1/lib/samuel/net_http.rb +10 -0
- data/spec/vendor/samuel-0.2.1/lib/samuel/request.rb +96 -0
- data/spec/vendor/samuel-0.2.1/samuel.gemspec +69 -0
- data/spec/vendor/samuel-0.2.1/test/request_test.rb +193 -0
- data/spec/vendor/samuel-0.2.1/test/samuel_test.rb +42 -0
- data/spec/vendor/samuel-0.2.1/test/test_helper.rb +66 -0
- data/spec/vendor/samuel-0.2.1/test/thread_test.rb +32 -0
- data/spec/webmock_spec.rb +492 -0
- data/test/test_helper.rb +9 -0
- data/test/test_webmock.rb +56 -0
- metadata +144 -0
@@ -0,0 +1,47 @@
|
|
1
|
+
module WebMock
|
2
|
+
|
3
|
+
class RequestRegistry
|
4
|
+
include Singleton
|
5
|
+
|
6
|
+
attr_accessor :request_stubs, :requested
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
reset_webmock
|
10
|
+
end
|
11
|
+
|
12
|
+
def reset_webmock
|
13
|
+
self.request_stubs = []
|
14
|
+
self.requested = HashCounter.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def register_request_stub(stub)
|
18
|
+
request_stubs.insert(0, stub)
|
19
|
+
stub
|
20
|
+
end
|
21
|
+
|
22
|
+
def registered_request?(request_profile)
|
23
|
+
stub_for(request_profile)
|
24
|
+
end
|
25
|
+
|
26
|
+
def response_for_request(request_profile)
|
27
|
+
stub = stub_for(request_profile)
|
28
|
+
self.requested.put(request_profile)
|
29
|
+
stub ? stub.response : nil
|
30
|
+
end
|
31
|
+
|
32
|
+
def times_executed(request_profile)
|
33
|
+
self.requested.hash.select { |executed_request_profile, times_executed|
|
34
|
+
executed_request_profile.match(request_profile)
|
35
|
+
}.inject(0) {|sum, (_, times_executed)| sum + times_executed }
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def stub_for(request_profile)
|
41
|
+
request_stubs.detect { |registered_request_stub|
|
42
|
+
request_profile.match(registered_request_stub.request_profile)
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module WebMock
|
2
|
+
class RequestStub
|
3
|
+
attr_accessor :request_profile, :response
|
4
|
+
|
5
|
+
def initialize(method, url)
|
6
|
+
@request_profile = RequestProfile.new(method, url)
|
7
|
+
@response = WebMock::Response.new
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
def with(params)
|
12
|
+
@request_profile.body = params[:body]
|
13
|
+
@request_profile.headers = Utility.normalize_headers(params[:headers])
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_return(response_hash)
|
18
|
+
@response = WebMock::Response.new(response_hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_raise(exception)
|
22
|
+
@response = WebMock::Response.new({:exception => exception})
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module WebMock
|
2
|
+
class Response
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
@options = options
|
6
|
+
end
|
7
|
+
|
8
|
+
def headers
|
9
|
+
Utility.normalize_headers(@options[:headers])
|
10
|
+
end
|
11
|
+
|
12
|
+
def body
|
13
|
+
return '' unless @options.has_key?(:body)
|
14
|
+
|
15
|
+
if !@options[:body].include?("\0") && File.exists?(@options[:body]) && !File.directory?(@options[:body])
|
16
|
+
File.read(@options[:body])
|
17
|
+
else
|
18
|
+
@options[:body]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def status
|
23
|
+
@options.has_key?(:status) ? @options[:status] : 200
|
24
|
+
end
|
25
|
+
|
26
|
+
def raise_error_if_any
|
27
|
+
raise @options[:exception].new('Exception from WebMock') if @options.has_key?(:exception)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/webmock/url.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module WebMock
|
2
|
+
|
3
|
+
class URL
|
4
|
+
|
5
|
+
def self.normalize_uri(uri)
|
6
|
+
return uri if uri.is_a?(Regexp)
|
7
|
+
normalized_uri =
|
8
|
+
case uri
|
9
|
+
when URI then uri
|
10
|
+
when String
|
11
|
+
uri = 'http://' + uri unless uri.match('^https?://')
|
12
|
+
URI.parse(uri)
|
13
|
+
end
|
14
|
+
normalized_uri.query = sort_query_params(normalized_uri.query)
|
15
|
+
normalized_uri.normalize
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.variations_of_uri_as_strings(uri_object)
|
19
|
+
normalized_uri = normalize_uri(uri_object.dup)
|
20
|
+
normalized_uri_string = normalized_uri.to_s
|
21
|
+
|
22
|
+
variations = [normalized_uri_string]
|
23
|
+
|
24
|
+
# if the port is implied in the original, add a copy with an explicit port
|
25
|
+
if normalized_uri.default_port == normalized_uri.port
|
26
|
+
variations << normalized_uri_string.sub(
|
27
|
+
/#{Regexp.escape(normalized_uri.request_uri)}$/,
|
28
|
+
":#{normalized_uri.port}#{normalized_uri.request_uri}")
|
29
|
+
end
|
30
|
+
|
31
|
+
variations
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def self.sort_query_params(query)
|
37
|
+
if query.nil? || query.empty?
|
38
|
+
nil
|
39
|
+
else
|
40
|
+
query.split('&').sort.join('&')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#This file is taken from FakeWeb (fakeweb.rubyforge.org/) and adopted
|
2
|
+
|
3
|
+
module WebMock
|
4
|
+
module Utility #:nodoc:
|
5
|
+
|
6
|
+
def self.decode_userinfo_from_header(header)
|
7
|
+
header.sub(/^Basic /, "").unpack("m").first
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.encode_unsafe_chars_in_userinfo(userinfo)
|
11
|
+
unsafe_in_userinfo = /[^#{URI::REGEXP::PATTERN::UNRESERVED};&=+$,]|^(#{URI::REGEXP::PATTERN::ESCAPED})/
|
12
|
+
userinfo.split(":").map { |part| URI.escape(part, unsafe_in_userinfo) }.join(":")
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.strip_default_port_from_uri(uri)
|
16
|
+
case uri
|
17
|
+
when %r{^http://} then uri.sub(%r{:80(/|$)}, '\1')
|
18
|
+
when %r{^https://} then uri.sub(%r{:443(/|$)}, '\1')
|
19
|
+
else uri
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.puts_warning_for_net_http_around_advice_libs_if_needed
|
24
|
+
libs = {"Samuel" => defined?(Samuel)}
|
25
|
+
warnings = libs.select { |_, loaded| loaded }.map do |name, _|
|
26
|
+
<<-TEXT.gsub(/ {10}/, '')
|
27
|
+
\e[1mWarning: WebMock was loaded after #{name}\e[0m
|
28
|
+
* #{name}'s code is being ignored when a request is handled by WebMock,
|
29
|
+
because both libraries work by patching Net::HTTP.
|
30
|
+
* To fix this, just reorder your requires so that WebMock is before #{name}.
|
31
|
+
TEXT
|
32
|
+
end
|
33
|
+
$stderr.puts "\n" + warnings.join("\n") + "\n" if warnings.any?
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.record_loaded_net_http_replacement_libs
|
37
|
+
libs = {"RightHttpConnection" => defined?(RightHttpConnection)}
|
38
|
+
@loaded_net_http_replacement_libs = libs.map { |name, loaded| name if loaded }.compact
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.puts_warning_for_net_http_replacement_libs_if_needed
|
42
|
+
libs = {"RightHttpConnection" => defined?(RightHttpConnection)}
|
43
|
+
warnings = libs.select { |_, loaded| loaded }.
|
44
|
+
reject { |name, _| @loaded_net_http_replacement_libs.include?(name) }.
|
45
|
+
map do |name, _|
|
46
|
+
<<-TEXT.gsub(/ {10}/, '')
|
47
|
+
\e[1mWarning: #{name} was loaded after WebMock\e[0m
|
48
|
+
* WebMock's code is being ignored, because #{name} replaces parts of
|
49
|
+
Net::HTTP without deferring to other libraries. This will break Net::HTTP requests.
|
50
|
+
* To fix this, just reorder your requires so that #{name} is before WebMock.
|
51
|
+
TEXT
|
52
|
+
end
|
53
|
+
$stderr.puts "\n" + warnings.join("\n") + "\n" if warnings.any?
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.normalize_headers(headers)
|
57
|
+
return nil unless headers
|
58
|
+
array = headers.map { |name, value|
|
59
|
+
[name.to_s.split(/_|-/).map { |segment| segment.capitalize }.join("-"), value.to_s]
|
60
|
+
}
|
61
|
+
Hash[*array.flatten]
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
WebMock::Utility.record_loaded_net_http_replacement_libs
|
2
|
+
WebMock::Utility.puts_warning_for_net_http_around_advice_libs_if_needed
|
3
|
+
|
4
|
+
|
5
|
+
module WebMock
|
6
|
+
|
7
|
+
def stub_request(method, url)
|
8
|
+
RequestRegistry.instance.register_request_stub(RequestStub.new(method, url))
|
9
|
+
end
|
10
|
+
|
11
|
+
alias_method :stub_http_request, :stub_request
|
12
|
+
|
13
|
+
def request(method, url)
|
14
|
+
RequestProfile.new(method, url)
|
15
|
+
end
|
16
|
+
|
17
|
+
def assert_requested(method, url, options = {})
|
18
|
+
expected_times_executed = options.delete(:times) || 1
|
19
|
+
request = RequestProfile.new(method, url, options[:body], options[:headers])
|
20
|
+
verifier = RequestExecutionVerifier.new(request, expected_times_executed)
|
21
|
+
assertion_failure(verifier.failure_message) unless verifier.verify
|
22
|
+
end
|
23
|
+
|
24
|
+
def assert_not_requested(method, url, options = {})
|
25
|
+
assert_requested(method, url, options.update(:times => 0))
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.allow_net_connect!
|
29
|
+
Config.instance.allow_net_connect = true
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.disable_net_connect!
|
33
|
+
Config.instance.allow_net_connect = false
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.net_connect_allowed?
|
37
|
+
Config.instance.allow_net_connect
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.registered_request?(request_profile)
|
41
|
+
RequestRegistry.instance.registered_request?(request_profile)
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.response_for_request(request_profile, &block)
|
45
|
+
RequestRegistry.instance.response_for_request(request_profile, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
def reset_webmock
|
49
|
+
WebMock::RequestRegistry.instance.reset_webmock
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def assertion_failure(message)
|
55
|
+
raise message
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'webmock_spec'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
include WebMock
|
6
|
+
|
7
|
+
def http_request(method, url, options = {})
|
8
|
+
url = URI.parse(url)
|
9
|
+
response = nil
|
10
|
+
clazz = Net::HTTP.const_get("#{method.to_s.capitalize}")
|
11
|
+
req = clazz.new(url.path, options[:headers])
|
12
|
+
req.basic_auth url.user, url.password if url.user
|
13
|
+
http = Net::HTTP.new(url.host, url.port)
|
14
|
+
http.use_ssl = true if url.scheme == "https"
|
15
|
+
response = http.start {|http|
|
16
|
+
http.request(req, options[:body])
|
17
|
+
}
|
18
|
+
OpenStruct.new({
|
19
|
+
:body => response.body,
|
20
|
+
:headers => response,
|
21
|
+
:status => response.code })
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
describe "Webmock with Net:HTTP" do
|
26
|
+
|
27
|
+
it_should_behave_like "WebMock"
|
28
|
+
|
29
|
+
it "should work with block provided" do
|
30
|
+
stub_http_request(:get, "www.google.com").to_return(:body => "abc"*100000)
|
31
|
+
Net::HTTP.start("www.google.com") { |query| query.get("/") }.body.should == "abc"*100000
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "loading other Net::HTTP based libraries" do
|
4
|
+
|
5
|
+
def capture_output_from_requiring(libs, additional_code = "")
|
6
|
+
requires = libs.map { |lib| "require '#{lib}'" }.join("; ")
|
7
|
+
webmock_dir = "#{File.dirname(__FILE__)}/../lib"
|
8
|
+
vendor_dirs = Dir["#{File.dirname(__FILE__)}/vendor/*/lib"]
|
9
|
+
load_path_opts = vendor_dirs.unshift(webmock_dir).map { |dir| "-I#{dir}" }.join(" ")
|
10
|
+
|
11
|
+
# TODO: use the same Ruby executable that this test was invoked with
|
12
|
+
`ruby #{load_path_opts} -e "#{requires}; #{additional_code}" 2>&1`
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should requiring samuel before webmock prints warning" do
|
16
|
+
output = capture_output_from_requiring %w(samuel webmock)
|
17
|
+
output.should match(%r(Warning: WebMock was loaded after Samuel))
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should requiring samuel after webmock does not print warning" do
|
21
|
+
output = capture_output_from_requiring %w(webmock samuel)
|
22
|
+
output.should be_empty
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should requiring right http connection before webmock and then connecting does not print warning" do
|
26
|
+
additional_code = "Net::HTTP.start('example.com')"
|
27
|
+
output = capture_output_from_requiring %w(right_http_connection webmock), additional_code
|
28
|
+
output.should be_empty
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should requiring right http connection after webmock and then connecting prints warning" do
|
32
|
+
additional_code = "Net::HTTP.start('example.com')"
|
33
|
+
output = capture_output_from_requiring %w(webmock right_http_connection), additional_code
|
34
|
+
output.should match(%r(Warning: RightHttpConnection was loaded after WebMock))
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe RequestExecutionVerifier do
|
4
|
+
before(:each) do
|
5
|
+
@verifier = RequestExecutionVerifier.new
|
6
|
+
@request_profile = mock(RequestProfile, :to_s => "www.google.com")
|
7
|
+
@verifier.request_profile = @request_profile
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
describe "failure message" do
|
12
|
+
|
13
|
+
it "should report failure message" do
|
14
|
+
@verifier.times_executed = 0
|
15
|
+
@verifier.expected_times_executed = 2
|
16
|
+
@verifier.failure_message.should == "The request www.google.com was expected to execute 2 times but it executed 0 times"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should report failure message correctly when executed times is one" do
|
20
|
+
@verifier.times_executed = 1
|
21
|
+
@verifier.expected_times_executed = 1
|
22
|
+
@verifier.failure_message.should == "The request www.google.com was expected to execute 1 time but it executed 1 time"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "verify" do
|
28
|
+
|
29
|
+
it "should succeed if request was executed expected number of times" do
|
30
|
+
RequestRegistry.instance.
|
31
|
+
should_receive(:times_executed).with(@request_profile).and_return(10)
|
32
|
+
@verifier.expected_times_executed = 10
|
33
|
+
@verifier.verify.should be_true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should fail if request was not executed expected number of times" do
|
37
|
+
RequestRegistry.instance.
|
38
|
+
should_receive(:times_executed).with(@request_profile).and_return(10)
|
39
|
+
@verifier.expected_times_executed = 5
|
40
|
+
@verifier.verify.should be_false
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def verify
|
46
|
+
@times_executed =
|
47
|
+
RequestRegistry.instance.times_executed(@request_profile)
|
48
|
+
@times_executed == @expected_times_executed
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
include WebMock
|
4
|
+
|
5
|
+
describe RequestProfile do
|
6
|
+
|
7
|
+
describe "initialization" do
|
8
|
+
|
9
|
+
it "should have assigned normalized uri" do
|
10
|
+
URL.should_receive(:normalize_uri).and_return("www.google.kom")
|
11
|
+
profile = RequestProfile.new(:get, "www.google.com")
|
12
|
+
profile.uri.should == "www.google.kom"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have assigned uri without normalization if uri is URI" do
|
16
|
+
URL.should_not_receive(:normalize_uri)
|
17
|
+
uri = URI.parse("www.google.com")
|
18
|
+
profile = RequestProfile.new(:get, uri)
|
19
|
+
profile.uri.should == uri
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should have assigned normalized headers" do
|
23
|
+
Utility.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
|
24
|
+
RequestProfile.new(:get, "www.google.com", nil, 'A' => 'a').headers.should == {'B' => 'b'}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should have assigned body" do
|
28
|
+
RequestProfile.new(:get, "www.google.com", "abc").body.should == "abc"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should report string" do
|
34
|
+
RequestProfile.new(:get, "www.google.com", "abc", {'A' => 'a', 'B' => 'b'}).to_s.should ==
|
35
|
+
"GET http://www.google.com/ with body 'abc' with headers {'A'=>'a', 'B'=>'b'}"
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
describe "with" do
|
40
|
+
before(:each) do
|
41
|
+
@request_profile = RequestProfile.new(:get, "www.google.com")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should assign body to request profile" do
|
45
|
+
@request_profile.with(:body => "abc")
|
46
|
+
@request_profile.body.should == "abc"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should assign normalized headers to request profile" do
|
50
|
+
Utility.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
|
51
|
+
@request_profile.with(:headers => {'A' => 'a'})
|
52
|
+
@request_profile.headers.should == {'B' => 'b'}
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "when matching" do
|
58
|
+
|
59
|
+
it "should match if url matches and method matches" do
|
60
|
+
RequestProfile.new(:get, "www.google.com").
|
61
|
+
should match(RequestProfile.new(:get, "www.google.com"))
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should match if url matches and method is any" do
|
65
|
+
RequestProfile.new(:get, "www.google.com").
|
66
|
+
should match(RequestProfile.new(:any, "www.google.com"))
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should not match if other request profile has different method" do
|
70
|
+
RequestProfile.new(:get, "www.google.com").
|
71
|
+
should_not match(RequestProfile.new(:post, "www.google.com"))
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should match if uri matches other uri" do
|
75
|
+
RequestProfile.new(:get, "www.google.com").
|
76
|
+
should match(RequestProfile.new(:get, "www.google.com"))
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should match if uri matches other regex uri" do
|
80
|
+
RequestProfile.new(:get, "www.google.com").
|
81
|
+
should match(RequestProfile.new(:get, /.*google.*/))
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should match for uris with same parameters" do
|
85
|
+
RequestProfile.new(:get, "www.google.com?a=1&b=2").
|
86
|
+
should match(RequestProfile.new(:get, "www.google.com?a=1&b=2"))
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should not match for uris with different parameters" do
|
90
|
+
RequestProfile.new(:get, "www.google.com?a=2&b=1").
|
91
|
+
should_not match(RequestProfile.new(:get, "www.google.com?a=1&b=2"))
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should match for parameters in different order" do
|
95
|
+
RequestProfile.new(:get, "www.google.com?a=1&b=2").
|
96
|
+
should match(RequestProfile.new(:get, "www.google.com?b=2&a=1"))
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should match for same bodies" do
|
100
|
+
RequestProfile.new(:get, "www.google.com", "abc").
|
101
|
+
should match(RequestProfile.new(:get, "www.google.com", "abc"))
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should not match for different bodies" do
|
105
|
+
RequestProfile.new(:get, "www.google.com", "abc").
|
106
|
+
should_not match(RequestProfile.new(:get, "www.google.com", "def"))
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should match is other has nil body" do
|
110
|
+
RequestProfile.new(:get, "www.google.com", "abc").
|
111
|
+
should match(RequestProfile.new(:get, "www.google.com", nil))
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should not match if other has empty body" do
|
115
|
+
RequestProfile.new(:get, "www.google.com", "abc").
|
116
|
+
should_not match(RequestProfile.new(:get, "www.google.com", ""))
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should match for same headers" do
|
120
|
+
RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg').
|
121
|
+
should match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg'))
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should not match for different values of the same header" do
|
125
|
+
RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg').
|
126
|
+
should_not match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/png'))
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should match if request has more headers than other" do
|
130
|
+
RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg', 'Content-Length' => '8888').
|
131
|
+
should match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg'))
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should not match if request has less headers that the other and all match" do
|
135
|
+
RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg').
|
136
|
+
should_not match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'))
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should match even is header keys or values are in different format" do
|
140
|
+
RequestProfile.new(:get, "www.google.com", nil, :ContentLength => 8888, 'content_type' => 'image/png').
|
141
|
+
should match(RequestProfile.new(:get, "www.google.com", nil, 'ContentLength' => '8888', 'Content-type' => 'image/png'))
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should match is other has nil headers" do
|
145
|
+
RequestProfile.new(:get, "www.google.com", nil, 'A' => 'a').
|
146
|
+
should match(RequestProfile.new(:get, "www.google.com", nil, nil))
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should not match if other has empty headers" do
|
150
|
+
RequestProfile.new(:get, "www.google.com", nil, 'A' => 'a').
|
151
|
+
should_not match(RequestProfile.new(:get, "www.google.com", nil, {}))
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should not match if profile has no headers but other has headers" do
|
155
|
+
RequestProfile.new(:get, "www.google.com", nil, nil).
|
156
|
+
should_not match(RequestProfile.new(:get, "www.google.com", nil, {'A'=>'a'}))
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should not match if profile has empty headers but other has headers" do
|
160
|
+
RequestProfile.new(:get, "www.google.com", nil, {}).
|
161
|
+
should_not match(RequestProfile.new(:get, "www.google.com", nil, {'A'=>'a'}))
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|