webmock 3.5.0 → 3.25.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 (142) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +476 -2
  3. data/README.md +166 -42
  4. data/lib/webmock/api.rb +2 -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 +228 -0
  12. data/lib/webmock/http_lib_adapters/curb_adapter.rb +14 -3
  13. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +22 -9
  14. data/lib/webmock/http_lib_adapters/excon_adapter.rb +5 -0
  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 +17 -5
  19. data/lib/webmock/http_lib_adapters/http_rb/response.rb +49 -5
  20. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +11 -3
  21. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +8 -2
  22. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +7 -5
  23. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +25 -7
  24. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +35 -15
  25. data/lib/webmock/http_lib_adapters/net_http.rb +59 -112
  26. data/lib/webmock/http_lib_adapters/net_http_response.rb +3 -1
  27. data/lib/webmock/http_lib_adapters/patron_adapter.rb +4 -2
  28. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +18 -2
  29. data/lib/webmock/matchers/any_arg_matcher.rb +2 -0
  30. data/lib/webmock/matchers/hash_argument_matcher.rb +2 -0
  31. data/lib/webmock/matchers/hash_excluding_matcher.rb +2 -0
  32. data/lib/webmock/matchers/hash_including_matcher.rb +2 -0
  33. data/lib/webmock/minitest.rb +2 -0
  34. data/lib/webmock/rack_response.rb +5 -1
  35. data/lib/webmock/request_body_diff.rb +3 -1
  36. data/lib/webmock/request_execution_verifier.rb +2 -0
  37. data/lib/webmock/request_pattern.rb +118 -61
  38. data/lib/webmock/request_registry.rb +2 -0
  39. data/lib/webmock/request_signature.rb +4 -2
  40. data/lib/webmock/request_signature_snippet.rb +2 -0
  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 +4 -1
  48. data/lib/webmock/stub_registry.rb +28 -11
  49. data/lib/webmock/stub_request_snippet.rb +2 -0
  50. data/lib/webmock/test_unit.rb +3 -3
  51. data/lib/webmock/util/hash_counter.rb +12 -6
  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 +23 -10
  55. data/lib/webmock/util/parsers/json.rb +72 -0
  56. data/lib/webmock/util/parsers/parse_error.rb +7 -0
  57. data/lib/webmock/util/parsers/xml.rb +16 -0
  58. data/lib/webmock/util/query_mapper.rb +6 -2
  59. data/lib/webmock/util/uri.rb +11 -9
  60. data/lib/webmock/util/values_stringifier.rb +2 -0
  61. data/lib/webmock/util/version_checker.rb +7 -5
  62. data/lib/webmock/version.rb +3 -1
  63. data/lib/webmock/webmock.rb +22 -3
  64. data/lib/webmock.rb +5 -2
  65. metadata +99 -175
  66. data/.gemtest +0 -0
  67. data/.gitignore +0 -34
  68. data/.rspec-tm +0 -2
  69. data/.travis.yml +0 -21
  70. data/Gemfile +0 -9
  71. data/Rakefile +0 -30
  72. data/lib/webmock/util/json.rb +0 -67
  73. data/minitest/test_helper.rb +0 -34
  74. data/minitest/test_webmock.rb +0 -9
  75. data/minitest/webmock_spec.rb +0 -60
  76. data/spec/acceptance/curb/curb_spec.rb +0 -481
  77. data/spec/acceptance/curb/curb_spec_helper.rb +0 -147
  78. data/spec/acceptance/em_http_request/em_http_request_spec.rb +0 -406
  79. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +0 -77
  80. data/spec/acceptance/excon/excon_spec.rb +0 -77
  81. data/spec/acceptance/excon/excon_spec_helper.rb +0 -50
  82. data/spec/acceptance/http_rb/http_rb_spec.rb +0 -82
  83. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +0 -54
  84. data/spec/acceptance/httpclient/httpclient_spec.rb +0 -217
  85. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +0 -57
  86. data/spec/acceptance/manticore/manticore_spec.rb +0 -56
  87. data/spec/acceptance/manticore/manticore_spec_helper.rb +0 -35
  88. data/spec/acceptance/net_http/net_http_shared.rb +0 -153
  89. data/spec/acceptance/net_http/net_http_spec.rb +0 -331
  90. data/spec/acceptance/net_http/net_http_spec_helper.rb +0 -64
  91. data/spec/acceptance/net_http/real_net_http_spec.rb +0 -20
  92. data/spec/acceptance/patron/patron_spec.rb +0 -125
  93. data/spec/acceptance/patron/patron_spec_helper.rb +0 -54
  94. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +0 -313
  95. data/spec/acceptance/shared/callbacks.rb +0 -147
  96. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +0 -36
  97. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +0 -95
  98. data/spec/acceptance/shared/precedence_of_stubs.rb +0 -15
  99. data/spec/acceptance/shared/request_expectations.rb +0 -923
  100. data/spec/acceptance/shared/returning_declared_responses.rb +0 -388
  101. data/spec/acceptance/shared/stubbing_requests.rb +0 -638
  102. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +0 -135
  103. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +0 -60
  104. data/spec/acceptance/webmock_shared.rb +0 -41
  105. data/spec/fixtures/test.txt +0 -1
  106. data/spec/quality_spec.rb +0 -84
  107. data/spec/spec_helper.rb +0 -48
  108. data/spec/support/example_curl_output.txt +0 -22
  109. data/spec/support/failures.rb +0 -9
  110. data/spec/support/my_rack_app.rb +0 -53
  111. data/spec/support/network_connection.rb +0 -19
  112. data/spec/support/webmock_server.rb +0 -69
  113. data/spec/unit/api_spec.rb +0 -175
  114. data/spec/unit/errors_spec.rb +0 -129
  115. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +0 -17
  116. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +0 -12
  117. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +0 -61
  118. data/spec/unit/matchers/hash_including_matcher_spec.rb +0 -87
  119. data/spec/unit/rack_response_spec.rb +0 -112
  120. data/spec/unit/request_body_diff_spec.rb +0 -90
  121. data/spec/unit/request_execution_verifier_spec.rb +0 -208
  122. data/spec/unit/request_pattern_spec.rb +0 -596
  123. data/spec/unit/request_registry_spec.rb +0 -95
  124. data/spec/unit/request_signature_snippet_spec.rb +0 -89
  125. data/spec/unit/request_signature_spec.rb +0 -155
  126. data/spec/unit/request_stub_spec.rb +0 -199
  127. data/spec/unit/response_spec.rb +0 -282
  128. data/spec/unit/stub_registry_spec.rb +0 -103
  129. data/spec/unit/stub_request_snippet_spec.rb +0 -115
  130. data/spec/unit/util/hash_counter_spec.rb +0 -39
  131. data/spec/unit/util/hash_keys_stringifier_spec.rb +0 -27
  132. data/spec/unit/util/headers_spec.rb +0 -28
  133. data/spec/unit/util/json_spec.rb +0 -33
  134. data/spec/unit/util/query_mapper_spec.rb +0 -150
  135. data/spec/unit/util/uri_spec.rb +0 -299
  136. data/spec/unit/util/version_checker_spec.rb +0 -65
  137. data/spec/unit/webmock_spec.rb +0 -11
  138. data/test/http_request.rb +0 -24
  139. data/test/shared_test.rb +0 -108
  140. data/test/test_helper.rb +0 -23
  141. data/test/test_webmock.rb +0 -6
  142. data/webmock.gemspec +0 -46
