webmock 3.7.1

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