webmock 2.0.3 → 2.1.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile +2 -7
- data/README.md +54 -53
- data/Rakefile +1 -1
- data/lib/webmock/callback_registry.rb +1 -1
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +68 -6
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +4 -4
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +9 -9
- data/lib/webmock/http_lib_adapters/http_rb/request.rb +2 -2
- data/lib/webmock/http_lib_adapters/http_rb/response.rb +5 -5
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +3 -3
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +5 -5
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +7 -7
- data/lib/webmock/http_lib_adapters/net_http.rb +3 -3
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +4 -4
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +14 -14
- data/lib/webmock/matchers/hash_including_matcher.rb +1 -1
- data/lib/webmock/rack_response.rb +3 -3
- data/lib/webmock/request_pattern.rb +8 -8
- data/lib/webmock/request_stub.rb +5 -5
- data/lib/webmock/util/uri.rb +2 -2
- data/lib/webmock/version.rb +1 -1
- data/minitest/webmock_spec.rb +3 -3
- data/spec/acceptance/curb/curb_spec.rb +64 -47
- data/spec/acceptance/curb/curb_spec_helper.rb +4 -4
- data/spec/acceptance/em_http_request/em_http_request_spec.rb +32 -32
- data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +9 -9
- data/spec/acceptance/excon/excon_spec.rb +14 -14
- data/spec/acceptance/excon/excon_spec_helper.rb +6 -6
- data/spec/acceptance/http_rb/http_rb_spec.rb +1 -1
- data/spec/acceptance/http_rb/http_rb_spec_helper.rb +7 -7
- data/spec/acceptance/httpclient/httpclient_spec.rb +20 -20
- data/spec/acceptance/httpclient/httpclient_spec_helper.rb +5 -5
- data/spec/acceptance/manticore/manticore_spec.rb +11 -11
- data/spec/acceptance/manticore/manticore_spec_helper.rb +3 -3
- data/spec/acceptance/net_http/net_http_shared.rb +4 -4
- data/spec/acceptance/net_http/net_http_spec.rb +25 -25
- data/spec/acceptance/net_http/net_http_spec_helper.rb +4 -4
- data/spec/acceptance/net_http/real_net_http_spec.rb +1 -1
- data/spec/acceptance/patron/patron_spec.rb +10 -10
- data/spec/acceptance/patron/patron_spec_helper.rb +5 -5
- data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +36 -36
- data/spec/acceptance/shared/callbacks.rb +11 -11
- data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +5 -5
- data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +4 -4
- data/spec/acceptance/shared/precedence_of_stubs.rb +4 -4
- data/spec/acceptance/shared/request_expectations.rb +90 -90
- data/spec/acceptance/shared/returning_declared_responses.rb +49 -49
- data/spec/acceptance/shared/stubbing_requests.rb +78 -78
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +11 -11
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +8 -8
- data/spec/spec_helper.rb +6 -3
- data/spec/support/webmock_server.rb +1 -1
- data/spec/unit/api_spec.rb +2 -2
- data/spec/unit/errors_spec.rb +3 -3
- data/spec/unit/matchers/hash_including_matcher_spec.rb +16 -16
- data/spec/unit/rack_response_spec.rb +2 -2
- data/spec/unit/request_body_diff_spec.rb +1 -1
- data/spec/unit/request_execution_verifier_spec.rb +1 -1
- data/spec/unit/request_pattern_spec.rb +115 -115
- data/spec/unit/request_signature_snippet_spec.rb +2 -2
- data/spec/unit/request_signature_spec.rb +16 -16
- data/spec/unit/request_stub_spec.rb +20 -20
- data/spec/unit/response_spec.rb +28 -28
- data/spec/unit/stub_registry_spec.rb +10 -10
- data/spec/unit/stub_request_snippet_spec.rb +6 -6
- data/spec/unit/util/hash_keys_stringifier_spec.rb +4 -4
- data/spec/unit/util/query_mapper_spec.rb +2 -2
- data/spec/unit/util/uri_spec.rb +1 -1
- data/test/http_request.rb +3 -3
- data/test/shared_test.rb +6 -6
- data/webmock.gemspec +4 -2
- metadata +32 -4
@@ -22,7 +22,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
22
22
|
|
23
23
|
describe "supposed response fields" do
|
24
24
|
it "present" do
|
25
|
-
stub_request(:get, "http://www.example.com").to_return(:
|
25
|
+
stub_request(:get, "http://www.example.com").to_return(headers: {'X-Test' => '1'})
|
26
26
|
response = Typhoeus.get("http://www.example.com")
|
27
27
|
expect(response.code).not_to be_nil
|
28
28
|
expect(response.status_message).not_to be_nil
|
@@ -34,21 +34,21 @@ unless RUBY_PLATFORM =~ /java/
|
|
34
34
|
|
35
35
|
describe "when params are used" do
|
36
36
|
it "should take into account params for POST request" do
|
37
|
-
stub_request(:post, "www.example.com/?hello=world").with(:
|
38
|
-
request = Typhoeus::Request.new("http://www.example.com", :
|
37
|
+
stub_request(:post, "www.example.com/?hello=world").with(query: {hello: 'world'})
|
38
|
+
request = Typhoeus::Request.new("http://www.example.com", method: :post, params: {hello: 'world'})
|
39
39
|
hydra.queue(request)
|
40
40
|
hydra.run
|
41
41
|
end
|
42
42
|
|
43
43
|
it "should take into account body for POST request" do
|
44
|
-
stub_request(:post, "www.example.com").with(:
|
45
|
-
response = Typhoeus.post("http://www.example.com", :
|
44
|
+
stub_request(:post, "www.example.com").with(body: {hello: 'world'})
|
45
|
+
response = Typhoeus.post("http://www.example.com", method: :post, body: {hello: 'world'})
|
46
46
|
expect(response.code).to eq(200)
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should take into account params for GET request" do
|
50
50
|
stub_request(:get, "http://www.example.com/?hello=world").to_return({})
|
51
|
-
request = Typhoeus::Request.new("http://www.example.com/?hello=world", :
|
51
|
+
request = Typhoeus::Request.new("http://www.example.com/?hello=world", method: :get)
|
52
52
|
hydra.queue(request)
|
53
53
|
hydra.run
|
54
54
|
end
|
@@ -58,7 +58,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
58
58
|
it "should support native typhoeus timeouts" do
|
59
59
|
stub_request(:any, "example.com").to_timeout
|
60
60
|
|
61
|
-
request = Typhoeus::Request.new("http://example.com", :
|
61
|
+
request = Typhoeus::Request.new("http://example.com", method: :get)
|
62
62
|
hydra.queue(request)
|
63
63
|
hydra.run
|
64
64
|
|
@@ -73,7 +73,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
73
73
|
|
74
74
|
it "should call on_complete with 2xx response" do
|
75
75
|
body = "on_success fired"
|
76
|
-
stub_request(:any, "example.com").to_return(:
|
76
|
+
stub_request(:any, "example.com").to_return(body: body)
|
77
77
|
|
78
78
|
test = nil
|
79
79
|
Typhoeus.on_complete do |c|
|
@@ -86,7 +86,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
86
86
|
|
87
87
|
it "should call on_complete with 5xx response" do
|
88
88
|
response_code = 599
|
89
|
-
stub_request(:any, "example.com").to_return(:
|
89
|
+
stub_request(:any, "example.com").to_return(status: [response_code, "Server On Fire"])
|
90
90
|
|
91
91
|
test = nil
|
92
92
|
Typhoeus.on_complete do |c|
|
@@ -99,7 +99,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
99
99
|
|
100
100
|
it "should call on_body with 2xx response" do
|
101
101
|
body = "on_body fired"
|
102
|
-
stub_request(:any, "example.com").to_return(:
|
102
|
+
stub_request(:any, "example.com").to_return(body: body)
|
103
103
|
|
104
104
|
test_body = nil
|
105
105
|
test_complete = nil
|
@@ -118,7 +118,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
118
118
|
|
119
119
|
it "should call on_headers with 2xx response" do
|
120
120
|
body = "on_headers fired"
|
121
|
-
stub_request(:any, "example.com").to_return(:
|
121
|
+
stub_request(:any, "example.com").to_return(body: body, headers: {'X-Test' => '1'})
|
122
122
|
|
123
123
|
test_headers = nil
|
124
124
|
skip("This test requires a newer version of Typhoeus") unless @request.respond_to?(:on_headers)
|
@@ -8,10 +8,10 @@ module TyphoeusHydraSpecHelper
|
|
8
8
|
def http_request(method, uri, options = {}, &block)
|
9
9
|
uri.gsub!(" ", "%20") #typhoeus doesn't like spaces in the uri
|
10
10
|
request_options = {
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
14
|
-
:
|
11
|
+
method: method,
|
12
|
+
body: options[:body],
|
13
|
+
headers: options[:headers],
|
14
|
+
timeout: 25000
|
15
15
|
}
|
16
16
|
if options[:basic_auth]
|
17
17
|
request_options[:userpwd] = options[:basic_auth].join(':')
|
@@ -26,10 +26,10 @@ module TyphoeusHydraSpecHelper
|
|
26
26
|
raise FakeTyphoeusHydraConnectError.new if response.return_code == :couldnt_connect
|
27
27
|
raise FakeTyphoeusHydraTimeoutError.new if response.timed_out?
|
28
28
|
OpenStruct.new({
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:
|
32
|
-
:
|
29
|
+
body: response.body,
|
30
|
+
headers: WebMock::Util::Headers.normalize_headers(join_array_values(response.headers)),
|
31
|
+
status: response.code.to_s,
|
32
|
+
message: response.status_message
|
33
33
|
})
|
34
34
|
end
|
35
35
|
|
data/spec/spec_helper.rb
CHANGED
@@ -21,16 +21,19 @@ require 'support/webmock_server'
|
|
21
21
|
require 'support/my_rack_app'
|
22
22
|
require 'support/failures'
|
23
23
|
|
24
|
+
require 'simplecov'
|
25
|
+
SimpleCov.start
|
26
|
+
|
24
27
|
CURL_EXAMPLE_OUTPUT_PATH = File.expand_path('../support/example_curl_output.txt', __FILE__)
|
25
28
|
|
26
29
|
RSpec.configure do |config|
|
27
30
|
no_network_connection = ENV["NO_CONNECTION"] || ! NetworkConnection.is_network_available?
|
28
31
|
if no_network_connection
|
29
32
|
warn("No network connectivity. Only examples which do not make real network connections will run.")
|
30
|
-
config.filter_run_excluding :
|
33
|
+
config.filter_run_excluding net_connect: true
|
31
34
|
end
|
32
35
|
|
33
|
-
config.filter_run_excluding :
|
36
|
+
config.filter_run_excluding without_webmock: true
|
34
37
|
|
35
38
|
config.before(:suite) do
|
36
39
|
WebMockServer.instance.start unless WebMockServer.instance.started
|
@@ -40,7 +43,7 @@ RSpec.configure do |config|
|
|
40
43
|
WebMockServer.instance.stop
|
41
44
|
end
|
42
45
|
|
43
|
-
config.filter_run :
|
46
|
+
config.filter_run focus: true
|
44
47
|
config.run_all_when_everything_filtered = true
|
45
48
|
|
46
49
|
config.include Failures
|
@@ -23,7 +23,7 @@ class WebMockServer
|
|
23
23
|
|
24
24
|
def start
|
25
25
|
@started = true
|
26
|
-
server = WEBrick::GenericServer.new(:
|
26
|
+
server = WEBrick::GenericServer.new(Port: 0, Logger: Logger.new("/dev/null"))
|
27
27
|
server.logger.level = 0
|
28
28
|
@port = server.config[:Port]
|
29
29
|
|
data/spec/unit/api_spec.rb
CHANGED
@@ -4,7 +4,7 @@ describe WebMock::API do
|
|
4
4
|
describe '#hash_including' do
|
5
5
|
|
6
6
|
subject { klass.new.hash_including(args) }
|
7
|
-
let(:args) { {:
|
7
|
+
let(:args) { {data: :one} }
|
8
8
|
|
9
9
|
context 'when mixed into a class that does not define `hash_including`' do
|
10
10
|
let(:klass) do
|
@@ -35,7 +35,7 @@ describe WebMock::API do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
context "when args are both keys and key/value pairs" do
|
38
|
-
subject {klass.new.hash_including(:foo, :bar, :
|
38
|
+
subject {klass.new.hash_including(:foo, :bar, data: :one)}
|
39
39
|
let(:anything) { WebMock::Matchers::AnyArgMatcher.new(nil) }
|
40
40
|
|
41
41
|
it "creates 'HashIncludingMatcher' with keys anythingized" do
|
data/spec/unit/errors_spec.rb
CHANGED
@@ -120,10 +120,10 @@ describe "errors" do
|
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
|
-
let(:request_signature) { double(:request_signature, :
|
124
|
-
let(:stub_result) { double(:stub_result, :
|
123
|
+
let(:request_signature) { double(:request_signature, to_s: rand(10**20).to_s) }
|
124
|
+
let(:stub_result) { double(:stub_result, to_s: rand(10**20).to_s) }
|
125
125
|
let(:request_stub) { double(:request_stub) }
|
126
|
-
let(:body_pattern) { double(:body_pattern, :
|
126
|
+
let(:body_pattern) { double(:body_pattern, body_pattern: nil)}
|
127
127
|
let(:body_diff) { [["+", "test", "test2"], ["-", "test3"], ["~", "test5", "test6"]] }
|
128
128
|
end
|
129
129
|
end
|
@@ -6,15 +6,15 @@ module WebMock
|
|
6
6
|
describe HashIncludingMatcher do
|
7
7
|
|
8
8
|
it "stringifies the given hash keys" do
|
9
|
-
expect(HashIncludingMatcher.new(:
|
9
|
+
expect(HashIncludingMatcher.new(a: 1, b: 2)).to eq("a" => 1, "b" => 2)
|
10
10
|
end
|
11
11
|
|
12
12
|
it "sorts elements in the hash" do
|
13
|
-
expect(HashIncludingMatcher.new(:
|
13
|
+
expect(HashIncludingMatcher.new(b: 2, a: 1)).to eq("a" => 1, "b" => 2)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "describes itself properly" do
|
17
|
-
expect(HashIncludingMatcher.new(:
|
17
|
+
expect(HashIncludingMatcher.new(a: 1).inspect).to eq "hash_including({\"a\"=>1})"
|
18
18
|
end
|
19
19
|
|
20
20
|
describe "success" do
|
@@ -23,63 +23,63 @@ module WebMock
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "matches a hash with extra stuff" do
|
26
|
-
expect(HashIncludingMatcher.new(:
|
26
|
+
expect(HashIncludingMatcher.new(a: 1)).to eq("a" => 1, "b" => 2)
|
27
27
|
end
|
28
28
|
|
29
29
|
describe "when matching anythingized keys" do
|
30
30
|
let(:anything) { WebMock::Matchers::AnyArgMatcher.new(nil) }
|
31
31
|
|
32
32
|
it "matches an int against anything()" do
|
33
|
-
expect(HashIncludingMatcher.new(:
|
33
|
+
expect(HashIncludingMatcher.new(a: anything, b: 2)).to eq({'a' => 1, 'b' => 2})
|
34
34
|
end
|
35
35
|
|
36
36
|
it "matches a string against anything()" do
|
37
|
-
expect(HashIncludingMatcher.new(:
|
37
|
+
expect(HashIncludingMatcher.new(a: anything, b: 2)).to eq({'a' => "1", 'b' => 2})
|
38
38
|
end
|
39
39
|
|
40
40
|
it "matches if the key is present" do
|
41
|
-
expect(HashIncludingMatcher.new(:
|
41
|
+
expect(HashIncludingMatcher.new(a: anything)).to eq({'a' => 1, 'b' => 2})
|
42
42
|
end
|
43
43
|
|
44
44
|
it "matches if more keys are present" do
|
45
|
-
expect(HashIncludingMatcher.new(:
|
45
|
+
expect(HashIncludingMatcher.new(a: anything, b: anything)).to eq({'a' => 1, 'b' => 2, 'c' => 3})
|
46
46
|
end
|
47
47
|
|
48
48
|
it "matches if passed many keys and many key/value pairs" do
|
49
|
-
expect(HashIncludingMatcher.new(:
|
49
|
+
expect(HashIncludingMatcher.new(a: anything, b: anything, c: 3, e: 5)).to eq({'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5})
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
describe "when matching an empty hash" do
|
54
54
|
it "matches against any hash" do
|
55
|
-
expect(HashIncludingMatcher.new({})).to eq({:
|
55
|
+
expect(HashIncludingMatcher.new({})).to eq({a: 1, b: 2, c: 3})
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
60
|
describe "failing" do
|
61
61
|
it "does not match a non-hash" do
|
62
|
-
expect(HashIncludingMatcher.new(:
|
62
|
+
expect(HashIncludingMatcher.new(a: 1)).not_to eq 1
|
63
63
|
end
|
64
64
|
|
65
65
|
it "does not match a hash with a missing key" do
|
66
|
-
expect(HashIncludingMatcher.new(:
|
66
|
+
expect(HashIncludingMatcher.new(a: 1)).not_to eq('b' => 2)
|
67
67
|
end
|
68
68
|
|
69
69
|
it "does not match an empty hash with a given key" do
|
70
|
-
expect(HashIncludingMatcher.new(:
|
70
|
+
expect(HashIncludingMatcher.new(a: 1)).not_to eq({})
|
71
71
|
end
|
72
72
|
|
73
73
|
it "does not match a hash with a missing key when one pair is matching" do
|
74
|
-
expect(HashIncludingMatcher.new(:
|
74
|
+
expect(HashIncludingMatcher.new(a: 1, b: 2)).not_to eq('b' => 2)
|
75
75
|
end
|
76
76
|
|
77
77
|
it "does not match a hash with an incorrect value" do
|
78
|
-
expect(HashIncludingMatcher.new(:
|
78
|
+
expect(HashIncludingMatcher.new(a: 1, b: 2)).not_to eq('a' => 1, 'b' => 3)
|
79
79
|
end
|
80
80
|
|
81
81
|
it "does not match when values are nil but keys are different" do
|
82
|
-
expect(HashIncludingMatcher.new(:
|
82
|
+
expect(HashIncludingMatcher.new(a: nil)).not_to eq('b' => nil)
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
@@ -43,7 +43,7 @@ describe WebMock::RackResponse do
|
|
43
43
|
|
44
44
|
it "should send along POST params" do
|
45
45
|
request = WebMock::RequestSignature.new(:post, 'www.example.com/greet',
|
46
|
-
:
|
46
|
+
body: 'name=Jimmy'
|
47
47
|
)
|
48
48
|
|
49
49
|
response = @rack_response.evaluate(request)
|
@@ -52,7 +52,7 @@ describe WebMock::RackResponse do
|
|
52
52
|
|
53
53
|
it "should send params with proper content length if params have non-ascii symbols" do
|
54
54
|
request = WebMock::RequestSignature.new(:post, 'www.example.com/greet',
|
55
|
-
:
|
55
|
+
body: 'name=Олег'
|
56
56
|
)
|
57
57
|
|
58
58
|
response = @rack_response.evaluate(request)
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe WebMock::RequestExecutionVerifier do
|
4
4
|
before(:each) do
|
5
5
|
@verifier = WebMock::RequestExecutionVerifier.new
|
6
|
-
@request_pattern = double(WebMock::RequestPattern, :
|
6
|
+
@request_pattern = double(WebMock::RequestPattern, to_s: "www.example.com")
|
7
7
|
@verifier.request_pattern = @request_pattern
|
8
8
|
allow(WebMock::RequestRegistry.instance).to receive(:to_s).and_return("executed requests")
|
9
9
|
@executed_requests_info = "\n\nThe following requests were made:\n\nexecuted requests\n" + "="*60
|
@@ -5,34 +5,34 @@ describe WebMock::RequestPattern do
|
|
5
5
|
describe "describing itself" do
|
6
6
|
it "should report string describing itself" do
|
7
7
|
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
8
|
-
:
|
8
|
+
body: "abc", headers: {'A' => 'a', 'B' => 'b'}).to_s).to eq(
|
9
9
|
"GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'}"
|
10
10
|
)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should report string describing itself with block" do
|
14
14
|
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
15
|
-
:
|
15
|
+
body: "abc", headers: {'A' => 'a', 'B' => 'b'}).with {|req| true}.to_s).to eq(
|
16
16
|
"GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'} with given block"
|
17
17
|
)
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should report string describing itself with query params" do
|
21
|
-
expect(WebMock::RequestPattern.new(:get, /.*example.*/, :
|
21
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {'a' => ['b', 'c']}).to_s).to eq(
|
22
22
|
"GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"
|
23
23
|
)
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should report string describing itself with query params as hash including matcher" do
|
27
27
|
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
28
|
-
:
|
28
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(
|
29
29
|
"GET /.*example.*/ with query params hash_including({\"a\"=>[\"b\", \"c\"]})"
|
30
30
|
)
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should report string describing itself with body as hash including matcher" do
|
34
34
|
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
35
|
-
:
|
35
|
+
body: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(
|
36
36
|
"GET /.*example.*/ with body hash_including({\"a\"=>[\"b\", \"c\"]})"
|
37
37
|
)
|
38
38
|
end
|
@@ -44,17 +44,17 @@ describe WebMock::RequestPattern do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should have assigned body pattern" do
|
47
|
-
@request_pattern.with(:
|
48
|
-
expect(@request_pattern.to_s).to eq(WebMock::RequestPattern.new(:get, "www.example.com", :
|
47
|
+
@request_pattern.with(body: "abc")
|
48
|
+
expect(@request_pattern.to_s).to eq(WebMock::RequestPattern.new(:get, "www.example.com", body: "abc").to_s)
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should have assigned normalized headers pattern" do
|
52
|
-
@request_pattern.with(:
|
53
|
-
expect(@request_pattern.to_s).to eq(WebMock::RequestPattern.new(:get, "www.example.com", :
|
52
|
+
@request_pattern.with(headers: {'A' => 'a'})
|
53
|
+
expect(@request_pattern.to_s).to eq(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'A' => 'a'}).to_s)
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should raise an error if options passed to `with` are invalid" do
|
57
|
-
expect { @request_pattern.with(:
|
57
|
+
expect { @request_pattern.with(foo: "bar") }.to raise_error('Unknown key: "foo". Valid keys are: "body", "headers", "query", "basic_auth"')
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should raise an error if neither options or block is provided" do
|
@@ -184,118 +184,118 @@ describe WebMock::RequestPattern do
|
|
184
184
|
|
185
185
|
describe "when uri is described as regexp" do
|
186
186
|
it "should match request query params" do
|
187
|
-
expect(WebMock::RequestPattern.new(:get, /.*example.*/, :
|
187
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"a" => ["b", "c"]})).
|
188
188
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
189
189
|
end
|
190
190
|
|
191
191
|
it "should match request query params if params don't match" do
|
192
|
-
expect(WebMock::RequestPattern.new(:get, /.*example.*/, :
|
192
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"x" => ["b", "c"]})).
|
193
193
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
194
194
|
end
|
195
195
|
|
196
196
|
it "should match when query params are declared as HashIncluding matcher matching params" do
|
197
197
|
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
198
|
-
:
|
198
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
199
199
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
200
200
|
end
|
201
201
|
|
202
202
|
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
203
203
|
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
204
|
-
:
|
204
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
205
205
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
206
206
|
end
|
207
207
|
|
208
208
|
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
209
209
|
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
210
|
-
:
|
210
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
211
211
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
212
212
|
end
|
213
213
|
|
214
214
|
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
215
215
|
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
216
|
-
:
|
216
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
217
217
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
218
218
|
end
|
219
219
|
end
|
220
220
|
|
221
221
|
describe "when uri is described as Addressable::Template" do
|
222
222
|
it "should raise error if query params are specified" do
|
223
|
-
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), :
|
223
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"a" => ["b", "c"]})).
|
224
224
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
225
225
|
end
|
226
226
|
|
227
227
|
it "should match request query params if params don't match" do
|
228
|
-
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), :
|
228
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"x" => ["b", "c"]})).
|
229
229
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
230
230
|
end
|
231
231
|
|
232
232
|
it "should match when query params are declared as HashIncluding matcher matching params" do
|
233
233
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
|
234
|
-
:
|
234
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
235
235
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
236
236
|
end
|
237
237
|
|
238
238
|
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
239
239
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
|
240
|
-
:
|
240
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
241
241
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
242
242
|
end
|
243
243
|
|
244
244
|
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
245
245
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
|
246
|
-
:
|
246
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
247
247
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
248
248
|
end
|
249
249
|
|
250
250
|
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
251
251
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
|
252
|
-
:
|
252
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
253
253
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
254
254
|
end
|
255
255
|
end
|
256
256
|
|
257
257
|
describe "when uri is described as string" do
|
258
258
|
it "should match when query params are the same as declared in hash" do
|
259
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
259
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", query: {"a" => ["b", "c"]})).
|
260
260
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
261
261
|
end
|
262
262
|
|
263
263
|
it "should not match when query params are different than the declared in hash" do
|
264
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
264
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", query: {"a" => ["b", "c"]})).
|
265
265
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?x[]=b&a[]=c"))
|
266
266
|
end
|
267
267
|
|
268
268
|
it "should match when query params are the same as declared as string" do
|
269
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
269
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", query: "a[]=b&a[]=c")).
|
270
270
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
271
271
|
end
|
272
272
|
|
273
273
|
it "should match when query params are the same as declared both in query option or url" do
|
274
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com/?x=3", :
|
274
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com/?x=3", query: "a[]=b&a[]=c")).
|
275
275
|
to match(WebMock::RequestSignature.new(:get, "www.example.com/?x=3&a[]=b&a[]=c"))
|
276
276
|
end
|
277
277
|
|
278
278
|
it "should match when query params are declared as HashIncluding matcher matching params" do
|
279
279
|
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
280
|
-
:
|
280
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
281
281
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
282
282
|
end
|
283
283
|
|
284
284
|
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
285
285
|
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
286
|
-
:
|
286
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
287
287
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
288
288
|
end
|
289
289
|
|
290
290
|
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
291
291
|
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
292
|
-
:
|
292
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
293
293
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
294
294
|
end
|
295
295
|
|
296
296
|
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
297
297
|
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
298
|
-
:
|
298
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
299
299
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
300
300
|
end
|
301
301
|
|
@@ -305,7 +305,7 @@ describe WebMock::RequestPattern do
|
|
305
305
|
end
|
306
306
|
|
307
307
|
it "should not match when repeated query params are not the same as declared as string" do
|
308
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
308
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", query: "a=b&a=c")).
|
309
309
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a=b&a=c"))
|
310
310
|
end
|
311
311
|
|
@@ -319,42 +319,42 @@ describe WebMock::RequestPattern do
|
|
319
319
|
describe "when matching requests with body" do
|
320
320
|
|
321
321
|
it "should match if request body and body pattern are the same" do
|
322
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
323
|
-
to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
322
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "abc")).
|
323
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
324
324
|
end
|
325
325
|
|
326
326
|
it "should match if request body matches regexp" do
|
327
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
328
|
-
to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
327
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: /^abc$/)).
|
328
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
329
329
|
end
|
330
330
|
|
331
331
|
it "should not match if body pattern is different than request body" do
|
332
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
333
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
332
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "def")).
|
333
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
334
334
|
end
|
335
335
|
|
336
336
|
it "should not match if request body doesn't match regexp pattern" do
|
337
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
338
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
337
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: /^abc$/)).
|
338
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "xabc"))
|
339
339
|
end
|
340
340
|
|
341
341
|
it "should match if pattern doesn't have specified body" do
|
342
342
|
expect(WebMock::RequestPattern.new(:get, "www.example.com")).
|
343
|
-
to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
343
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
344
344
|
end
|
345
345
|
|
346
346
|
it "should not match if pattern has body specified as nil but request body is not empty" do
|
347
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
348
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
347
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: nil)).
|
348
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
349
349
|
end
|
350
350
|
|
351
351
|
it "should not match if pattern has empty body but request body is not empty" do
|
352
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
353
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
352
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "")).
|
353
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
354
354
|
end
|
355
355
|
|
356
356
|
it "should not match if pattern has body specified but request has no body" do
|
357
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
357
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "abc")).
|
358
358
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
359
359
|
end
|
360
360
|
|
@@ -363,89 +363,89 @@ describe WebMock::RequestPattern do
|
|
363
363
|
|
364
364
|
describe "for request with url encoded body" do
|
365
365
|
it "should match when hash matches body" do
|
366
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
367
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
366
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
367
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=1&c[d][]=e&c[d][]=f&b=five'))
|
368
368
|
end
|
369
369
|
|
370
370
|
it "should match when hash matches body in different order of params" do
|
371
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
372
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
371
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
372
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=1&c[d][]=e&b=five&c[d][]=f'))
|
373
373
|
end
|
374
374
|
|
375
375
|
it "should not match when hash doesn't match url encoded body" do
|
376
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
377
|
-
not_to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
376
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
377
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'c[d][]=f&a=1&c[d][]=e'))
|
378
378
|
end
|
379
379
|
|
380
380
|
it "should not match when body is not url encoded" do
|
381
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
382
|
-
not_to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
381
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
382
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'foo bar'))
|
383
383
|
end
|
384
384
|
|
385
385
|
it "should match when hash contains regex values" do
|
386
|
-
expect(WebMock::RequestPattern.new(:post, "www.example.com", :
|
387
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
386
|
+
expect(WebMock::RequestPattern.new(:post, "www.example.com", body: {a: /^\w{5}$/, b: {c: /^\d{3}$/}})).
|
387
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=abcde&b[c]=123'))
|
388
388
|
end
|
389
389
|
|
390
390
|
it "should not match when hash does not contains regex values" do
|
391
|
-
expect(WebMock::RequestPattern.new(:post, "www.example.com", :
|
392
|
-
not_to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
391
|
+
expect(WebMock::RequestPattern.new(:post, "www.example.com", body: {a: /^\d+$/, b: {c: /^\d{3}$/}})).
|
392
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=abcde&b[c]=123'))
|
393
393
|
end
|
394
394
|
|
395
395
|
context 'body is an hash with an array of hashes' do
|
396
|
-
let(:body_hash) { {:
|
396
|
+
let(:body_hash) { {a: [{'b' => '1'}, {'b' => '2'}]} }
|
397
397
|
|
398
398
|
it "should match when hash matches body" do
|
399
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
400
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
399
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
400
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a[][b]=1&a[][b]=2'))
|
401
401
|
end
|
402
402
|
end
|
403
403
|
|
404
404
|
context 'body is an hash with an array of hashes with multiple keys' do
|
405
|
-
let(:body_hash) { {:
|
405
|
+
let(:body_hash) { {a: [{'b' => '1', 'a' => '2'}, {'b' => '3'}]} }
|
406
406
|
|
407
407
|
it "should match when hash matches body" do
|
408
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
409
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
408
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
409
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a[][b]=1&a[][a]=2&a[][b]=3'))
|
410
410
|
end
|
411
411
|
end
|
412
412
|
end
|
413
413
|
|
414
414
|
describe "for request with json body and content type is set to json" do
|
415
415
|
it "should match when hash matches body" do
|
416
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
417
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
418
|
-
:
|
416
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
417
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/json'},
|
418
|
+
body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))
|
419
419
|
end
|
420
420
|
|
421
421
|
it "should match if hash matches body in different form" do
|
422
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
423
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
424
|
-
:
|
422
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
423
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/json'},
|
424
|
+
body: "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
|
425
425
|
end
|
426
426
|
|
427
427
|
it "should not match when body is not json" do
|
428
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
428
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
429
429
|
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
430
|
-
:
|
430
|
+
headers: {content_type: 'application/json'}, body: "foo bar"))
|
431
431
|
end
|
432
432
|
|
433
433
|
it "should not match if request body is different" do
|
434
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
434
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
|
435
435
|
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
436
|
-
:
|
436
|
+
headers: {content_type: 'application/json'}, body: "{\"a\":1,\"c\":null}"))
|
437
437
|
end
|
438
438
|
|
439
439
|
it "should not match if request body is has less params than pattern" do
|
440
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
440
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
|
441
441
|
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
442
|
-
:
|
442
|
+
headers: {content_type: 'application/json'}, body: "{\"a\":1}"))
|
443
443
|
end
|
444
444
|
|
445
445
|
it "should not match if request body is has more params than pattern" do
|
446
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
446
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1})).
|
447
447
|
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
448
|
-
:
|
448
|
+
headers: {content_type: 'application/json'}, body: "{\"a\":1,\"c\":1}"))
|
449
449
|
end
|
450
450
|
end
|
451
451
|
|
@@ -453,119 +453,119 @@ describe WebMock::RequestPattern do
|
|
453
453
|
let(:body_hash) { {"opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}}} }
|
454
454
|
|
455
455
|
it "should match when hash matches body" do
|
456
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
457
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
458
|
-
:
|
456
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
457
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/xml'},
|
458
|
+
body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
|
459
459
|
end
|
460
460
|
|
461
461
|
it "should match if hash matches body in different form" do
|
462
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
463
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
464
|
-
:
|
462
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
463
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/xml'},
|
464
|
+
body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
|
465
465
|
end
|
466
466
|
|
467
467
|
it "should not match when body is not xml" do
|
468
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
468
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
469
469
|
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
470
|
-
:
|
470
|
+
headers: {content_type: 'application/xml'}, body: "foo bar"))
|
471
471
|
end
|
472
472
|
|
473
473
|
it "matches when the content type include a charset" do
|
474
|
-
expect(WebMock::RequestPattern.new(:post, 'www.example.com', :
|
475
|
-
to match(WebMock::RequestSignature.new(:post, "www.example.com", :
|
476
|
-
:
|
474
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
475
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/xml;charset=UTF-8'},
|
476
|
+
body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
|
477
477
|
|
478
478
|
end
|
479
479
|
end
|
480
480
|
end
|
481
481
|
|
482
482
|
describe "when body in a pattern is declared as a partial hash matcher" do
|
483
|
-
let(:signature) { WebMock::RequestSignature.new(:post, "www.example.com", :
|
483
|
+
let(:signature) { WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=1&c[d][]=e&c[d][]=f&b=five') }
|
484
484
|
|
485
485
|
it "should match when query params are declared as HashIncluding matcher matching params" do
|
486
486
|
expect(WebMock::RequestPattern.new(:post, "www.example.com",
|
487
|
-
:
|
487
|
+
body: WebMock::Matchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}}))).
|
488
488
|
to match(signature)
|
489
489
|
end
|
490
490
|
|
491
491
|
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
492
492
|
expect(WebMock::RequestPattern.new(:post, "www.example.com",
|
493
|
-
:
|
493
|
+
body: WebMock::Matchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}}))).
|
494
494
|
not_to match(signature)
|
495
495
|
end
|
496
496
|
|
497
497
|
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
498
498
|
expect(WebMock::RequestPattern.new(:post, "www.example.com",
|
499
|
-
:
|
499
|
+
body: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}}))).
|
500
500
|
to match(signature)
|
501
501
|
end
|
502
502
|
|
503
503
|
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
504
504
|
expect(WebMock::RequestPattern.new(:post, "www.example.com",
|
505
|
-
:
|
505
|
+
body: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}}))).
|
506
506
|
not_to match(signature)
|
507
507
|
end
|
508
508
|
end
|
509
509
|
end
|
510
510
|
|
511
511
|
it "should match if pattern and request have the same headers" do
|
512
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
513
|
-
to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
512
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'})).
|
513
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))
|
514
514
|
end
|
515
515
|
|
516
516
|
it "should match if pattern headers values are regexps matching request header values" do
|
517
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
518
|
-
to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
517
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => %r{^image/jpeg$}})).
|
518
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))
|
519
519
|
end
|
520
520
|
|
521
521
|
it "should not match if pattern has different value of header than request" do
|
522
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
523
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
522
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/png'})).
|
523
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))
|
524
524
|
end
|
525
525
|
|
526
526
|
it "should not match if pattern header value regexp doesn't match request header value" do
|
527
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
528
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
527
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => %r{^image\/jpeg$}})).
|
528
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpegx'}))
|
529
529
|
end
|
530
530
|
|
531
531
|
it "should match if request has more headers than request pattern" do
|
532
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
533
|
-
to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
532
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'})).
|
533
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}))
|
534
534
|
end
|
535
535
|
|
536
536
|
it "should not match if request has less headers than the request pattern" do
|
537
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
538
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
537
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'})).
|
538
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))
|
539
539
|
end
|
540
540
|
|
541
541
|
it "should match even is header keys are declared in different form" do
|
542
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
543
|
-
to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
542
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'ContentLength' => '8888', 'Content-type' => 'image/png'})).
|
543
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {:ContentLength => 8888, 'content_type' => 'image/png'}))
|
544
544
|
end
|
545
545
|
|
546
546
|
it "should match is pattern doesn't have specified headers" do
|
547
547
|
expect(WebMock::RequestPattern.new(:get, "www.example.com")).
|
548
|
-
to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
548
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))
|
549
549
|
end
|
550
550
|
|
551
551
|
it "should not match if pattern has nil headers but request has headers" do
|
552
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
553
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
552
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: nil)).
|
553
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))
|
554
554
|
end
|
555
555
|
|
556
556
|
it "should not match if pattern has empty headers but request has headers" do
|
557
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
558
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
557
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {})).
|
558
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))
|
559
559
|
end
|
560
560
|
|
561
561
|
it "should not match if pattern has specified headers but request has nil headers" do
|
562
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
562
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'A'=>'a'})).
|
563
563
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
564
564
|
end
|
565
565
|
|
566
566
|
it "should not match if pattern has specified headers but request has empty headers" do
|
567
|
-
expect(WebMock::RequestPattern.new(:get, "www.example.com", :
|
568
|
-
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", :
|
567
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'A'=>'a'})).
|
568
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {}))
|
569
569
|
end
|
570
570
|
|
571
571
|
it "should match if block given in pattern evaluates request to true" do
|