@@ -1,638 +0,0 @@
1
- # encoding: utf-8
2
-
3
- shared_examples_for "stubbing requests" do |*adapter_info|
4
- describe "when requests are stubbed" do
5
- describe "based on uri" do
6
- it "should return stubbed response even if request have escaped parameters" do
7
- stub_request(:get, "www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}").to_return(body: "abc")
8
- expect(http_request(:get, "http://www.example.com/hello%2B/?#{ESCAPED_PARAMS}").body).to eq("abc")
9
- end
10
-
11
- it "should return stubbed response even if query params have integer values" do
12
- stub_request(:get, "www.example.com").with(query: {"a" => 1}).to_return(body: "abc")
13
- expect(http_request(:get, "http://www.example.com/?a=1").body).to eq("abc")
14
- end
15
-
16
- it "should return stubbed response even if request has non escaped params" do
17
- stub_request(:get, "www.example.com/hello%2B/?#{ESCAPED_PARAMS}").to_return(body: "abc")
18
- expect(http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}").body).to eq("abc")
19
- end
20
-
21
- it "should return stubbed response for url with non utf query params" do
22
- param = 'aäoöuü'.encode('iso-8859-1')
23
- param = CGI.escape(param)
24
- stub_request(:get, "www.example.com/?#{param}").to_return(body: "abc")
25
- expect(http_request(:get, "http://www.example.com/?#{param}").body).to eq("abc")
26
- end
27
-
28
- it "should return stubbed response even if stub uri is declared as regexp and request params are escaped" do
29
- stub_request(:get, /.*x=ab c.*/).to_return(body: "abc")
30
- expect(http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body).to eq("abc")
31
- end
32
-
33
- it "should raise error specifying stubbing instructions with escaped characters in params if there is no matching stub" do
34
- begin
35
- http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}")
36
- rescue WebMock::NetConnectNotAllowedError => e
37
- expect(e.message).to match(/Unregistered request: GET http:\/\/www\.example\.com\/hello\+\/\?x=ab%20c&z='Stop!'%20said%20Fred%20m/m)
38
- expect(e.message).to match(/stub_request\(:get, "http:\/\/www\.example\.com\/hello\+\/\?x=ab%20c&z='Stop!'%20said%20Fred%20m"\)/m)
39
- end
40
-
41
- stub_request(:get, "http://www.example.com/hello+/?x=ab%20c&z='Stop!'%20said%20Fred%20m")
42
- http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}")
43
- end
44
- end
45
-
46
- describe "based on query params" do
47
- it "should return stubbed response when stub declares query params as a hash" do
48
- stub_request(:get, "www.example.com").with(query: {"a" => ["b x", "c d"]}).to_return(body: "abc")
49
- expect(http_request(:get, "http://www.example.com/?a[]=b+x&a[]=c%20d").body).to eq("abc")
50
- end
51
-
52
- it "should return stubbed response when stub declares query params as a hash" do
53
- stub_request(:get, "www.example.com").with(query: "a[]=b&a[]=c").to_return(body: "abc")
54
- expect(http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body).to eq("abc")
55
- end
56
-
57
- it "should return stubbed response when stub declares query params both in uri and as a hash" do
58
- stub_request(:get, "www.example.com/?x=3").with(query: {"a" => ["b", "c"]}).to_return(body: "abc")
59
- expect(http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body).to eq("abc")
60
- end
61
-
62
- it "should return stubbed response when stub expects only part of query params" do
63
- stub_request(:get, "www.example.com").with(query: hash_including({"a" => ["b", "c"]})).to_return(body: "abc")
64
- expect(http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1").body).to eq("abc")
65
- end
66
-
67
- it 'should return stubbed response when stub expects exclude part of query params' do
68
- stub_request(:get, 'www.example.com').with(query: hash_excluding(a: ['b', 'c'])).to_return(body: 'abc')
69
- expect(http_request(:get, 'http://www.example.com/?a[]=c&a[]=d&b=1').body).to eq('abc')
70
- end
71
- end
72
-
73
- describe "based on method" do
74
- it "should return stubbed response" do
75
- stub_request(:get, "www.example.com")
76
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
77
- end
78
-
79
- it "should match stubbed request when http request was made with method given as string" do
80
- stub_request(:get, "www.example.com")
81
- expect(http_request('get', "http://www.example.com/").status).to eq("200")
82
- end
83
-
84
- it "should raise error if stubbed request has different method" do
85
- stub_request(:get, "www.example.com")
86
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
87
- expect {
88
- http_request(:delete, "http://www.example.com/")
89
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/)
90
- )
91
- end
92
- end
93
-
94
- describe "based on body" do
95
- it "should match requests if body is the same" do
96
- stub_request(:post, "www.example.com").with(body: "abc")
97
- expect(http_request(
98
- :post, "http://www.example.com/",
99
- body: "abc").status).to eq("200")
100
- end
101
-
102
- it "should match requests if body is not set in the stub" do
103
- stub_request(:post, "www.example.com")
104
- expect(http_request(
105
- :post, "http://www.example.com/",
106
- body: "abc").status).to eq("200")
107
- end
108
-
109
- it "should not match requests if body is different" do
110
- stub_request(:post, "www.example.com").with(body: "abc")
111
- expect {
112
- http_request(:post, "http://www.example.com/", body: "def")
113
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'def'))
114
- end
115
-
116
- describe "with regular expressions" do
117
- it "should match requests if body matches regexp" do
118
- stub_request(:post, "www.example.com").with(body: /\d+abc$/)
119
- expect(http_request(
120
- :post, "http://www.example.com/",
121
- body: "123abc").status).to eq("200")
122
- end
123
-
124
- it "should not match requests if body doesn't match regexp" do
125
- stub_request(:post, "www.example.com").with(body: /^abc/)
126
- expect {
127
- http_request(:post, "http://www.example.com/", body: "xabc")
128
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'xabc'))
129
- end
130
- end
131
-
132
- describe "when body is declared as a hash" do
133
- before(:each) do
134
- stub_request(:post, "www.example.com").
135
- with(body: {:a => '1', :b => 'five x', 'c' => {'d' => ['e', 'f']} })
136
- end
137
-
138
- describe "for request with url encoded body" do
139
- it "should match request if hash matches body" do
140
- expect(http_request(
141
- :post, "http://www.example.com/",
142
- body: 'a=1&c[d][]=e&c[d][]=f&b=five+x').status).to eq("200")
143
- end
144
-
145
- it "should match request if hash matches body in different order of params" do
146
- expect(http_request(
147
- :post, "http://www.example.com/",
148
- body: 'a=1&c[d][]=e&b=five+x&c[d][]=f').status).to eq("200")
149
- end
150
-
151
- it "should not match if hash doesn't match url encoded body" do
152
- expect {
153
- http_request(
154
- :post, "http://www.example.com/",
155
- body: 'c[d][]=f&a=1&c[d][]=e')
156
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'c\[d\]\[\]=f&a=1&c\[d\]\[\]=e'))
157
- end
158
-
159
- describe "for request with form url encoded body and content type" do
160
- it "should match if stubbed request body hash has string values matching string values in request body" do
161
- WebMock.reset!
162
- stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
163
- expect(http_request(
164
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
165
- body: "foo=1").status).to eq("200")
166
- end
167
-
168
- it "should match if stubbed request body hash has NON string values matching string values in request body" do
169
- WebMock.reset!
170
- stub_request(:post, "www.example.com").with(body: {"foo" => 1})
171
- expect(http_request(
172
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
173
- body: "foo=1").status).to eq("200")
174
- end
175
-
176
- it "should match if stubbed request body is hash_included" do
177
- WebMock.reset!
178
- stub_request(:post, "www.example.com").with(body: {"foo" => hash_including("bar" => '1')})
179
- expect(http_request(
180
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
181
- body: "foo[bar]=1").status).to eq("200")
182
- end
183
- end
184
- end
185
-
186
-
187
- describe "for request with json body and content type is set to json" do
188
- it "should match if hash matches body" do
189
- expect(http_request(
190
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
191
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five x\"}").status).to eq("200")
192
- end
193
-
194
- it "should match if hash matches body in different form" do
195
- expect(http_request(
196
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
197
- body: "{\"a\":\"1\",\"b\":\"five x\",\"c\":{\"d\":[\"e\",\"f\"]}}").status).to eq("200")
198
- end
199
-
200
- it "should match if hash contains date string" do #Crack creates date object
201
- WebMock.reset!
202
- stub_request(:post, "www.example.com").
203
- with(body: {"foo" => "2010-01-01"})
204
- expect(http_request(
205
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
206
- body: "{\"foo\":\"2010-01-01\"}").status).to eq("200")
207
- end
208
-
209
- it "should match if any of the strings have spaces" do
210
- WebMock.reset!
211
- stub_request(:post, "www.example.com").with(body: {"foo" => "a b c"})
212
- expect(http_request(
213
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
214
- body: "{\"foo\":\"a b c\"}").status).to eq("200")
215
- end
216
-
217
- it "should match if stubbed request body hash has NON string values matching NON string values in request body" do
218
- WebMock.reset!
219
- stub_request(:post, "www.example.com").with(body: {"foo" => 1})
220
- expect(http_request(
221
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
222
- body: "{\"foo\":1}").status).to eq("200")
223
- end
224
-
225
- it "should not match if stubbed request body hash has string values matching NON string values in request body" do
226
- WebMock.reset!
227
- stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
228
- expect{http_request(
229
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
230
- body: "{\"foo\":1}") }.to raise_error(WebMock::NetConnectNotAllowedError)
231
- end
232
-
233
- it "should not match if stubbed request body hash has NON string values matching string values in request body" do
234
- WebMock.reset!
235
- stub_request(:post, "www.example.com").with(body: {"foo" => 1})
236
- expect{http_request(
237
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
238
- body: "{\"foo\":\"1\"}") }.to raise_error(WebMock::NetConnectNotAllowedError)
239
- end
240
- end
241
-
242
- describe "for request with xml body and content type is set to xml" do
243
- before(:each) do
244
- WebMock.reset!
245
- stub_request(:post, "www.example.com").
246
- with(body: { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
247
- end
248
-
249
- it "should match if hash matches body" do
250
- expect(http_request(
251
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
252
- body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status).to eq("200")
253
- end
254
-
255
- it "should match if hash matches body in different form" do
256
- expect(http_request(
257
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
258
- body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status).to eq("200")
259
- end
260
-
261
- it "should match if hash contains date string" do #Crack creates date object
262
- WebMock.reset!
263
- stub_request(:post, "www.example.com").
264
- with(body: {"opt" => {"foo" => "2010-01-01"}})
265
- expect(http_request(
266
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
267
- body: "<opt foo=\"2010-01-01\">\n</opt>\n").status).to eq("200")
268
- end
269
- end
270
- end
271
-
272
- describe "when body is declared as partial hash matcher" do
273
- subject(:request) { http_request( :post, "http://www.example.com/",
274
- body: 'a=1&c[d][]=e&c[d][]=f&b=five') }
275
-
276
- subject(:wrong_request) { http_request(:post, "http://www.example.com/",
277
- body: 'c[d][]=f&a=1&c[d][]=e').status }
278
-
279
- describe "when using 'RSpec:Mocks::ArgumentMatchers#hash_including'" do
280
- before(:each) do
281
- stub_request(:post, "www.example.com").
282
- with(body: hash_including(:a, c: {'d' => ['e', 'f']} ))
283
- end
284
-
285
- describe "for request with url encoded body" do
286
- it "should match request if hash matches body" do
287
- expect(request.status).to eq("200")
288
- end
289
-
290
- it "should not match if hash doesn't match url encoded body" do
291
- expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
292
- end
293
- end
294
-
295
- describe "for request with json body and content type is set to json" do
296
- it "should match if hash matches body" do
297
- expect(http_request(
298
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
299
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
300
- end
301
- end
302
- end
303
-
304
- describe "when using 'WebMock::API#hash_including'" do
305
- before(:each) do
306
- stub_request(:post, "www.example.com").
307
- with(body: WebMock::API.hash_including(:a, c: {'d' => ['e', 'f']} ))
308
- end
309
-
310
- describe "for request with url encoded body" do
311
- it "should match request if hash matches body" do
312
- expect(request.status).to eq("200")
313
- end
314
-
315
- it "should not match if hash doesn't match url encoded body" do
316
- expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
317
- end
318
- end
319
-
320
- describe "for request with json body and content type is set to json" do
321
- it "should match if hash matches body" do
322
- expect(http_request(
323
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
324
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
325
- end
326
- end
327
- end
328
-
329
- end
330
- end
331
-
332
- describe "based on headers" do
333
- it "should match requests if headers are the same" do
334
- stub_request(:get, "www.example.com").with(headers: SAMPLE_HEADERS )
335
- expect(http_request(
336
- :get, "http://www.example.com/",
337
- headers: SAMPLE_HEADERS).status).to eq("200")
338
- end
339
-
340
- it "should match requests if headers are the same and declared as array" do
341
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b"]} )
342
- expect(http_request(
343
- :get, "http://www.example.com/",
344
- headers: {"a" => "b"}).status).to eq("200")
345
- end
346
-
347
- describe "when multiple headers with the same key are used" do
348
- it "should match requests if headers are the same" do
349
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
350
- expect(http_request(
351
- :get, "http://www.example.com/",
352
- headers: {"a" => ["b", "c"]}).status).to eq("200")
353
- end
354
-
355
- it "should match requests if headers are the same but in different order" do
356
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
357
- expect(http_request(
358
- :get, "http://www.example.com/",
359
- headers: {"a" => ["c", "b"]}).status).to eq("200")
360
- end
361
-
362
- it "should not match requests if headers are different" do
363
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]})
364
-
365
- expect {
366
- http_request(
367
- :get, "http://www.example.com/",
368
- headers: {"a" => ["b", "d"]})
369
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
370
- end
371
- end
372
-
373
- it "should match requests if request headers are not stubbed" do
374
- stub_request(:get, "www.example.com")
375
- expect(http_request(
376
- :get, "http://www.example.com/",
377
- headers: SAMPLE_HEADERS).status).to eq("200")
378
- end
379
-
380
- it "should not match requests if headers are different" do
381
- stub_request(:get, "www.example.com").with(headers: SAMPLE_HEADERS)
382
-
383
- expect {
384
- http_request(
385
- :get, "http://www.example.com/",
386
- headers: { 'Content-Length' => '9999'})
387
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
388
- end
389
-
390
- it "should not match if accept header is different" do
391
- stub_request(:get, "www.example.com").
392
- with(headers: { 'Accept' => 'application/json'})
393
- expect {
394
- http_request(
395
- :get, "http://www.example.com/",
396
- headers: { 'Accept' => 'application/xml'})
397
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
398
- end
399
-
400
- describe "declared as regular expressions" do
401
- it "should match requests if header values match regular expression" do
402
- stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
403
- expect(http_request(
404
- :get, "http://www.example.com/",
405
- headers: { 'some-header' => 'MyAppName' }).status).to eq("200")
406
- end
407
-
408
- it "should not match requests if headers values do not match regular expression" do
409
- stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
410
-
411
- expect {
412
- http_request(
413
- :get, "http://www.example.com/",
414
- headers: { 'some-header' => 'xMyAppName' })
415
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
416
- end
417
- end
418
- end
419
-
420
- describe "when stubbing request with userinfo in url", unless: (adapter_info.include?(:no_url_auth)) do
421
- it "should match if credentials are the same" do
422
- stub_request(:get, "user:pass@www.example.com")
423
- expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
424
- end
425
-
426
- it "should not match if credentials are different" do
427
- stub_request(:get, "user:pass@www.example.com")
428
- expect {
429
- expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
430
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
431
- end
432
-
433
- it "should not match if credentials are stubbed but not provided in the request" do
434
- stub_request(:get, "user:pass@www.example.com")
435
- expect {
436
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
437
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
438
- end
439
-
440
- it "should not match if credentials are not stubbed but exist in the request" do
441
- stub_request(:get, "www.example.com")
442
- expect {
443
- expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
444
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
445
- end
446
-
447
- it "should not match if request has credentials provided in basic authentication herader" do
448
- stub_request(:get, "user:pass@www.example.com")
449
- expect {
450
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
451
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
452
- end
453
- end
454
-
455
- describe "when stubbing request with basic authentication header" do
456
- it "should match if credentials are the same" do
457
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
458
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
459
- end
460
-
461
- it "should match if credentials are the same and encode to more than one line" do
462
- stub_request(:get, "www.example.com").with(basic_auth: ['user' * 5, 'pass' * 5])
463
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user' * 5, 'pass' * 5]).status).to eq("200")
464
- end
465
-
466
- it "should match if credentials are the same and were provided directly as authentication headers in request" do
467
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
468
- expect(http_request(:get, "http://www.example.com/", headers: {'Authorization'=>'Basic dXNlcjpwYXNz'}).status).to eq("200")
469
- end
470
-
471
- it "should match if credentials are the same and have been declared in the stub as encoded header" do
472
- stub_request(:get, "www.example.com").with(headers: {'Authorization'=>'Basic dXNlcjpwYXNz'})
473
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
474
- end
475
-
476
- it "should not match if credentials are different" do
477
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
478
- expect {
479
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pazz']).status).to eq("200")
480
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
481
- end
482
-
483
- it "should not match if credentials are stubbed but not provided in the request" do
484
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
485
- expect {
486
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
487
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
488
- end
489
-
490
- it "should match if credentials are not stubbed but exist in the request" do
491
- stub_request(:get, "www.example.com")
492
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
493
- end
494
-
495
- it "should not match if request has credentials provides in userinfo", unless: (adapter_info.include?(:no_url_auth)) do
496
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
497
- expect {
498
- expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
499
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pass@www.example.com/))
500
- end
501
- end
502
-
503
- describe "when stubbing request with a global hook" do
504
- after(:each) do
505
- WebMock::StubRegistry.instance.global_stubs.clear
506
- end
507
-
508
- it 'returns the response returned by the hook' do
509
- WebMock.globally_stub_request do |request|
510
- { body: "global stub body" }
511
- end
512
-
513
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
514
- end
515
-
516
- it 'does not get cleared when a user calls WebMock.reset!' do
517
- WebMock.globally_stub_request do |request|
518
- { body: "global stub body" }
519
- end
520
- WebMock.reset!
521
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
522
- end
523
-
524
- it "does not stub the request if the hook does not return anything" do
525
- WebMock.globally_stub_request { |r| }
526
- expect {
527
- http_request(:get, "http://www.example.com/")
528
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
529
- end
530
-
531
- it "passes the request to the block" do
532
- passed_request = nil
533
- WebMock.globally_stub_request do |request|
534
- passed_request = request
535
- { body: "global stub body" }
536
- end
537
-
538
- http_request(:get, "http://www.example.com:456/bar")
539
- expect(passed_request.uri.to_s).to eq("http://www.example.com:456/bar")
540
- end
541
-
542
- it "should call the block only once per request" do
543
- call_count = 0
544
- WebMock.globally_stub_request do |request|
545
- call_count += 1
546
- { body: "global stub body" }
547
- end
548
- http_request(:get, "http://www.example.com/")
549
- expect(call_count).to eq(1)
550
- end
551
-
552
- it 'supports multiple global stubs; the first registered one that returns a non-nil value determines the stub' do
553
- stub_invocation_order = []
554
- WebMock.globally_stub_request do |request|
555
- stub_invocation_order << :nil_stub
556
- nil
557
- end
558
-
559
- WebMock.globally_stub_request do |request|
560
- stub_invocation_order << :hash_stub
561
- { body: "global stub body" }
562
- end
563
-
564
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
565
- expect(stub_invocation_order).to eq([:nil_stub, :hash_stub])
566
- end
567
-
568
- [:before, :after].each do |before_or_after|
569
- context "when there is also a non-global registered stub #{before_or_after} the global stub" do
570
- def stub_non_globally
571
- stub_request(:get, "www.example.com").to_return(body: 'non-global stub body')
572
- end
573
-
574
- define_method :register_stubs do |block|
575
- stub_non_globally if before_or_after == :before
576
- WebMock.globally_stub_request(&block)
577
- stub_non_globally if before_or_after == :after
578
- end
579
-
580
- it 'uses the response from the global stub if the block returns a non-nil value' do
581
- register_stubs(lambda { |req| { body: 'global stub body' } })
582
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
583
- end
584
-
585
- it 'uses the response from the non-global stub if the block returns a nil value' do
586
- register_stubs(lambda { |req| nil })
587
- expect(http_request(:get, "http://www.example.com/").body).to eq("non-global stub body")
588
- end
589
- end
590
- end
591
- end
592
-
593
- describe "when stubbing request with a block evaluated on request" do
594
- it "should match if block returns true" do
595
- stub_request(:get, "www.example.com").with { |request| true }
596
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
597
- end
598
-
599
- it "should not match if block returns false" do
600
- stub_request(:get, "www.example.com").with { |request| false }
601
- expect {
602
- http_request(:get, "http://www.example.com/")
603
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
604
- end
605
-
606
- it "should pass the request to the block" do
607
- stub_request(:post, "www.example.com").with { |request| request.body == "wadus" }
608
- expect(http_request(
609
- :post, "http://www.example.com/",
610
- body: "wadus").status).to eq("200")
611
- expect {
612
- http_request(:post, "http://www.example.com/", body: "jander")
613
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'))
614
- end
615
-
616
- it "should call the block only once per request" do
617
- call_count = 0
618
- stub_request(:get, "www.example.com").with { |request| call_count += 1; true }
619
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
620
- expect(call_count).to eq(1)
621
- end
622
- end
623
- end
624
-
625
- describe "when request stub was removed" do
626
- it "should raise an error on request" do
627
- stub = stub_request(:get, "www.example.com")
628
-
629
- http_request(:get, "http://www.example.com/")
630
-
631
- remove_request_stub(stub)
632
-
633
- expect {
634
- http_request(:get, "http://www.example.com/")
635
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
636
- end
637
- end
638
- end