webmock 0.9.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +47 -2
- data/README.md +68 -6
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/webmock.rb +2 -2
- data/lib/webmock/adapters/rspec.rb +1 -1
- data/lib/webmock/adapters/rspec/matchers.rb +2 -2
- data/lib/webmock/adapters/rspec/{request_profile_matcher.rb → request_pattern_matcher.rb} +5 -5
- data/lib/webmock/adapters/rspec/webmock_matcher.rb +2 -2
- data/lib/webmock/config.rb +2 -1
- data/lib/webmock/http_lib_adapters/httpclient.rb +5 -4
- data/lib/webmock/http_lib_adapters/net_http.rb +5 -3
- data/lib/webmock/http_lib_adapters/patron.rb +82 -0
- data/lib/webmock/request_execution_verifier.rb +8 -8
- data/lib/webmock/request_pattern.rb +130 -0
- data/lib/webmock/request_registry.rb +4 -9
- data/lib/webmock/request_signature.rb +18 -37
- data/lib/webmock/request_stub.rb +17 -6
- data/lib/webmock/response.rb +87 -31
- data/lib/webmock/util/headers.rb +5 -0
- data/lib/webmock/webmock.rb +10 -6
- data/spec/httpclient_spec.rb +0 -1
- data/spec/httpclient_spec_helper.rb +11 -1
- data/spec/net_http_spec.rb +8 -1
- data/spec/net_http_spec_helper.rb +11 -1
- data/spec/patron_spec.rb +83 -0
- data/spec/patron_spec_helper.rb +44 -0
- data/spec/request_execution_verifier_spec.rb +8 -8
- data/spec/request_pattern_spec.rb +243 -0
- data/spec/request_registry_spec.rb +34 -19
- data/spec/request_signature_spec.rb +23 -191
- data/spec/request_stub_spec.rb +32 -11
- data/spec/response_spec.rb +98 -5
- data/spec/spec_helper.rb +8 -16
- data/spec/webmock_spec.rb +154 -49
- data/webmock.gemspec +14 -7
- metadata +21 -7
- data/lib/webmock/request.rb +0 -29
- data/lib/webmock/request_profile.rb +0 -50
- data/spec/request_profile_spec.rb +0 -68
data/lib/webmock/util/headers.rb
CHANGED
@@ -12,6 +12,11 @@ module WebMock
|
|
12
12
|
Hash[*array.flatten]
|
13
13
|
end
|
14
14
|
|
15
|
+
def self.sorted_headers_string(headers)
|
16
|
+
headers = WebMock::Util::Headers.normalize_headers(headers)
|
17
|
+
'{' + headers.inspect[1..-2].split(', ').sort.join(', ').gsub("\"","'").gsub("\\","") + '}'
|
18
|
+
end
|
19
|
+
|
15
20
|
def self.decode_userinfo_from_header(header)
|
16
21
|
header.sub(/^Basic /, "").unpack("m").first
|
17
22
|
end
|
data/lib/webmock/webmock.rb
CHANGED
@@ -8,18 +8,18 @@ module WebMock
|
|
8
8
|
alias_method :stub_http_request, :stub_request
|
9
9
|
|
10
10
|
def request(method, uri)
|
11
|
-
|
11
|
+
RequestPattern.new(method, uri)
|
12
12
|
end
|
13
13
|
|
14
14
|
def assert_requested(method, uri, options = {}, &block)
|
15
15
|
expected_times_executed = options.delete(:times) || 1
|
16
|
-
request =
|
16
|
+
request = RequestPattern.new(method, uri, options).with(&block)
|
17
17
|
verifier = RequestExecutionVerifier.new(request, expected_times_executed)
|
18
18
|
assertion_failure(verifier.failure_message) unless verifier.matches?
|
19
19
|
end
|
20
20
|
|
21
21
|
def assert_not_requested(method, uri, options = {}, &block)
|
22
|
-
request =
|
22
|
+
request = RequestPattern.new(method, uri, options).with(&block)
|
23
23
|
verifier = RequestExecutionVerifier.new(request, options.delete(:times))
|
24
24
|
assertion_failure(verifier.negative_failure_message) unless verifier.does_not_match?
|
25
25
|
end
|
@@ -28,12 +28,16 @@ module WebMock
|
|
28
28
|
Config.instance.allow_net_connect = true
|
29
29
|
end
|
30
30
|
|
31
|
-
def disable_net_connect!
|
31
|
+
def disable_net_connect!(options = {})
|
32
32
|
Config.instance.allow_net_connect = false
|
33
|
+
Config.instance.allow_localhost = options[:allow_localhost]
|
33
34
|
end
|
34
35
|
|
35
|
-
def net_connect_allowed?
|
36
|
-
|
36
|
+
def net_connect_allowed?(uri = nil)
|
37
|
+
if uri.class == String
|
38
|
+
uri = URI::parse(uri)
|
39
|
+
end
|
40
|
+
Config.instance.allow_net_connect || ( Config.instance.allow_localhost && uri.is_a?(URI) && uri.host == 'localhost' )
|
37
41
|
end
|
38
42
|
|
39
43
|
def registered_request?(request_signature)
|
data/spec/httpclient_spec.rb
CHANGED
@@ -20,7 +20,17 @@ module HTTPClientSpecHelper
|
|
20
20
|
OpenStruct.new({
|
21
21
|
:body => HTTPClientSpecHelper.async_mode ? response.content.read : response.content,
|
22
22
|
:headers => Hash[*response.header.all.flatten],
|
23
|
-
:status => response.code.to_s
|
23
|
+
:status => response.code.to_s,
|
24
|
+
:message => response.reason
|
25
|
+
})
|
26
|
+
end
|
27
|
+
|
28
|
+
def client_timeout_exception_class
|
29
|
+
HTTPClient::TimeoutError
|
30
|
+
end
|
31
|
+
|
32
|
+
def connection_refused_exception_class
|
33
|
+
Errno::ECONNREFUSED
|
24
34
|
end
|
25
35
|
|
26
36
|
def default_client_request_headers(request_method = nil, has_body = false)
|
data/spec/net_http_spec.rb
CHANGED
@@ -29,7 +29,7 @@ describe "Webmock with Net:HTTP" do
|
|
29
29
|
req.body = "my_params"
|
30
30
|
Net::HTTP.start("www.example.com") { |http| http.request(req)}.body.should == "abc"
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
it "should handle Net::HTTP::Post#body_stream" do
|
34
34
|
stub_http_request(:post, "www.example.com").with(:body => "my_params").to_return(:body => "abc")
|
35
35
|
req = Net::HTTP::Post.new("/")
|
@@ -46,5 +46,12 @@ describe "Webmock with Net:HTTP" do
|
|
46
46
|
}.should raise_error("both of body argument and HTTPRequest#body set")
|
47
47
|
end
|
48
48
|
|
49
|
+
it "should handle real requests with readable body" do
|
50
|
+
WebMock.allow_net_connect!
|
51
|
+
req = Net::HTTP::Post.new("/")
|
52
|
+
Net::HTTP.start("www.example.com") {|http|
|
53
|
+
http.request(req, StringIO.new("my_params"))
|
54
|
+
}.body.should =~ /Example Web Page/
|
55
|
+
end
|
49
56
|
|
50
57
|
end
|
@@ -21,7 +21,9 @@ module NetHTTPSpecHelper
|
|
21
21
|
OpenStruct.new({
|
22
22
|
:body => response.body,
|
23
23
|
:headers => WebMock::Util::Headers.normalize_headers(headers),
|
24
|
-
:status => response.code
|
24
|
+
:status => response.code,
|
25
|
+
:message => response.message
|
26
|
+
})
|
25
27
|
end
|
26
28
|
|
27
29
|
def default_client_request_headers(request_method = nil, has_body = false)
|
@@ -30,6 +32,14 @@ module NetHTTPSpecHelper
|
|
30
32
|
[k, v.flatten]
|
31
33
|
}.flatten]
|
32
34
|
end
|
35
|
+
|
36
|
+
def client_timeout_exception_class
|
37
|
+
Timeout::Error
|
38
|
+
end
|
39
|
+
|
40
|
+
def connection_refused_exception_class
|
41
|
+
Errno::ECONNREFUSED
|
42
|
+
end
|
33
43
|
|
34
44
|
# Sets several expectations that a real HTTP request makes it
|
35
45
|
# past WebMock to the socket layer. You can use this when you need to check
|
data/spec/patron_spec.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'webmock_spec'
|
3
|
+
|
4
|
+
|
5
|
+
unless RUBY_PLATFORM =~ /java/
|
6
|
+
require 'patron_spec_helper'
|
7
|
+
require 'tmpdir'
|
8
|
+
require 'fileutils'
|
9
|
+
|
10
|
+
describe "Webmock with Patron" do
|
11
|
+
include PatronSpecHelper
|
12
|
+
|
13
|
+
it_should_behave_like "WebMock"
|
14
|
+
|
15
|
+
describe "when custom functionality is used" do
|
16
|
+
before(:each) do
|
17
|
+
@sess = Patron::Session.new
|
18
|
+
@sess.base_url = "http://www.example.com"
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "file requests" do
|
22
|
+
|
23
|
+
before(:each) do
|
24
|
+
@dir_path = Dir.tmpdir
|
25
|
+
@file_path = File.join(@dir_path, "webmock_temp_test_file")
|
26
|
+
FileUtils.rm_rf(@file_path) if File.exists?(@file_path)
|
27
|
+
end
|
28
|
+
|
29
|
+
after(:each) do
|
30
|
+
FileUtils.rm_rf(@dir_path) if File.exist?(@dir_path)
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
it "should work with get_file" do
|
35
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
36
|
+
@sess.get_file("/", @file_path)
|
37
|
+
File.read(@file_path).should == "abc"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should raise same error as Patron if file is not readable for get request" do
|
41
|
+
stub_http_request(:get, "www.example.com")
|
42
|
+
lambda {
|
43
|
+
@sess.get_file("/", "/non_existing_file")
|
44
|
+
}.should raise_error(ArgumentError, "Unable to open specified file.")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should work with put_file" do
|
48
|
+
File.open(@file_path, "w") {|f| f.write "abc"}
|
49
|
+
stub_http_request(:put, "www.example.com").with(:body => "abc")
|
50
|
+
@sess.put_file("/", @file_path)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should work with post_file" do
|
54
|
+
File.open(@file_path, "w") {|f| f.write "abc"}
|
55
|
+
stub_http_request(:post, "www.example.com").with(:body => "abc")
|
56
|
+
@sess.post_file("/", @file_path)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should raise same error as Patron if file is not readable for post request" do
|
60
|
+
stub_http_request(:post, "www.example.com").with(:body => "abc")
|
61
|
+
lambda {
|
62
|
+
@sess.post_file("/", "/non_existing_file")
|
63
|
+
}.should raise_error(ArgumentError, "Unable to open specified file.")
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "handling errors same way as patron" do
|
69
|
+
it "should raise error if put request has neither upload_data nor file_name" do
|
70
|
+
stub_http_request(:post, "www.example.com")
|
71
|
+
lambda {
|
72
|
+
@sess.post("/", nil)
|
73
|
+
}.should raise_error(ArgumentError, "Must provide either data or a filename when doing a PUT or POST")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should work with WebDAV copy request" do
|
78
|
+
stub_http_request(:copy, "www.example.com/abc").with(:headers => {'Destination' => "/def"})
|
79
|
+
@sess.copy("/abc", "/def")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module PatronSpecHelper
|
2
|
+
def http_request(method, uri, options = {}, &block)
|
3
|
+
uri = Addressable::URI.heuristic_parse(uri)
|
4
|
+
sess = Patron::Session.new
|
5
|
+
sess.base_url = "#{uri.omit(:userinfo, :query).normalize.to_s}".gsub(/\/$/,"")
|
6
|
+
|
7
|
+
sess.username = uri.user
|
8
|
+
sess.password = uri.password
|
9
|
+
|
10
|
+
sess.timeout = 10
|
11
|
+
|
12
|
+
response = sess.request(method, "#{uri.path}#{uri.query ? '?' : ''}#{uri.query}", options[:headers] || {}, {
|
13
|
+
:data => options[:body]
|
14
|
+
})
|
15
|
+
|
16
|
+
OpenStruct.new({
|
17
|
+
:body => response.body,
|
18
|
+
:headers => WebMock::Util::Headers.normalize_headers(response.headers),
|
19
|
+
:status => response.status.to_s,
|
20
|
+
:message => response.status_line
|
21
|
+
})
|
22
|
+
end
|
23
|
+
|
24
|
+
def default_client_request_headers(request_method = nil, has_body = false)
|
25
|
+
if Patron.version >= "0.4.6"
|
26
|
+
{'Expect'=>''}
|
27
|
+
else
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def client_timeout_exception_class
|
33
|
+
Patron::TimeoutError
|
34
|
+
end
|
35
|
+
|
36
|
+
def connection_refused_exception_class
|
37
|
+
Patron::ConnectionFailed
|
38
|
+
end
|
39
|
+
|
40
|
+
def setup_expectations_for_real_request(options = {})
|
41
|
+
#TODO
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -3,8 +3,8 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
3
3
|
describe RequestExecutionVerifier do
|
4
4
|
before(:each) do
|
5
5
|
@verifier = RequestExecutionVerifier.new
|
6
|
-
@
|
7
|
-
@verifier.
|
6
|
+
@request_pattern = mock(RequestPattern, :to_s => "www.example.com")
|
7
|
+
@verifier.request_pattern = @request_pattern
|
8
8
|
end
|
9
9
|
|
10
10
|
|
@@ -43,14 +43,14 @@ describe RequestExecutionVerifier do
|
|
43
43
|
|
44
44
|
it "should succeed if request was executed expected number of times" do
|
45
45
|
RequestRegistry.instance.
|
46
|
-
should_receive(:times_executed).with(@
|
46
|
+
should_receive(:times_executed).with(@request_pattern).and_return(10)
|
47
47
|
@verifier.expected_times_executed = 10
|
48
48
|
@verifier.matches?.should be_true
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should fail if request was not executed expected number of times" do
|
52
52
|
RequestRegistry.instance.
|
53
|
-
should_receive(:times_executed).with(@
|
53
|
+
should_receive(:times_executed).with(@request_pattern).and_return(10)
|
54
54
|
@verifier.expected_times_executed = 5
|
55
55
|
@verifier.matches?.should be_false
|
56
56
|
end
|
@@ -61,26 +61,26 @@ describe RequestExecutionVerifier do
|
|
61
61
|
|
62
62
|
it "should fail if request executed expected number of times" do
|
63
63
|
RequestRegistry.instance.
|
64
|
-
should_receive(:times_executed).with(@
|
64
|
+
should_receive(:times_executed).with(@request_pattern).and_return(10)
|
65
65
|
@verifier.expected_times_executed = 10
|
66
66
|
@verifier.does_not_match?.should be_false
|
67
67
|
end
|
68
68
|
|
69
69
|
it "should succeed if request was not executed at all and expected number of times was not set" do
|
70
70
|
RequestRegistry.instance.
|
71
|
-
should_receive(:times_executed).with(@
|
71
|
+
should_receive(:times_executed).with(@request_pattern).and_return(0)
|
72
72
|
@verifier.does_not_match?.should be_true
|
73
73
|
end
|
74
74
|
|
75
75
|
it "should fail if request was executed and expected number of times was not set" do
|
76
76
|
RequestRegistry.instance.
|
77
|
-
should_receive(:times_executed).with(@
|
77
|
+
should_receive(:times_executed).with(@request_pattern).and_return(1)
|
78
78
|
@verifier.does_not_match?.should be_false
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should succeed if request was not executed expected number of times" do
|
82
82
|
RequestRegistry.instance.
|
83
|
-
should_receive(:times_executed).with(@
|
83
|
+
should_receive(:times_executed).with(@request_pattern).and_return(10)
|
84
84
|
@verifier.expected_times_executed = 5
|
85
85
|
@verifier.does_not_match?.should be_true
|
86
86
|
end
|
@@ -0,0 +1,243 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe RequestPattern do
|
4
|
+
|
5
|
+
it "should report string describing itself" do
|
6
|
+
RequestPattern.new(:get, "www.example.com",
|
7
|
+
:body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).to_s.should ==
|
8
|
+
"GET http://www.example.com/ with body 'abc' with headers {'A'=>'a', 'B'=>'b'}"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should report string describing itself with block" do
|
12
|
+
RequestPattern.new(:get, "www.example.com",
|
13
|
+
:body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).with {|req| true}.to_s.should ==
|
14
|
+
"GET http://www.example.com/ with body 'abc' with headers {'A'=>'a', 'B'=>'b'} with given block"
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "with" do
|
18
|
+
before(:each) do
|
19
|
+
@request_pattern = RequestPattern.new(:get, "www.example.com")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should have assigned body pattern" do
|
23
|
+
@request_pattern.with(:body => "abc")
|
24
|
+
@request_pattern.to_s.should == RequestPattern.new(:get, "www.example.com", :body => "abc").to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should have assigned normalized headers pattern" do
|
28
|
+
@request_pattern.with(:headers => {'A' => 'a'})
|
29
|
+
@request_pattern.to_s.should == RequestPattern.new(:get, "www.example.com", :headers => {'A' => 'a'}).to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
class WebMock::RequestPattern
|
36
|
+
def match(request_signature)
|
37
|
+
self.matches?(request_signature)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "when matching" do
|
42
|
+
|
43
|
+
it "should match if uri matches and method matches" do
|
44
|
+
RequestPattern.new(:get, "www.example.com").
|
45
|
+
should match(RequestSignature.new(:get, "www.example.com"))
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should match if uri matches and method pattern is any" do
|
49
|
+
RequestPattern.new(:any, "www.example.com").
|
50
|
+
should match(RequestSignature.new(:get, "www.example.com"))
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should not match if request has different method" do
|
54
|
+
RequestPattern.new(:post, "www.example.com").
|
55
|
+
should_not match(RequestSignature.new(:get, "www.example.com"))
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should match if uri matches request uri" do
|
59
|
+
RequestPattern.new(:get, "www.example.com").
|
60
|
+
should match(RequestSignature.new(:get, "www.example.com"))
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should match if request has unescaped uri" do
|
64
|
+
RequestPattern.new(:get, "www.example.com/my%20path").
|
65
|
+
should match(RequestSignature.new(:get, "www.example.com/my path"))
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should match if request has escaped uri" do
|
69
|
+
RequestPattern.new(:get, "www.example.com/my path").
|
70
|
+
should match(RequestSignature.new(:get, "www.example.com/my%20path"))
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should match if uri regexp pattern matches unescaped form of request uri" do
|
74
|
+
RequestPattern.new(:get, /.*my path.*/).
|
75
|
+
should match(RequestSignature.new(:get, "www.example.com/my%20path"))
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should match if uri regexp pattern matches request uri" do
|
79
|
+
RequestPattern.new(:get, /.*example.*/).
|
80
|
+
should match(RequestSignature.new(:get, "www.example.com"))
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should match for uris with same parameters as pattern" do
|
84
|
+
RequestPattern.new(:get, "www.example.com?a=1&b=2").
|
85
|
+
should match(RequestSignature.new(:get, "www.example.com?a=1&b=2"))
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should not match for uris with different parameters" do
|
89
|
+
RequestPattern.new(:get, "www.example.com?a=1&b=2").
|
90
|
+
should_not match(RequestSignature.new(:get, "www.example.com?a=2&b=1"))
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should match for uri parameters in different order" do
|
94
|
+
RequestPattern.new(:get, "www.example.com?b=2&a=1").
|
95
|
+
should match(RequestSignature.new(:get, "www.example.com?a=1&b=2"))
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "when parameters are escaped" do
|
99
|
+
|
100
|
+
it "should match if uri pattern has escaped parameters and request has unescaped parameters" do
|
101
|
+
RequestPattern.new(:get, "www.example.com/?a=a%20b").
|
102
|
+
should match(RequestSignature.new(:get, "www.example.com/?a=a b"))
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should match if uri pattern has unescaped parameters and request has escaped parameters" do
|
106
|
+
RequestPattern.new(:get, "www.example.com/?a=a b").
|
107
|
+
should match(RequestSignature.new(:get, "www.example.com/?a=a%20b"))
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should match if uri regexp pattern matches uri with unescaped parameters and request has escaped parameters" do
|
111
|
+
RequestPattern.new(:get, /.*a=a b.*/).
|
112
|
+
should match(RequestSignature.new(:get, "www.example.com/?a=a%20b"))
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should match if uri regexp pattern matches uri with escaped parameters and request has unescaped parameters" do
|
116
|
+
RequestPattern.new(:get, /.*a=a%20b.*/).
|
117
|
+
should match(RequestSignature.new(:get, "www.example.com/?a=a b"))
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
it "should match if request body and body pattern are the same" do
|
125
|
+
RequestPattern.new(:get, "www.example.com", :body => "abc").
|
126
|
+
should match(RequestSignature.new(:get, "www.example.com", :body => "abc"))
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should match if request body matches regexp" do
|
130
|
+
RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
|
131
|
+
should match(RequestSignature.new(:get, "www.example.com", :body => "abc"))
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should not match if body pattern is different than request body" do
|
135
|
+
RequestPattern.new(:get, "www.example.com", :body => "def").
|
136
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :body => "abc"))
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should not match if request body doesn't match regexp pattern" do
|
140
|
+
RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
|
141
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :body => "xabc"))
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should match if pattern doesn't have specified body" do
|
145
|
+
RequestPattern.new(:get, "www.example.com").
|
146
|
+
should match(RequestSignature.new(:get, "www.example.com", :body => "abc"))
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should not match if pattern has body specified as nil but request body is not empty" do
|
150
|
+
RequestPattern.new(:get, "www.example.com", :body => nil).
|
151
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :body => "abc"))
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should not match if pattern has empty body but request body is not empty" do
|
155
|
+
RequestPattern.new(:get, "www.example.com", :body => "").
|
156
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :body => "abc"))
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should not match if pattern has body specified but request has no body" do
|
160
|
+
RequestPattern.new(:get, "www.example.com", :body => "abc").
|
161
|
+
should_not match(RequestSignature.new(:get, "www.example.com"))
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should match if pattern and request have the same headers" do
|
165
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
|
166
|
+
should match(RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should match if pattern headers values are regexps matching request header values" do
|
170
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image/jpeg$}}).
|
171
|
+
should match(RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should not match if pattern has different value of header than request" do
|
175
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/png'}).
|
176
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should not match if pattern header value regexp doesn't match request header value" do
|
180
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image\/jpeg$}}).
|
181
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpegx'}))
|
182
|
+
end
|
183
|
+
|
184
|
+
it "should match if request has more headers than request pattern" do
|
185
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
|
186
|
+
should match(RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}))
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should not match if request has less headers than the request pattern" do
|
190
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}).
|
191
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should match even is header keys are declared in different form" do
|
195
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'ContentLength' => '8888', 'Content-type' => 'image/png'}).
|
196
|
+
should match(RequestSignature.new(:get, "www.example.com", :headers => {:ContentLength => 8888, 'content_type' => 'image/png'}))
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should match is pattern doesn't have specified headers" do
|
200
|
+
RequestPattern.new(:get, "www.example.com").
|
201
|
+
should match(RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}))
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should not match if pattern has nil headers but request has headers" do
|
205
|
+
RequestPattern.new(:get, "www.example.com", :headers => nil).
|
206
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}))
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should not match if pattern has empty headers but request has headers" do
|
210
|
+
RequestPattern.new(:get, "www.example.com", :headers => {}).
|
211
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}))
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should not match if pattern has specified headers but request has nil headers" do
|
215
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
|
216
|
+
should_not match(RequestSignature.new(:get, "www.example.com"))
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should not match if pattern has specified headers but request has empty headers" do
|
220
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
|
221
|
+
should_not match(RequestSignature.new(:get, "www.example.com", :headers => {}))
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should match if block given in pattern evaluates request to true" do
|
225
|
+
RequestPattern.new(:get, "www.example.com").with { |request| true }.
|
226
|
+
should match(RequestSignature.new(:get, "www.example.com"))
|
227
|
+
end
|
228
|
+
|
229
|
+
it "should not match if block given in pattrn evaluates request to false" do
|
230
|
+
RequestPattern.new(:get, "www.example.com").with { |request| false }.
|
231
|
+
should_not match(RequestSignature.new(:get, "www.example.com"))
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should yield block with request signature" do
|
235
|
+
signature = RequestSignature.new(:get, "www.example.com")
|
236
|
+
RequestPattern.new(:get, "www.example.com").with { |request| request == signature }.
|
237
|
+
should match(signature)
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
|
243
|
+
end
|