webmock 1.8.6 → 3.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/CI.yml +37 -0
- data/.gitignore +6 -0
- data/CHANGELOG.md +1198 -0
- data/Gemfile +3 -15
- data/README.md +761 -305
- data/Rakefile +13 -40
- data/lib/webmock/api.rb +63 -17
- data/lib/webmock/callback_registry.rb +1 -1
- data/lib/webmock/config.rb +8 -0
- data/lib/webmock/cucumber.rb +2 -0
- data/lib/webmock/errors.rb +8 -24
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +216 -0
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +148 -84
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +224 -4
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +104 -34
- data/lib/webmock/http_lib_adapters/http_rb/client.rb +17 -0
- data/lib/webmock/http_lib_adapters/http_rb/request.rb +16 -0
- data/lib/webmock/http_lib_adapters/http_rb/response.rb +64 -0
- data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +29 -0
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +68 -0
- data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +37 -0
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +152 -86
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +145 -0
- data/lib/webmock/http_lib_adapters/net_http.rb +155 -46
- data/lib/webmock/http_lib_adapters/net_http_response.rb +1 -1
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +16 -15
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +76 -82
- data/lib/webmock/matchers/any_arg_matcher.rb +13 -0
- data/lib/webmock/matchers/hash_argument_matcher.rb +21 -0
- data/lib/webmock/matchers/hash_excluding_matcher.rb +15 -0
- data/lib/webmock/matchers/hash_including_matcher.rb +4 -12
- data/lib/webmock/minitest.rb +29 -3
- data/lib/webmock/rack_response.rb +14 -7
- data/lib/webmock/request_body_diff.rb +64 -0
- data/lib/webmock/request_execution_verifier.rb +38 -17
- data/lib/webmock/request_pattern.rb +158 -38
- data/lib/webmock/request_registry.rb +3 -3
- data/lib/webmock/request_signature.rb +7 -3
- data/lib/webmock/request_signature_snippet.rb +61 -0
- data/lib/webmock/request_stub.rb +9 -6
- data/lib/webmock/response.rb +30 -15
- data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +38 -2
- data/lib/webmock/rspec/matchers/webmock_matcher.rb +23 -2
- data/lib/webmock/rspec/matchers.rb +0 -1
- data/lib/webmock/rspec.rb +11 -2
- data/lib/webmock/stub_registry.rb +31 -10
- data/lib/webmock/stub_request_snippet.rb +14 -6
- data/lib/webmock/test_unit.rb +4 -4
- data/lib/webmock/util/hash_counter.rb +20 -6
- data/lib/webmock/util/hash_keys_stringifier.rb +5 -3
- data/lib/webmock/util/hash_validator.rb +17 -0
- data/lib/webmock/util/headers.rb +23 -2
- data/lib/webmock/util/json.rb +20 -7
- data/lib/webmock/util/query_mapper.rb +281 -0
- data/lib/webmock/util/uri.rb +29 -19
- data/lib/webmock/util/values_stringifier.rb +20 -0
- data/lib/webmock/util/version_checker.rb +40 -2
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +56 -17
- data/lib/webmock.rb +56 -46
- data/minitest/test_helper.rb +8 -3
- data/minitest/test_webmock.rb +4 -1
- data/minitest/webmock_spec.rb +16 -6
- data/spec/acceptance/async_http_client/async_http_client_spec.rb +375 -0
- data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
- data/spec/acceptance/curb/curb_spec.rb +227 -68
- data/spec/acceptance/curb/curb_spec_helper.rb +11 -8
- data/spec/acceptance/em_http_request/em_http_request_spec.rb +322 -28
- data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +15 -10
- data/spec/acceptance/excon/excon_spec.rb +66 -4
- data/spec/acceptance/excon/excon_spec_helper.rb +21 -7
- data/spec/acceptance/http_rb/http_rb_spec.rb +93 -0
- data/spec/acceptance/http_rb/http_rb_spec_helper.rb +54 -0
- data/spec/acceptance/httpclient/httpclient_spec.rb +152 -11
- data/spec/acceptance/httpclient/httpclient_spec_helper.rb +25 -16
- data/spec/acceptance/manticore/manticore_spec.rb +107 -0
- data/spec/acceptance/manticore/manticore_spec_helper.rb +35 -0
- data/spec/acceptance/net_http/net_http_shared.rb +52 -24
- data/spec/acceptance/net_http/net_http_spec.rb +164 -50
- data/spec/acceptance/net_http/net_http_spec_helper.rb +19 -10
- data/spec/acceptance/net_http/real_net_http_spec.rb +1 -1
- data/spec/acceptance/patron/patron_spec.rb +29 -40
- data/spec/acceptance/patron/patron_spec_helper.rb +15 -11
- data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +229 -58
- data/spec/acceptance/shared/callbacks.rb +32 -30
- data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +20 -5
- data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +14 -14
- data/spec/acceptance/shared/precedence_of_stubs.rb +6 -6
- data/spec/acceptance/shared/request_expectations.rb +560 -296
- data/spec/acceptance/shared/returning_declared_responses.rb +180 -138
- data/spec/acceptance/shared/stubbing_requests.rb +385 -154
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +78 -17
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +19 -15
- data/spec/acceptance/webmock_shared.rb +2 -2
- data/spec/fixtures/test.txt +1 -0
- data/spec/quality_spec.rb +27 -3
- data/spec/spec_helper.rb +11 -20
- data/spec/support/failures.rb +9 -0
- data/spec/support/my_rack_app.rb +8 -3
- data/spec/support/network_connection.rb +7 -13
- data/spec/support/webmock_server.rb +8 -3
- data/spec/unit/api_spec.rb +175 -0
- data/spec/unit/errors_spec.rb +116 -19
- data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +1 -1
- data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +2 -2
- data/spec/unit/matchers/hash_excluding_matcher_spec.rb +61 -0
- data/spec/unit/matchers/hash_including_matcher_spec.rb +87 -0
- data/spec/unit/rack_response_spec.rb +54 -16
- data/spec/unit/request_body_diff_spec.rb +90 -0
- data/spec/unit/request_execution_verifier_spec.rb +147 -39
- data/spec/unit/request_pattern_spec.rb +462 -198
- data/spec/unit/request_registry_spec.rb +29 -9
- data/spec/unit/request_signature_snippet_spec.rb +89 -0
- data/spec/unit/request_signature_spec.rb +91 -49
- data/spec/unit/request_stub_spec.rb +71 -70
- data/spec/unit/response_spec.rb +100 -81
- data/spec/unit/stub_registry_spec.rb +37 -20
- data/spec/unit/stub_request_snippet_spec.rb +51 -31
- data/spec/unit/util/hash_counter_spec.rb +6 -6
- data/spec/unit/util/hash_keys_stringifier_spec.rb +4 -4
- data/spec/unit/util/headers_spec.rb +4 -4
- data/spec/unit/util/json_spec.rb +29 -3
- data/spec/unit/util/query_mapper_spec.rb +157 -0
- data/spec/unit/util/uri_spec.rb +150 -36
- data/spec/unit/util/version_checker_spec.rb +15 -9
- data/spec/unit/webmock_spec.rb +57 -4
- data/test/http_request.rb +3 -3
- data/test/shared_test.rb +45 -13
- data/test/test_helper.rb +1 -1
- data/test/test_webmock.rb +6 -0
- data/webmock.gemspec +30 -11
- metadata +308 -199
- data/.rvmrc +0 -1
- data/.travis.yml +0 -11
- data/Guardfile +0 -24
- data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_0_x.rb +0 -151
- data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +0 -210
@@ -4,32 +4,37 @@ describe WebMock::RequestPattern do
|
|
4
4
|
|
5
5
|
describe "describing itself" do
|
6
6
|
it "should report string describing itself" do
|
7
|
-
WebMock::RequestPattern.new(:get, "www.example.com",
|
8
|
-
:
|
7
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
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
|
end
|
11
12
|
|
12
13
|
it "should report string describing itself with block" do
|
13
|
-
WebMock::RequestPattern.new(:get, "www.example.com",
|
14
|
-
:
|
14
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
15
|
+
body: "abc", headers: {'A' => 'a', 'B' => 'b'}).with {|req| true}.to_s).to eq(
|
15
16
|
"GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'} with given block"
|
17
|
+
)
|
16
18
|
end
|
17
19
|
|
18
20
|
it "should report string describing itself with query params" do
|
19
|
-
WebMock::RequestPattern.new(:get, /.*example.*/, :
|
21
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {'a' => ['b', 'c']}).to_s).to eq(
|
20
22
|
"GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"
|
23
|
+
)
|
21
24
|
end
|
22
25
|
|
23
26
|
it "should report string describing itself with query params as hash including matcher" do
|
24
|
-
WebMock::RequestPattern.new(:get, /.*example.*/,
|
25
|
-
:
|
27
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
28
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(
|
26
29
|
"GET /.*example.*/ with query params hash_including({\"a\"=>[\"b\", \"c\"]})"
|
30
|
+
)
|
27
31
|
end
|
28
32
|
|
29
33
|
it "should report string describing itself with body as hash including matcher" do
|
30
|
-
WebMock::RequestPattern.new(:get, /.*example.*/,
|
31
|
-
:
|
34
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
35
|
+
body: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(
|
32
36
|
"GET /.*example.*/ with body hash_including({\"a\"=>[\"b\", \"c\"]})"
|
37
|
+
)
|
33
38
|
end
|
34
39
|
end
|
35
40
|
|
@@ -39,15 +44,22 @@ describe WebMock::RequestPattern do
|
|
39
44
|
end
|
40
45
|
|
41
46
|
it "should have assigned body pattern" do
|
42
|
-
@request_pattern.with(:
|
43
|
-
@request_pattern.to_s.
|
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)
|
44
49
|
end
|
45
50
|
|
46
51
|
it "should have assigned normalized headers pattern" do
|
47
|
-
@request_pattern.with(:
|
48
|
-
@request_pattern.to_s.
|
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)
|
49
54
|
end
|
50
55
|
|
56
|
+
it "should raise an error if options passed to `with` are invalid" do
|
57
|
+
expect { @request_pattern.with(foo: "bar") }.to raise_error('Unknown key: "foo". Valid keys are: "body", "headers", "query", "basic_auth"')
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should raise an error if neither options or block is provided" do
|
61
|
+
expect { @request_pattern.with() }.to raise_error('#with method invoked with no arguments. Either options hash or block must be specified. Created a block with do..end? Try creating it with curly braces {} instead.')
|
62
|
+
end
|
51
63
|
end
|
52
64
|
|
53
65
|
|
@@ -60,80 +72,160 @@ describe WebMock::RequestPattern do
|
|
60
72
|
describe "when matching" do
|
61
73
|
|
62
74
|
it "should match if uri matches and method matches" do
|
63
|
-
WebMock::RequestPattern.new(:get, "www.example.com").
|
64
|
-
|
75
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com")).
|
76
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
65
77
|
end
|
66
78
|
|
67
79
|
it "should match if uri matches and method pattern is any" do
|
68
|
-
WebMock::RequestPattern.new(:any, "www.example.com").
|
69
|
-
|
80
|
+
expect(WebMock::RequestPattern.new(:any, "www.example.com")).
|
81
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
70
82
|
end
|
71
83
|
|
72
84
|
it "should not match if request has different method" do
|
73
|
-
WebMock::RequestPattern.new(:post, "www.example.com").
|
74
|
-
|
85
|
+
expect(WebMock::RequestPattern.new(:post, "www.example.com")).
|
86
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
75
87
|
end
|
76
88
|
|
77
89
|
it "should match if uri matches request uri" do
|
78
|
-
WebMock::RequestPattern.new(:get, "www.example.com").
|
79
|
-
|
90
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com")).
|
91
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
80
92
|
end
|
81
93
|
|
82
94
|
it "should match if request has unescaped uri" do
|
83
|
-
WebMock::RequestPattern.new(:get, "www.example.com/my%20path").
|
84
|
-
|
95
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com/my%20path")).
|
96
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/my path"))
|
85
97
|
end
|
86
98
|
|
87
99
|
it "should match if request has escaped uri" do
|
88
|
-
WebMock::RequestPattern.new(:get, "www.example.com/my path").
|
89
|
-
|
100
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com/my path")).
|
101
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
|
90
102
|
end
|
91
103
|
|
92
104
|
it "should match if uri regexp pattern matches unescaped form of request uri" do
|
93
|
-
WebMock::RequestPattern.new(:get, /.*my path.*/).
|
94
|
-
|
105
|
+
expect(WebMock::RequestPattern.new(:get, /.*my path.*/)).
|
106
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
|
95
107
|
end
|
96
108
|
|
97
109
|
it "should match if uri regexp pattern matches request uri" do
|
98
|
-
WebMock::RequestPattern.new(:get, /.*example.*/).
|
99
|
-
|
110
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/)).
|
111
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should match if uri matches requesst uri as URI object" do
|
115
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"))).
|
116
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should match if uri proc pattern returning true" do
|
120
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true })).
|
121
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should not match if uri proc pattern returns false" do
|
125
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { false })).
|
126
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should match if uri Addressable::Template pattern matches unescaped form of request uri" do
|
130
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{any_path}"))).
|
131
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should match if uri Addressable::Template pattern matches request uri" do
|
135
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"))).
|
136
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should match if uri Addressable::Template pattern matches request uri without TLD" do
|
140
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("localhost"))).
|
141
|
+
to match(WebMock::RequestSignature.new(:get, "localhost"))
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should match if Addressable::Template pattern that has ip address host matches request uri" do
|
145
|
+
signature = WebMock::RequestSignature.new(:get, "127.0.0.1:3000/1234")
|
146
|
+
uri = Addressable::Template.new("127.0.0.1:3000/{id}")
|
147
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should match if Addressable::Template pattern that has ip address host without port matches request uri" do
|
151
|
+
signature = WebMock::RequestSignature.new(:get, "127.0.0.1/1234")
|
152
|
+
uri = Addressable::Template.new("127.0.0.1/{id}")
|
153
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should match if Addressable::Template pattern host matches request uri" do
|
157
|
+
signature = WebMock::RequestSignature.new(:get, "www.example.com")
|
158
|
+
uri = Addressable::Template.new("{subdomain}.example.com")
|
159
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should not match if Addressable::Template pattern host does not match request uri" do
|
163
|
+
signature = WebMock::RequestSignature.new(:get, "www.bad-example.com")
|
164
|
+
uri = Addressable::Template.new("{subdomain}.example.com")
|
165
|
+
expect(WebMock::RequestPattern.new(:get, uri)).not_to match(signature)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should match if uri Addressable::Template pattern matches request uri without a schema and a path " do
|
169
|
+
signature = WebMock::RequestSignature.new(:get, "127.0.0.1:3000")
|
170
|
+
uri = Addressable::Template.new("127.0.0.1:3000")
|
171
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
100
172
|
end
|
101
173
|
|
102
174
|
it "should match for uris with same parameters as pattern" do
|
103
|
-
WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
|
104
|
-
|
175
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")).
|
176
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
|
105
177
|
end
|
106
178
|
|
107
179
|
it "should not match for uris with different parameters" do
|
108
|
-
WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
|
109
|
-
|
180
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")).
|
181
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a=2&b=1"))
|
110
182
|
end
|
111
183
|
|
112
184
|
it "should match for uri parameters in different order" do
|
113
|
-
WebMock::RequestPattern.new(:get, "www.example.com?b=2&a=1").
|
114
|
-
|
185
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com?b=2&a=1")).
|
186
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
|
115
187
|
end
|
116
188
|
|
117
189
|
describe "when parameters are escaped" do
|
118
190
|
|
119
191
|
it "should match if uri pattern has escaped parameters and request has unescaped parameters" do
|
120
|
-
WebMock::RequestPattern.new(:get, "www.example.com/?a=a%20b").
|
121
|
-
|
192
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com/?a=a%20b")).
|
193
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
|
122
194
|
end
|
123
195
|
|
124
196
|
it "should match if uri pattern has unescaped parameters and request has escaped parameters" do
|
125
|
-
WebMock::RequestPattern.new(:get, "www.example.com/?a=a b").
|
126
|
-
|
197
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com/?a=a b")).
|
198
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
|
127
199
|
end
|
128
200
|
|
129
201
|
it "should match if uri regexp pattern matches uri with unescaped parameters and request has escaped parameters" do
|
130
|
-
WebMock::RequestPattern.new(:get, /.*a=a b.*/).
|
131
|
-
|
202
|
+
expect(WebMock::RequestPattern.new(:get, /.*a=a b.*/)).
|
203
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
|
132
204
|
end
|
133
205
|
|
134
206
|
it "should match if uri regexp pattern matches uri with escaped parameters and request has unescaped parameters" do
|
135
|
-
WebMock::RequestPattern.new(:get, /.*a=a%20b.*/).
|
136
|
-
|
207
|
+
expect(WebMock::RequestPattern.new(:get, /.*a=a%20b.*/)).
|
208
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should match if uri Addressable::Template pattern matches uri without parameter value and request has escaped parameters" do
|
212
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{?a}"))).
|
213
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should match if uri Addressable::Template pattern matches uri without parameter value and request has unescaped parameters" do
|
217
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{?a}"))).
|
218
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should match if uri Addressable::Template pattern matches uri with unescaped parameter value and request has unescaped parameters" do
|
222
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/?a=a b"))).
|
223
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should match if uri Addressable::Template pattern matches uri with escaped parameter value and request has escaped parameters" do
|
227
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/?a=a%20b"))).
|
228
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
|
137
229
|
end
|
138
230
|
|
139
231
|
end
|
@@ -142,83 +234,206 @@ describe WebMock::RequestPattern do
|
|
142
234
|
|
143
235
|
describe "when uri is described as regexp" do
|
144
236
|
it "should match request query params" do
|
145
|
-
WebMock::RequestPattern.new(:get, /.*example.*/, :
|
146
|
-
|
237
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"a" => ["b", "c"]})).
|
238
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
147
239
|
end
|
148
240
|
|
149
|
-
it "should match request query params if params don't match" do
|
150
|
-
WebMock::RequestPattern.new(:get, /.*example.*/, :
|
151
|
-
|
241
|
+
it "should not match request query params if params don't match" do
|
242
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"x" => ["b", "c"]})).
|
243
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
152
244
|
end
|
153
245
|
|
154
246
|
it "should match when query params are declared as HashIncluding matcher matching params" do
|
155
|
-
WebMock::RequestPattern.new(:get, /.*example.*/,
|
156
|
-
:
|
157
|
-
|
247
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
248
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
249
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
158
250
|
end
|
159
251
|
|
160
252
|
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
161
|
-
WebMock::RequestPattern.new(:get, /.*example.*/,
|
162
|
-
:
|
163
|
-
|
253
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
254
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
255
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
164
256
|
end
|
165
257
|
|
166
258
|
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
167
|
-
WebMock::RequestPattern.new(:get, /.*example.*/,
|
168
|
-
:
|
169
|
-
|
259
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
260
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
261
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
170
262
|
end
|
171
263
|
|
172
264
|
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
173
|
-
WebMock::RequestPattern.new(:get, /.*example.*/,
|
174
|
-
:
|
175
|
-
|
265
|
+
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
|
266
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
267
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
describe "when uri is described as URI" do
|
272
|
+
it "should match request query params" do
|
273
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"), query: {"a" => ["b", "c"]})).
|
274
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should not match request query params if params don't match" do
|
278
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"), query: {"x" => ["b", "c"]})).
|
279
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should match when query params are declared as HashIncluding matcher matching params" do
|
283
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
284
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
285
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
286
|
+
end
|
287
|
+
|
288
|
+
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
289
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
290
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
291
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
292
|
+
end
|
293
|
+
|
294
|
+
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
295
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
296
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
297
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
301
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
302
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
303
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
describe "when uri is described as a proc" do
|
308
|
+
it "should match request query params" do
|
309
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"a" => ["b", "c"]})).
|
310
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
311
|
+
end
|
312
|
+
|
313
|
+
it "should not match request query params if params don't match" do
|
314
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"x" => ["b", "c"]})).
|
315
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should match when query params are declared as HashIncluding matcher matching params" do
|
319
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
320
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
321
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
325
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
326
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
327
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
328
|
+
end
|
329
|
+
|
330
|
+
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
331
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
332
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
333
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
334
|
+
end
|
335
|
+
|
336
|
+
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
337
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
338
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
339
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
describe "when uri is described as Addressable::Template" do
|
344
|
+
it "should raise error if query params are specified" do
|
345
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"a" => ["b", "c"]})).
|
346
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
347
|
+
end
|
348
|
+
|
349
|
+
it "should not match request query params if params don't match" do
|
350
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"x" => ["b", "c"]})).
|
351
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
352
|
+
end
|
353
|
+
|
354
|
+
it "should match when query params are declared as HashIncluding matcher matching params" do
|
355
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
|
356
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
357
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
358
|
+
end
|
359
|
+
|
360
|
+
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
361
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
|
362
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
363
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
364
|
+
end
|
365
|
+
|
366
|
+
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
367
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
|
368
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
369
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
370
|
+
end
|
371
|
+
|
372
|
+
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
373
|
+
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
|
374
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
375
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
176
376
|
end
|
177
377
|
end
|
178
378
|
|
179
379
|
describe "when uri is described as string" do
|
180
380
|
it "should match when query params are the same as declared in hash" do
|
181
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
182
|
-
|
381
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", query: {"a" => ["b", "c"]})).
|
382
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
183
383
|
end
|
184
384
|
|
185
385
|
it "should not match when query params are different than the declared in hash" do
|
186
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
187
|
-
|
386
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", query: {"a" => ["b", "c"]})).
|
387
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?x[]=b&a[]=c"))
|
188
388
|
end
|
189
389
|
|
190
390
|
it "should match when query params are the same as declared as string" do
|
191
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
192
|
-
|
391
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", query: "a[]=b&a[]=c")).
|
392
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
193
393
|
end
|
194
394
|
|
195
395
|
it "should match when query params are the same as declared both in query option or url" do
|
196
|
-
WebMock::RequestPattern.new(:get, "www.example.com/?x=3", :
|
197
|
-
|
396
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com/?x=3", query: "a[]=b&a[]=c")).
|
397
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com/?x=3&a[]=b&a[]=c"))
|
198
398
|
end
|
199
399
|
|
200
400
|
it "should match when query params are declared as HashIncluding matcher matching params" do
|
201
|
-
WebMock::RequestPattern.new(:get, "www.example.com",
|
202
|
-
:
|
203
|
-
|
401
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
402
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
403
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
204
404
|
end
|
205
405
|
|
206
406
|
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
207
|
-
WebMock::RequestPattern.new(:get, "www.example.com",
|
208
|
-
:
|
209
|
-
|
407
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
408
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
409
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
210
410
|
end
|
211
411
|
|
212
412
|
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
213
|
-
WebMock::RequestPattern.new(:get, "www.example.com",
|
214
|
-
:
|
215
|
-
|
413
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
414
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
415
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
216
416
|
end
|
217
417
|
|
218
418
|
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
219
|
-
WebMock::RequestPattern.new(:get, "www.example.com",
|
220
|
-
:
|
221
|
-
|
419
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com",
|
420
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
421
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
422
|
+
end
|
423
|
+
|
424
|
+
context "when using query values notation as flat array" do
|
425
|
+
before :all do
|
426
|
+
WebMock::Config.instance.query_values_notation = :flat_array
|
427
|
+
end
|
428
|
+
|
429
|
+
it "should not match when repeated query params are not the same as declared as string" do
|
430
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", query: "a=b&a=c")).
|
431
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a=b&a=c"))
|
432
|
+
end
|
433
|
+
|
434
|
+
after :all do
|
435
|
+
WebMock::Config.instance.query_values_notation = nil
|
436
|
+
end
|
222
437
|
end
|
223
438
|
end
|
224
439
|
end
|
@@ -226,43 +441,43 @@ describe WebMock::RequestPattern do
|
|
226
441
|
describe "when matching requests with body" do
|
227
442
|
|
228
443
|
it "should match if request body and body pattern are the same" do
|
229
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
230
|
-
|
444
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "abc")).
|
445
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
231
446
|
end
|
232
447
|
|
233
448
|
it "should match if request body matches regexp" do
|
234
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
235
|
-
|
449
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: /^abc$/)).
|
450
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
236
451
|
end
|
237
452
|
|
238
453
|
it "should not match if body pattern is different than request body" do
|
239
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
240
|
-
|
454
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "def")).
|
455
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
241
456
|
end
|
242
457
|
|
243
458
|
it "should not match if request body doesn't match regexp pattern" do
|
244
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
245
|
-
|
459
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: /^abc$/)).
|
460
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "xabc"))
|
246
461
|
end
|
247
462
|
|
248
463
|
it "should match if pattern doesn't have specified body" do
|
249
|
-
WebMock::RequestPattern.new(:get, "www.example.com").
|
250
|
-
|
464
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com")).
|
465
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
251
466
|
end
|
252
467
|
|
253
468
|
it "should not match if pattern has body specified as nil but request body is not empty" do
|
254
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
255
|
-
|
469
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: nil)).
|
470
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
256
471
|
end
|
257
472
|
|
258
473
|
it "should not match if pattern has empty body but request body is not empty" do
|
259
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
260
|
-
|
474
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "")).
|
475
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
|
261
476
|
end
|
262
477
|
|
263
478
|
it "should not match if pattern has body specified but request has no body" do
|
264
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
265
|
-
|
479
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "abc")).
|
480
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
266
481
|
end
|
267
482
|
|
268
483
|
describe "when body in pattern is declared as a hash" do
|
@@ -270,200 +485,249 @@ describe WebMock::RequestPattern do
|
|
270
485
|
|
271
486
|
describe "for request with url encoded body" do
|
272
487
|
it "should match when hash matches body" do
|
273
|
-
WebMock::RequestPattern.new(:post, 'www.example.com', :
|
274
|
-
|
488
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
489
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=1&c[d][]=e&c[d][]=f&b=five'))
|
275
490
|
end
|
276
491
|
|
277
492
|
it "should match when hash matches body in different order of params" do
|
278
|
-
WebMock::RequestPattern.new(:post, 'www.example.com', :
|
279
|
-
|
493
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
494
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=1&c[d][]=e&b=five&c[d][]=f'))
|
280
495
|
end
|
281
496
|
|
282
497
|
it "should not match when hash doesn't match url encoded body" do
|
283
|
-
WebMock::RequestPattern.new(:post, 'www.example.com', :
|
284
|
-
|
498
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
499
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'c[d][]=f&a=1&c[d][]=e'))
|
285
500
|
end
|
286
501
|
|
287
502
|
it "should not match when body is not url encoded" do
|
288
|
-
WebMock::RequestPattern.new(:post, 'www.example.com', :
|
289
|
-
|
503
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
504
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'foo bar'))
|
290
505
|
end
|
291
506
|
|
292
507
|
it "should match when hash contains regex values" do
|
293
|
-
WebMock::RequestPattern.new(:post, "www.example.com", :
|
294
|
-
|
508
|
+
expect(WebMock::RequestPattern.new(:post, "www.example.com", body: {a: /^\w{5}$/, b: {c: /^\d{3}$/}})).
|
509
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=abcde&b[c]=123'))
|
295
510
|
end
|
296
511
|
|
297
512
|
it "should not match when hash does not contains regex values" do
|
298
|
-
WebMock::RequestPattern.new(:post, "www.example.com", :
|
299
|
-
|
513
|
+
expect(WebMock::RequestPattern.new(:post, "www.example.com", body: {a: /^\d+$/, b: {c: /^\d{3}$/}})).
|
514
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=abcde&b[c]=123'))
|
300
515
|
end
|
301
|
-
end
|
302
516
|
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
it "should match if hash matches body in different form" do
|
311
|
-
WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
|
312
|
-
should match(WebMock::RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/json'},
|
313
|
-
:body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
|
314
|
-
end
|
315
|
-
|
316
|
-
it "should not match when body is not json" do
|
317
|
-
WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
|
318
|
-
should_not match(WebMock::RequestSignature.new(:post, "www.example.com",
|
319
|
-
:headers => {:content_type => 'application/json'}, :body => "foo bar"))
|
517
|
+
context 'body is an hash with an array of hashes' do
|
518
|
+
let(:body_hash) { {a: [{'b' => '1'}, {'b' => '2'}]} }
|
519
|
+
|
520
|
+
it "should match when hash matches body" do
|
521
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
522
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a[][b]=1&a[][b]=2'))
|
523
|
+
end
|
320
524
|
end
|
321
525
|
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
526
|
+
context 'body is an hash with an array of hashes with multiple keys' do
|
527
|
+
let(:body_hash) { {a: [{'b' => '1', 'a' => '2'}, {'b' => '3'}]} }
|
528
|
+
|
529
|
+
it "should match when hash matches body" do
|
530
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
531
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a[][b]=1&a[][a]=2&a[][b]=3'))
|
532
|
+
end
|
533
|
+
end
|
534
|
+
end
|
535
|
+
|
536
|
+
describe "for request with json body and content type is set to json" do
|
537
|
+
shared_examples "a json body" do
|
538
|
+
it "should match when hash matches body" do
|
539
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
540
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: content_type},
|
541
|
+
body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))
|
542
|
+
end
|
543
|
+
|
544
|
+
it "should match if hash matches body in different form" do
|
545
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
546
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: content_type},
|
547
|
+
body: "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
|
548
|
+
end
|
549
|
+
|
550
|
+
it "should not match when body is not json" do
|
551
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
552
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
553
|
+
headers: {content_type: content_type}, body: "foo bar"))
|
554
|
+
end
|
555
|
+
|
556
|
+
it "should not match if request body is different" do
|
557
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
|
558
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
559
|
+
headers: {content_type: content_type}, body: "{\"a\":1,\"c\":null}"))
|
560
|
+
end
|
561
|
+
|
562
|
+
it "should not match if request body is has less params than pattern" do
|
563
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
|
564
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
565
|
+
headers: {content_type: content_type}, body: "{\"a\":1}"))
|
566
|
+
end
|
567
|
+
|
568
|
+
it "should not match if request body is has more params than pattern" do
|
569
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1})).
|
570
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
571
|
+
headers: {content_type: content_type}, body: "{\"a\":1,\"c\":1}"))
|
572
|
+
end
|
326
573
|
end
|
327
574
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
:headers => {:content_type => 'application/json'}, :body => "{\"a\":1}"))
|
575
|
+
context "standard application/json" do
|
576
|
+
let(:content_type) { 'application/json' }
|
577
|
+
it_behaves_like "a json body"
|
332
578
|
end
|
333
579
|
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
:headers => {:content_type => 'application/json'}, :body => "{\"a\":1,\"c\":1}"))
|
580
|
+
context "custom json content type" do
|
581
|
+
let(:content_type) { 'application/vnd.api+json' }
|
582
|
+
it_behaves_like "a json body"
|
338
583
|
end
|
339
584
|
end
|
340
585
|
|
341
586
|
describe "for request with xml body and content type is set to xml" do
|
342
587
|
let(:body_hash) { {"opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}}} }
|
343
588
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
589
|
+
shared_examples "a xml body" do
|
590
|
+
it "should match when hash matches body" do
|
591
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
592
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: content_type},
|
593
|
+
body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
|
594
|
+
end
|
595
|
+
|
596
|
+
it "should match if hash matches body in different form" do
|
597
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
598
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: content_type},
|
599
|
+
body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
|
600
|
+
end
|
601
|
+
|
602
|
+
it "should not match when body is not xml" do
|
603
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
604
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
605
|
+
headers: {content_type: content_type}, body: "foo bar"))
|
606
|
+
end
|
607
|
+
|
608
|
+
it "matches when the content type include a charset" do
|
609
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
610
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: "#{content_type};charset=UTF-8"},
|
611
|
+
body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
|
612
|
+
|
613
|
+
end
|
614
|
+
end
|
615
|
+
|
616
|
+
context "standard application/xml" do
|
617
|
+
let(:content_type) { 'application/xml' }
|
618
|
+
it_behaves_like "a xml body"
|
619
|
+
end
|
620
|
+
|
621
|
+
context "custom xml content type" do
|
622
|
+
let(:content_type) { 'application/atom+xml' }
|
623
|
+
it_behaves_like "a xml body"
|
624
|
+
end
|
361
625
|
end
|
362
626
|
end
|
363
627
|
|
364
628
|
describe "when body in a pattern is declared as a partial hash matcher" do
|
365
|
-
let(:signature) { WebMock::RequestSignature.new(:post, "www.example.com", :
|
629
|
+
let(:signature) { WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=1&c[d][]=e&c[d][]=f&b=five') }
|
366
630
|
|
367
631
|
it "should match when query params are declared as HashIncluding matcher matching params" do
|
368
|
-
WebMock::RequestPattern.new(:post, "www.example.com",
|
369
|
-
:
|
370
|
-
|
632
|
+
expect(WebMock::RequestPattern.new(:post, "www.example.com",
|
633
|
+
body: WebMock::Matchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}}))).
|
634
|
+
to match(signature)
|
371
635
|
end
|
372
636
|
|
373
637
|
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
374
|
-
WebMock::RequestPattern.new(:post, "www.example.com",
|
375
|
-
:
|
376
|
-
|
638
|
+
expect(WebMock::RequestPattern.new(:post, "www.example.com",
|
639
|
+
body: WebMock::Matchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}}))).
|
640
|
+
not_to match(signature)
|
377
641
|
end
|
378
642
|
|
379
643
|
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
380
|
-
WebMock::RequestPattern.new(:post, "www.example.com",
|
381
|
-
:
|
382
|
-
|
644
|
+
expect(WebMock::RequestPattern.new(:post, "www.example.com",
|
645
|
+
body: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}}))).
|
646
|
+
to match(signature)
|
383
647
|
end
|
384
648
|
|
385
649
|
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
386
|
-
WebMock::RequestPattern.new(:post, "www.example.com",
|
387
|
-
:
|
388
|
-
|
650
|
+
expect(WebMock::RequestPattern.new(:post, "www.example.com",
|
651
|
+
body: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}}))).
|
652
|
+
not_to match(signature)
|
389
653
|
end
|
390
654
|
end
|
391
655
|
end
|
392
656
|
|
393
657
|
it "should match if pattern and request have the same headers" do
|
394
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
395
|
-
|
658
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'})).
|
659
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))
|
396
660
|
end
|
397
661
|
|
398
662
|
it "should match if pattern headers values are regexps matching request header values" do
|
399
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
400
|
-
|
663
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => %r{^image/jpeg$}})).
|
664
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))
|
401
665
|
end
|
402
666
|
|
403
667
|
it "should not match if pattern has different value of header than request" do
|
404
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
405
|
-
|
668
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/png'})).
|
669
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))
|
406
670
|
end
|
407
671
|
|
408
672
|
it "should not match if pattern header value regexp doesn't match request header value" do
|
409
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
410
|
-
|
673
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => %r{^image\/jpeg$}})).
|
674
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpegx'}))
|
411
675
|
end
|
412
676
|
|
413
677
|
it "should match if request has more headers than request pattern" do
|
414
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
415
|
-
|
678
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'})).
|
679
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}))
|
416
680
|
end
|
417
681
|
|
418
682
|
it "should not match if request has less headers than the request pattern" do
|
419
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
420
|
-
|
683
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'})).
|
684
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))
|
421
685
|
end
|
422
686
|
|
423
687
|
it "should match even is header keys are declared in different form" do
|
424
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
425
|
-
|
688
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'ContentLength' => '8888', 'Content-type' => 'image/png'})).
|
689
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {:ContentLength => 8888, 'content_type' => 'image/png'}))
|
426
690
|
end
|
427
691
|
|
428
692
|
it "should match is pattern doesn't have specified headers" do
|
429
|
-
WebMock::RequestPattern.new(:get, "www.example.com").
|
430
|
-
|
693
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com")).
|
694
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))
|
431
695
|
end
|
432
696
|
|
433
697
|
it "should not match if pattern has nil headers but request has headers" do
|
434
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
435
|
-
|
698
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: nil)).
|
699
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))
|
436
700
|
end
|
437
701
|
|
438
702
|
it "should not match if pattern has empty headers but request has headers" do
|
439
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
440
|
-
|
703
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {})).
|
704
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))
|
441
705
|
end
|
442
706
|
|
443
707
|
it "should not match if pattern has specified headers but request has nil headers" do
|
444
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
445
|
-
|
708
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'A'=>'a'})).
|
709
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
446
710
|
end
|
447
711
|
|
448
712
|
it "should not match if pattern has specified headers but request has empty headers" do
|
449
|
-
WebMock::RequestPattern.new(:get, "www.example.com", :
|
450
|
-
|
713
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'A'=>'a'})).
|
714
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {}))
|
451
715
|
end
|
452
716
|
|
453
717
|
it "should match if block given in pattern evaluates request to true" do
|
454
|
-
WebMock::RequestPattern.new(:get, "www.example.com").with { |request| true }.
|
455
|
-
|
718
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com").with { |request| true }).
|
719
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
456
720
|
end
|
457
721
|
|
458
722
|
it "should not match if block given in pattrn evaluates request to false" do
|
459
|
-
WebMock::RequestPattern.new(:get, "www.example.com").with { |request| false }.
|
460
|
-
|
723
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com").with { |request| false }).
|
724
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
461
725
|
end
|
462
726
|
|
463
727
|
it "should yield block with request signature" do
|
464
728
|
signature = WebMock::RequestSignature.new(:get, "www.example.com")
|
465
|
-
WebMock::RequestPattern.new(:get, "www.example.com").with { |request| request == signature }.
|
466
|
-
|
729
|
+
expect(WebMock::RequestPattern.new(:get, "www.example.com").with { |request| request == signature }).
|
730
|
+
to match(signature)
|
467
731
|
end
|
468
732
|
|
469
733
|
end
|