webmock 3.0.1 → 3.20.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.
Files changed (138) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +558 -2
  3. data/README.md +200 -42
  4. data/lib/webmock/api.rb +14 -0
  5. data/lib/webmock/assertion_failure.rb +2 -0
  6. data/lib/webmock/callback_registry.rb +2 -0
  7. data/lib/webmock/config.rb +2 -0
  8. data/lib/webmock/cucumber.rb +2 -0
  9. data/lib/webmock/deprecation.rb +2 -0
  10. data/lib/webmock/errors.rb +2 -0
  11. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +223 -0
  12. data/lib/webmock/http_lib_adapters/curb_adapter.rb +21 -5
  13. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +20 -9
  14. data/lib/webmock/http_lib_adapters/excon_adapter.rb +7 -2
  15. data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +2 -0
  16. data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +2 -0
  17. data/lib/webmock/http_lib_adapters/http_rb/client.rb +4 -1
  18. data/lib/webmock/http_lib_adapters/http_rb/request.rb +19 -1
  19. data/lib/webmock/http_lib_adapters/http_rb/response.rb +29 -3
  20. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +11 -3
  21. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +9 -3
  22. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +2 -0
  23. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +30 -9
  24. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +35 -15
  25. data/lib/webmock/http_lib_adapters/net_http.rb +38 -89
  26. data/lib/webmock/http_lib_adapters/net_http_response.rb +3 -1
  27. data/lib/webmock/http_lib_adapters/patron_adapter.rb +6 -4
  28. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +18 -2
  29. data/lib/webmock/matchers/any_arg_matcher.rb +15 -0
  30. data/lib/webmock/matchers/hash_argument_matcher.rb +23 -0
  31. data/lib/webmock/matchers/hash_excluding_matcher.rb +17 -0
  32. data/lib/webmock/matchers/hash_including_matcher.rb +6 -23
  33. data/lib/webmock/minitest.rb +2 -0
  34. data/lib/webmock/rack_response.rb +3 -1
  35. data/lib/webmock/request_body_diff.rb +3 -1
  36. data/lib/webmock/request_execution_verifier.rb +4 -3
  37. data/lib/webmock/request_pattern.rb +132 -52
  38. data/lib/webmock/request_registry.rb +3 -1
  39. data/lib/webmock/request_signature.rb +5 -3
  40. data/lib/webmock/request_signature_snippet.rb +6 -4
  41. data/lib/webmock/request_stub.rb +34 -0
  42. data/lib/webmock/response.rb +22 -14
  43. data/lib/webmock/responses_sequence.rb +2 -0
  44. data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +2 -0
  45. data/lib/webmock/rspec/matchers/webmock_matcher.rb +2 -0
  46. data/lib/webmock/rspec/matchers.rb +2 -0
  47. data/lib/webmock/rspec.rb +12 -3
  48. data/lib/webmock/stub_registry.rb +28 -11
  49. data/lib/webmock/stub_request_snippet.rb +12 -6
  50. data/lib/webmock/test_unit.rb +3 -3
  51. data/lib/webmock/util/hash_counter.rb +15 -9
  52. data/lib/webmock/util/hash_keys_stringifier.rb +2 -0
  53. data/lib/webmock/util/hash_validator.rb +2 -0
  54. data/lib/webmock/util/headers.rb +39 -11
  55. data/lib/webmock/util/json.rb +3 -2
  56. data/lib/webmock/util/query_mapper.rb +11 -7
  57. data/lib/webmock/util/uri.rb +13 -11
  58. data/lib/webmock/util/values_stringifier.rb +22 -0
  59. data/lib/webmock/util/version_checker.rb +7 -5
  60. data/lib/webmock/version.rb +3 -1
  61. data/lib/webmock/webmock.rb +22 -3
  62. data/lib/webmock.rb +55 -48
  63. metadata +106 -175
  64. data/.gemtest +0 -0
  65. data/.gitignore +0 -34
  66. data/.rspec-tm +0 -2
  67. data/.travis.yml +0 -20
  68. data/Gemfile +0 -9
  69. data/Rakefile +0 -30
  70. data/minitest/test_helper.rb +0 -34
  71. data/minitest/test_webmock.rb +0 -9
  72. data/minitest/webmock_spec.rb +0 -60
  73. data/spec/acceptance/curb/curb_spec.rb +0 -466
  74. data/spec/acceptance/curb/curb_spec_helper.rb +0 -147
  75. data/spec/acceptance/em_http_request/em_http_request_spec.rb +0 -406
  76. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +0 -77
  77. data/spec/acceptance/excon/excon_spec.rb +0 -75
  78. data/spec/acceptance/excon/excon_spec_helper.rb +0 -50
  79. data/spec/acceptance/http_rb/http_rb_spec.rb +0 -73
  80. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +0 -51
  81. data/spec/acceptance/httpclient/httpclient_spec.rb +0 -210
  82. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +0 -57
  83. data/spec/acceptance/manticore/manticore_spec.rb +0 -56
  84. data/spec/acceptance/manticore/manticore_spec_helper.rb +0 -35
  85. data/spec/acceptance/net_http/net_http_shared.rb +0 -153
  86. data/spec/acceptance/net_http/net_http_spec.rb +0 -317
  87. data/spec/acceptance/net_http/net_http_spec_helper.rb +0 -64
  88. data/spec/acceptance/net_http/real_net_http_spec.rb +0 -20
  89. data/spec/acceptance/patron/patron_spec.rb +0 -118
  90. data/spec/acceptance/patron/patron_spec_helper.rb +0 -54
  91. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +0 -313
  92. data/spec/acceptance/shared/callbacks.rb +0 -147
  93. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +0 -36
  94. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +0 -95
  95. data/spec/acceptance/shared/precedence_of_stubs.rb +0 -15
  96. data/spec/acceptance/shared/request_expectations.rb +0 -916
  97. data/spec/acceptance/shared/returning_declared_responses.rb +0 -388
  98. data/spec/acceptance/shared/stubbing_requests.rb +0 -583
  99. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +0 -135
  100. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +0 -60
  101. data/spec/acceptance/webmock_shared.rb +0 -41
  102. data/spec/fixtures/test.txt +0 -1
  103. data/spec/quality_spec.rb +0 -84
  104. data/spec/spec_helper.rb +0 -48
  105. data/spec/support/example_curl_output.txt +0 -22
  106. data/spec/support/failures.rb +0 -9
  107. data/spec/support/my_rack_app.rb +0 -53
  108. data/spec/support/network_connection.rb +0 -19
  109. data/spec/support/webmock_server.rb +0 -69
  110. data/spec/unit/api_spec.rb +0 -75
  111. data/spec/unit/errors_spec.rb +0 -129
  112. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +0 -17
  113. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +0 -12
  114. data/spec/unit/matchers/hash_including_matcher_spec.rb +0 -87
  115. data/spec/unit/rack_response_spec.rb +0 -112
  116. data/spec/unit/request_body_diff_spec.rb +0 -90
  117. data/spec/unit/request_execution_verifier_spec.rb +0 -208
  118. data/spec/unit/request_pattern_spec.rb +0 -590
  119. data/spec/unit/request_registry_spec.rb +0 -95
  120. data/spec/unit/request_signature_snippet_spec.rb +0 -89
  121. data/spec/unit/request_signature_spec.rb +0 -155
  122. data/spec/unit/request_stub_spec.rb +0 -199
  123. data/spec/unit/response_spec.rb +0 -282
  124. data/spec/unit/stub_registry_spec.rb +0 -103
  125. data/spec/unit/stub_request_snippet_spec.rb +0 -95
  126. data/spec/unit/util/hash_counter_spec.rb +0 -39
  127. data/spec/unit/util/hash_keys_stringifier_spec.rb +0 -27
  128. data/spec/unit/util/headers_spec.rb +0 -28
  129. data/spec/unit/util/json_spec.rb +0 -33
  130. data/spec/unit/util/query_mapper_spec.rb +0 -144
  131. data/spec/unit/util/uri_spec.rb +0 -299
  132. data/spec/unit/util/version_checker_spec.rb +0 -65
  133. data/spec/unit/webmock_spec.rb +0 -11
  134. data/test/http_request.rb +0 -24
  135. data/test/shared_test.rb +0 -95
  136. data/test/test_helper.rb +0 -23
  137. data/test/test_webmock.rb +0 -6
  138. data/webmock.gemspec +0 -46
@@ -1,590 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe WebMock::RequestPattern do
4
-
5
- describe "describing itself" do
6
- it "should report string describing itself" do
7
- expect(WebMock::RequestPattern.new(:get, "www.example.com",
8
- body: "abc", headers: {'A' => 'a', 'B' => 'b'}).to_s).to eq(
9
- "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'}"
10
- )
11
- end
12
-
13
- it "should report string describing itself with block" do
14
- expect(WebMock::RequestPattern.new(:get, "www.example.com",
15
- body: "abc", headers: {'A' => 'a', 'B' => 'b'}).with {|req| true}.to_s).to eq(
16
- "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'} with given block"
17
- )
18
- end
19
-
20
- it "should report string describing itself with query params" do
21
- expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {'a' => ['b', 'c']}).to_s).to eq(
22
- "GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"
23
- )
24
- end
25
-
26
- it "should report string describing itself with query params as hash including matcher" do
27
- expect(WebMock::RequestPattern.new(:get, /.*example.*/,
28
- query: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(
29
- "GET /.*example.*/ with query params hash_including({\"a\"=>[\"b\", \"c\"]})"
30
- )
31
- end
32
-
33
- it "should report string describing itself with body as hash including matcher" do
34
- expect(WebMock::RequestPattern.new(:get, /.*example.*/,
35
- body: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(
36
- "GET /.*example.*/ with body hash_including({\"a\"=>[\"b\", \"c\"]})"
37
- )
38
- end
39
- end
40
-
41
- describe "with" do
42
- before(:each) do
43
- @request_pattern =WebMock::RequestPattern.new(:get, "www.example.com")
44
- end
45
-
46
- it "should have assigned body pattern" do
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
- end
50
-
51
- it "should have assigned normalized headers pattern" do
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
- end
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.')
62
- end
63
- end
64
-
65
-
66
- class WebMock::RequestPattern
67
- def match(request_signature)
68
- self.matches?(request_signature)
69
- end
70
- end
71
-
72
- describe "when matching" do
73
-
74
- it "should match if uri matches and method matches" do
75
- expect(WebMock::RequestPattern.new(:get, "www.example.com")).
76
- to match(WebMock::RequestSignature.new(:get, "www.example.com"))
77
- end
78
-
79
- it "should match if uri matches and method pattern is any" do
80
- expect(WebMock::RequestPattern.new(:any, "www.example.com")).
81
- to match(WebMock::RequestSignature.new(:get, "www.example.com"))
82
- end
83
-
84
- it "should not match if request has different method" do
85
- expect(WebMock::RequestPattern.new(:post, "www.example.com")).
86
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
87
- end
88
-
89
- it "should match if uri matches request uri" do
90
- expect(WebMock::RequestPattern.new(:get, "www.example.com")).
91
- to match(WebMock::RequestSignature.new(:get, "www.example.com"))
92
- end
93
-
94
- it "should match if request has unescaped uri" do
95
- expect(WebMock::RequestPattern.new(:get, "www.example.com/my%20path")).
96
- to match(WebMock::RequestSignature.new(:get, "www.example.com/my path"))
97
- end
98
-
99
- it "should match if request has escaped uri" do
100
- expect(WebMock::RequestPattern.new(:get, "www.example.com/my path")).
101
- to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
102
- end
103
-
104
- it "should match if uri regexp pattern matches unescaped form of request uri" do
105
- expect(WebMock::RequestPattern.new(:get, /.*my path.*/)).
106
- to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
107
- end
108
-
109
- it "should match if uri regexp pattern matches request uri" do
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 Addressable::Template pattern matches unescaped form of request uri" do
115
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{any_path}"))).
116
- to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
117
- end
118
-
119
- it "should match if uri Addressable::Template pattern matches request uri" do
120
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"))).
121
- to match(WebMock::RequestSignature.new(:get, "www.example.com"))
122
- end
123
-
124
- it "should match for uris with same parameters as pattern" do
125
- expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")).
126
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
127
- end
128
-
129
- it "should not match for uris with different parameters" do
130
- expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")).
131
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a=2&b=1"))
132
- end
133
-
134
- it "should match for uri parameters in different order" do
135
- expect(WebMock::RequestPattern.new(:get, "www.example.com?b=2&a=1")).
136
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
137
- end
138
-
139
- describe "when parameters are escaped" do
140
-
141
- it "should match if uri pattern has escaped parameters and request has unescaped parameters" do
142
- expect(WebMock::RequestPattern.new(:get, "www.example.com/?a=a%20b")).
143
- to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
144
- end
145
-
146
- it "should match if uri pattern has unescaped parameters and request has escaped parameters" do
147
- expect(WebMock::RequestPattern.new(:get, "www.example.com/?a=a b")).
148
- to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
149
- end
150
-
151
- it "should match if uri regexp pattern matches uri with unescaped parameters and request has escaped parameters" do
152
- expect(WebMock::RequestPattern.new(:get, /.*a=a b.*/)).
153
- to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
154
- end
155
-
156
- it "should match if uri regexp pattern matches uri with escaped parameters and request has unescaped parameters" do
157
- expect(WebMock::RequestPattern.new(:get, /.*a=a%20b.*/)).
158
- to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
159
- end
160
-
161
- it "should match if uri Addressable::Template pattern matches uri without parameter value and request has escaped parameters" do
162
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{?a}"))).
163
- to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
164
- end
165
-
166
- it "should match if uri Addressable::Template pattern matches uri without parameter value and request has unescaped parameters" do
167
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{?a}"))).
168
- to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
169
- end
170
-
171
- it "should match if uri Addressable::Template pattern matches uri with unescaped parameter value and request has unescaped parameters" do
172
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/?a=a b"))).
173
- to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
174
- end
175
-
176
- it "should match if uri Addressable::Template pattern matches uri with escaped parameter value and request has escaped parameters" do
177
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/?a=a%20b"))).
178
- to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
179
- end
180
-
181
- end
182
-
183
- describe "when matching requests on query params" do
184
-
185
- describe "when uri is described as regexp" do
186
- it "should match request query params" do
187
- expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"a" => ["b", "c"]})).
188
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
189
- end
190
-
191
- it "should match request query params if params don't match" do
192
- expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"x" => ["b", "c"]})).
193
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
194
- end
195
-
196
- it "should match when query params are declared as HashIncluding matcher matching params" do
197
- expect(WebMock::RequestPattern.new(:get, /.*example.*/,
198
- query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
199
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
200
- end
201
-
202
- it "should not match when query params are declared as HashIncluding matcher not matching params" do
203
- expect(WebMock::RequestPattern.new(:get, /.*example.*/,
204
- query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
205
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
206
- end
207
-
208
- it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
209
- expect(WebMock::RequestPattern.new(:get, /.*example.*/,
210
- query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
211
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
212
- end
213
-
214
- it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
215
- expect(WebMock::RequestPattern.new(:get, /.*example.*/,
216
- query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
217
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
218
- end
219
- end
220
-
221
- describe "when uri is described as Addressable::Template" do
222
- it "should raise error if query params are specified" do
223
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"a" => ["b", "c"]})).
224
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
225
- end
226
-
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"), query: {"x" => ["b", "c"]})).
229
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
230
- end
231
-
232
- it "should match when query params are declared as HashIncluding matcher matching params" do
233
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
234
- query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
235
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
236
- end
237
-
238
- it "should not match when query params are declared as HashIncluding matcher not matching params" do
239
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
240
- query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
241
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
242
- end
243
-
244
- it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
245
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
246
- query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
247
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
248
- end
249
-
250
- it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
251
- expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
252
- query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
253
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
254
- end
255
- end
256
-
257
- describe "when uri is described as string" do
258
- it "should match when query params are the same as declared in hash" do
259
- expect(WebMock::RequestPattern.new(:get, "www.example.com", query: {"a" => ["b", "c"]})).
260
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
261
- end
262
-
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", query: {"a" => ["b", "c"]})).
265
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?x[]=b&a[]=c"))
266
- end
267
-
268
- it "should match when query params are the same as declared as string" do
269
- expect(WebMock::RequestPattern.new(:get, "www.example.com", query: "a[]=b&a[]=c")).
270
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
271
- end
272
-
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", query: "a[]=b&a[]=c")).
275
- to match(WebMock::RequestSignature.new(:get, "www.example.com/?x=3&a[]=b&a[]=c"))
276
- end
277
-
278
- it "should match when query params are declared as HashIncluding matcher matching params" do
279
- expect(WebMock::RequestPattern.new(:get, "www.example.com",
280
- query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
281
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
282
- end
283
-
284
- it "should not match when query params are declared as HashIncluding matcher not matching params" do
285
- expect(WebMock::RequestPattern.new(:get, "www.example.com",
286
- query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
287
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
288
- end
289
-
290
- it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
291
- expect(WebMock::RequestPattern.new(:get, "www.example.com",
292
- query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
293
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
294
- end
295
-
296
- it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
297
- expect(WebMock::RequestPattern.new(:get, "www.example.com",
298
- query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
299
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
300
- end
301
-
302
- context "when using query values notation as flat array" do
303
- before :all do
304
- WebMock::Config.instance.query_values_notation = :flat_array
305
- end
306
-
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", query: "a=b&a=c")).
309
- to match(WebMock::RequestSignature.new(:get, "www.example.com?a=b&a=c"))
310
- end
311
-
312
- after :all do
313
- WebMock::Config.instance.query_values_notation = nil
314
- end
315
- end
316
- end
317
- end
318
-
319
- describe "when matching requests with body" do
320
-
321
- it "should match if request body and body pattern are the same" do
322
- expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "abc")).
323
- to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
324
- end
325
-
326
- it "should match if request body matches regexp" do
327
- expect(WebMock::RequestPattern.new(:get, "www.example.com", body: /^abc$/)).
328
- to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
329
- end
330
-
331
- it "should not match if body pattern is different than request body" do
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
- end
335
-
336
- it "should not match if request body doesn't match regexp pattern" do
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
- end
340
-
341
- it "should match if pattern doesn't have specified body" do
342
- expect(WebMock::RequestPattern.new(:get, "www.example.com")).
343
- to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
344
- end
345
-
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", body: nil)).
348
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
349
- end
350
-
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", body: "")).
353
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))
354
- end
355
-
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", body: "abc")).
358
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
359
- end
360
-
361
- describe "when body in pattern is declared as a hash" do
362
- let(:body_hash) { {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}} }
363
-
364
- describe "for request with url encoded body" do
365
- it "should match when hash matches body" do
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
- end
369
-
370
- it "should match when hash matches body in different order of params" do
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
- end
374
-
375
- it "should not match when hash doesn't match url encoded body" do
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
- end
379
-
380
- it "should not match when body is not url encoded" do
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
- end
384
-
385
- it "should match when hash contains regex values" do
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
- end
389
-
390
- it "should not match when hash does not contains regex values" do
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
- end
394
-
395
- context 'body is an hash with an array of hashes' do
396
- let(:body_hash) { {a: [{'b' => '1'}, {'b' => '2'}]} }
397
-
398
- it "should match when hash matches body" do
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
- end
402
- end
403
-
404
- context 'body is an hash with an array of hashes with multiple keys' do
405
- let(:body_hash) { {a: [{'b' => '1', 'a' => '2'}, {'b' => '3'}]} }
406
-
407
- it "should match when hash matches body" do
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
- end
411
- end
412
- end
413
-
414
- describe "for request with json body and content type is set to json" do
415
- it "should match when hash matches body" do
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
- end
420
-
421
- it "should match if hash matches body in different form" do
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
- end
426
-
427
- it "should not match when body is not json" do
428
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
429
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
430
- headers: {content_type: 'application/json'}, body: "foo bar"))
431
- end
432
-
433
- it "should not match if request body is different" do
434
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
435
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
436
- headers: {content_type: 'application/json'}, body: "{\"a\":1,\"c\":null}"))
437
- end
438
-
439
- it "should not match if request body is has less params than pattern" do
440
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
441
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
442
- headers: {content_type: 'application/json'}, body: "{\"a\":1}"))
443
- end
444
-
445
- it "should not match if request body is has more params than pattern" do
446
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1})).
447
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
448
- headers: {content_type: 'application/json'}, body: "{\"a\":1,\"c\":1}"))
449
- end
450
- end
451
-
452
- describe "for request with xml body and content type is set to xml" do
453
- let(:body_hash) { {"opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}}} }
454
-
455
- it "should match when hash matches body" do
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
- end
460
-
461
- it "should match if hash matches body in different form" do
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
- end
466
-
467
- it "should not match when body is not xml" do
468
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
469
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
470
- headers: {content_type: 'application/xml'}, body: "foo bar"))
471
- end
472
-
473
- it "matches when the content type include a charset" do
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
-
478
- end
479
- end
480
- end
481
-
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", body: 'a=1&c[d][]=e&c[d][]=f&b=five') }
484
-
485
- it "should match when query params are declared as HashIncluding matcher matching params" do
486
- expect(WebMock::RequestPattern.new(:post, "www.example.com",
487
- body: WebMock::Matchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}}))).
488
- to match(signature)
489
- end
490
-
491
- it "should not match when query params are declared as HashIncluding matcher not matching params" do
492
- expect(WebMock::RequestPattern.new(:post, "www.example.com",
493
- body: WebMock::Matchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}}))).
494
- not_to match(signature)
495
- end
496
-
497
- it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
498
- expect(WebMock::RequestPattern.new(:post, "www.example.com",
499
- body: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}}))).
500
- to match(signature)
501
- end
502
-
503
- it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
504
- expect(WebMock::RequestPattern.new(:post, "www.example.com",
505
- body: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}}))).
506
- not_to match(signature)
507
- end
508
- end
509
- end
510
-
511
- it "should match if pattern and request have the same headers" do
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
- end
515
-
516
- it "should match if pattern headers values are regexps matching request header values" do
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
- end
520
-
521
- it "should not match if pattern has different value of header than request" do
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
- end
525
-
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", headers: {'Content-Type' => %r{^image\/jpeg$}})).
528
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpegx'}))
529
- end
530
-
531
- it "should match if request has more headers than request pattern" do
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
- end
535
-
536
- it "should not match if request has less headers than the request pattern" do
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
- end
540
-
541
- it "should match even is header keys are declared in different form" do
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
- end
545
-
546
- it "should match is pattern doesn't have specified headers" do
547
- expect(WebMock::RequestPattern.new(:get, "www.example.com")).
548
- to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))
549
- end
550
-
551
- it "should not match if pattern has nil headers but request has headers" do
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
- end
555
-
556
- it "should not match if pattern has empty headers but request has headers" do
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
- end
560
-
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", headers: {'A'=>'a'})).
563
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
564
- end
565
-
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", headers: {'A'=>'a'})).
568
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {}))
569
- end
570
-
571
- it "should match if block given in pattern evaluates request to true" do
572
- expect(WebMock::RequestPattern.new(:get, "www.example.com").with { |request| true }).
573
- to match(WebMock::RequestSignature.new(:get, "www.example.com"))
574
- end
575
-
576
- it "should not match if block given in pattrn evaluates request to false" do
577
- expect(WebMock::RequestPattern.new(:get, "www.example.com").with { |request| false }).
578
- not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
579
- end
580
-
581
- it "should yield block with request signature" do
582
- signature = WebMock::RequestSignature.new(:get, "www.example.com")
583
- expect(WebMock::RequestPattern.new(:get, "www.example.com").with { |request| request == signature }).
584
- to match(signature)
585
- end
586
-
587
- end
588
-
589
-
590
- end