webmock 3.0.1 → 3.20.0

Sign up to get free protection for your applications and to get access to all the features.
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,583 +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
- end
67
-
68
- describe "based on method" do
69
- it "should return stubbed response" do
70
- stub_request(:get, "www.example.com")
71
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
72
- end
73
-
74
- it "should match stubbed request when http request was made with method given as string" 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 raise error if stubbed request has different method" do
80
- stub_request(:get, "www.example.com")
81
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
82
- expect {
83
- http_request(:delete, "http://www.example.com/")
84
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/)
85
- )
86
- end
87
- end
88
-
89
- describe "based on body" do
90
- it "should match requests if body is the same" do
91
- stub_request(:post, "www.example.com").with(body: "abc")
92
- expect(http_request(
93
- :post, "http://www.example.com/",
94
- body: "abc").status).to eq("200")
95
- end
96
-
97
- it "should match requests if body is not set in the stub" do
98
- stub_request(:post, "www.example.com")
99
- expect(http_request(
100
- :post, "http://www.example.com/",
101
- body: "abc").status).to eq("200")
102
- end
103
-
104
- it "should not match requests if body is different" do
105
- stub_request(:post, "www.example.com").with(body: "abc")
106
- expect {
107
- http_request(:post, "http://www.example.com/", body: "def")
108
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'def'))
109
- end
110
-
111
- describe "with regular expressions" do
112
- it "should match requests if body matches regexp" do
113
- stub_request(:post, "www.example.com").with(body: /\d+abc$/)
114
- expect(http_request(
115
- :post, "http://www.example.com/",
116
- body: "123abc").status).to eq("200")
117
- end
118
-
119
- it "should not match requests if body doesn't match regexp" do
120
- stub_request(:post, "www.example.com").with(body: /^abc/)
121
- expect {
122
- http_request(:post, "http://www.example.com/", body: "xabc")
123
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'xabc'))
124
- end
125
- end
126
-
127
- describe "when body is declared as a hash" do
128
- before(:each) do
129
- stub_request(:post, "www.example.com").
130
- with(body: {:a => '1', :b => 'five x', 'c' => {'d' => ['e', 'f']} })
131
- end
132
-
133
- describe "for request with url encoded body" do
134
- it "should match request if hash matches body" do
135
- expect(http_request(
136
- :post, "http://www.example.com/",
137
- body: 'a=1&c[d][]=e&c[d][]=f&b=five+x').status).to eq("200")
138
- end
139
-
140
- it "should match request if hash matches body in different order of params" do
141
- expect(http_request(
142
- :post, "http://www.example.com/",
143
- body: 'a=1&c[d][]=e&b=five+x&c[d][]=f').status).to eq("200")
144
- end
145
-
146
- it "should not match if hash doesn't match url encoded body" do
147
- expect {
148
- http_request(
149
- :post, "http://www.example.com/",
150
- body: 'c[d][]=f&a=1&c[d][]=e')
151
- }.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'))
152
- end
153
- end
154
-
155
-
156
- describe "for request with json body and content type is set to json" do
157
- it "should match if hash matches body" do
158
- expect(http_request(
159
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
160
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five x\"}").status).to eq("200")
161
- end
162
-
163
- it "should match if hash matches body in different form" do
164
- expect(http_request(
165
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
166
- body: "{\"a\":\"1\",\"b\":\"five x\",\"c\":{\"d\":[\"e\",\"f\"]}}").status).to eq("200")
167
- end
168
-
169
- it "should match if hash contains date string" do #Crack creates date object
170
- WebMock.reset!
171
- stub_request(:post, "www.example.com").
172
- with(body: {"foo" => "2010-01-01"})
173
- expect(http_request(
174
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
175
- body: "{\"foo\":\"2010-01-01\"}").status).to eq("200")
176
- end
177
-
178
- it "should match if any of the strings have spaces" do
179
- WebMock.reset!
180
- stub_request(:post, "www.example.com").with(body: {"foo" => "a b c"})
181
- expect(http_request(
182
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
183
- body: "{\"foo\":\"a b c\"}").status).to eq("200")
184
- end
185
- end
186
-
187
- describe "for request with xml body and content type is set to xml" do
188
- before(:each) do
189
- WebMock.reset!
190
- stub_request(:post, "www.example.com").
191
- with(body: { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
192
- end
193
-
194
- it "should match if hash matches body" do
195
- expect(http_request(
196
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
197
- 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")
198
- end
199
-
200
- it "should match if hash matches body in different form" do
201
- expect(http_request(
202
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
203
- 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")
204
- end
205
-
206
- it "should match if hash contains date string" do #Crack creates date object
207
- WebMock.reset!
208
- stub_request(:post, "www.example.com").
209
- with(body: {"opt" => {"foo" => "2010-01-01"}})
210
- expect(http_request(
211
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
212
- body: "<opt foo=\"2010-01-01\">\n</opt>\n").status).to eq("200")
213
- end
214
- end
215
- end
216
-
217
- describe "when body is declared as partial hash matcher" do
218
- subject(:request) { http_request( :post, "http://www.example.com/",
219
- body: 'a=1&c[d][]=e&c[d][]=f&b=five') }
220
-
221
- subject(:wrong_request) { http_request(:post, "http://www.example.com/",
222
- body: 'c[d][]=f&a=1&c[d][]=e').status }
223
-
224
- describe "when using 'RSpec:Mocks::ArgumentMatchers#hash_including'" do
225
- before(:each) do
226
- stub_request(:post, "www.example.com").
227
- with(body: hash_including(:a, c: {'d' => ['e', 'f']} ))
228
- end
229
-
230
- describe "for request with url encoded body" do
231
- it "should match request if hash matches body" do
232
- expect(request.status).to eq("200")
233
- end
234
-
235
- it "should not match if hash doesn't match url encoded body" do
236
- expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
237
- end
238
- end
239
-
240
- describe "for request with json body and content type is set to json" do
241
- it "should match if hash matches body" do
242
- expect(http_request(
243
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
244
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
245
- end
246
- end
247
- end
248
-
249
- describe "when using 'WebMock::API#hash_including'" do
250
- before(:each) do
251
- stub_request(:post, "www.example.com").
252
- with(body: WebMock::API.hash_including(:a, c: {'d' => ['e', 'f']} ))
253
- end
254
-
255
- describe "for request with url encoded body" do
256
- it "should match request if hash matches body" do
257
- expect(request.status).to eq("200")
258
- end
259
-
260
- it "should not match if hash doesn't match url encoded body" do
261
- expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
262
- end
263
- end
264
-
265
- describe "for request with json body and content type is set to json" do
266
- it "should match if hash matches body" do
267
- expect(http_request(
268
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
269
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
270
- end
271
- end
272
- end
273
-
274
- end
275
- end
276
-
277
- describe "based on headers" do
278
- it "should match requests if headers are the same" do
279
- stub_request(:get, "www.example.com").with(headers: SAMPLE_HEADERS )
280
- expect(http_request(
281
- :get, "http://www.example.com/",
282
- headers: SAMPLE_HEADERS).status).to eq("200")
283
- end
284
-
285
- it "should match requests if headers are the same and declared as array" do
286
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b"]} )
287
- expect(http_request(
288
- :get, "http://www.example.com/",
289
- headers: {"a" => "b"}).status).to eq("200")
290
- end
291
-
292
- describe "when multiple headers with the same key are used" do
293
- it "should match requests if headers are the same" do
294
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
295
- expect(http_request(
296
- :get, "http://www.example.com/",
297
- headers: {"a" => ["b", "c"]}).status).to eq("200")
298
- end
299
-
300
- it "should match requests if headers are the same but in different order" do
301
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
302
- expect(http_request(
303
- :get, "http://www.example.com/",
304
- headers: {"a" => ["c", "b"]}).status).to eq("200")
305
- end
306
-
307
- it "should not match requests if headers are different" do
308
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]})
309
-
310
- expect {
311
- http_request(
312
- :get, "http://www.example.com/",
313
- headers: {"a" => ["b", "d"]})
314
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
315
- end
316
- end
317
-
318
- it "should match requests if request headers are not stubbed" do
319
- stub_request(:get, "www.example.com")
320
- expect(http_request(
321
- :get, "http://www.example.com/",
322
- headers: SAMPLE_HEADERS).status).to eq("200")
323
- end
324
-
325
- it "should not match requests if headers are different" do
326
- stub_request(:get, "www.example.com").with(headers: SAMPLE_HEADERS)
327
-
328
- expect {
329
- http_request(
330
- :get, "http://www.example.com/",
331
- headers: { 'Content-Length' => '9999'})
332
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
333
- end
334
-
335
- it "should not match if accept header is different" do
336
- stub_request(:get, "www.example.com").
337
- with(headers: { 'Accept' => 'application/json'})
338
- expect {
339
- http_request(
340
- :get, "http://www.example.com/",
341
- headers: { 'Accept' => 'application/xml'})
342
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
343
- end
344
-
345
- describe "declared as regular expressions" do
346
- it "should match requests if header values match regular expression" do
347
- stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
348
- expect(http_request(
349
- :get, "http://www.example.com/",
350
- headers: { 'some-header' => 'MyAppName' }).status).to eq("200")
351
- end
352
-
353
- it "should not match requests if headers values do not match regular expression" do
354
- stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
355
-
356
- expect {
357
- http_request(
358
- :get, "http://www.example.com/",
359
- headers: { 'some-header' => 'xMyAppName' })
360
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
361
- end
362
- end
363
- end
364
-
365
- describe "when stubbing request with userinfo in url", unless: (adapter_info.include?(:no_url_auth)) do
366
- it "should match if credentials are the same" do
367
- stub_request(:get, "user:pass@www.example.com")
368
- expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
369
- end
370
-
371
- it "should not match if credentials are different" do
372
- stub_request(:get, "user:pass@www.example.com")
373
- expect {
374
- expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
375
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
376
- end
377
-
378
- it "should not match if credentials are stubbed but not provided in the request" do
379
- stub_request(:get, "user:pass@www.example.com")
380
- expect {
381
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
382
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
383
- end
384
-
385
- it "should not match if credentials are not stubbed but exist in the request" do
386
- stub_request(:get, "www.example.com")
387
- expect {
388
- expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
389
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
390
- end
391
-
392
- it "should not match if request has credentials provided in basic authentication herader" do
393
- stub_request(:get, "user:pass@www.example.com")
394
- expect {
395
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
396
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
397
- end
398
- end
399
-
400
- describe "when stubbing request with basic authentication header" do
401
- it "should match if credentials are the same" do
402
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
403
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
404
- end
405
-
406
- it "should match if credentials are the same and encode to more than one line" do
407
- stub_request(:get, "www.example.com").with(basic_auth: ['user' * 5, 'pass' * 5])
408
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user' * 5, 'pass' * 5]).status).to eq("200")
409
- end
410
-
411
- it "should match if credentials are the same and were provided directly as authentication headers in request" do
412
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
413
- expect(http_request(:get, "http://www.example.com/", headers: {'Authorization'=>'Basic dXNlcjpwYXNz'}).status).to eq("200")
414
- end
415
-
416
- it "should match if credentials are the same and have been declared in the stub as encoded header" do
417
- stub_request(:get, "www.example.com").with(headers: {'Authorization'=>'Basic dXNlcjpwYXNz'})
418
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
419
- end
420
-
421
- it "should not match if credentials are different" do
422
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
423
- expect {
424
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pazz']).status).to eq("200")
425
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
426
- end
427
-
428
- it "should not match if credentials are stubbed but not provided in the request" do
429
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
430
- expect {
431
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
432
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
433
- end
434
-
435
- it "should match if credentials are not stubbed but exist in the request" do
436
- stub_request(:get, "www.example.com")
437
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
438
- end
439
-
440
- it "should not match if request has credentials provides in userinfo", unless: (adapter_info.include?(:no_url_auth)) do
441
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
442
- expect {
443
- expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
444
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pass@www.example.com/))
445
- end
446
- end
447
-
448
- describe "when stubbing request with a global hook" do
449
- after(:each) do
450
- WebMock::StubRegistry.instance.global_stubs.clear
451
- end
452
-
453
- it 'returns the response returned by the hook' do
454
- WebMock.globally_stub_request do |request|
455
- { body: "global stub body" }
456
- end
457
-
458
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
459
- end
460
-
461
- it 'does not get cleared when a user calls WebMock.reset!' do
462
- WebMock.globally_stub_request do |request|
463
- { body: "global stub body" }
464
- end
465
- WebMock.reset!
466
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
467
- end
468
-
469
- it "does not stub the request if the hook does not return anything" do
470
- WebMock.globally_stub_request { |r| }
471
- expect {
472
- http_request(:get, "http://www.example.com/")
473
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
474
- end
475
-
476
- it "passes the request to the block" do
477
- passed_request = nil
478
- WebMock.globally_stub_request do |request|
479
- passed_request = request
480
- { body: "global stub body" }
481
- end
482
-
483
- http_request(:get, "http://www.example.com:456/bar")
484
- expect(passed_request.uri.to_s).to eq("http://www.example.com:456/bar")
485
- end
486
-
487
- it "should call the block only once per request" do
488
- call_count = 0
489
- WebMock.globally_stub_request do |request|
490
- call_count += 1
491
- { body: "global stub body" }
492
- end
493
- http_request(:get, "http://www.example.com/")
494
- expect(call_count).to eq(1)
495
- end
496
-
497
- it 'supports multiple global stubs; the first registered one that returns a non-nil value determines the stub' do
498
- stub_invocation_order = []
499
- WebMock.globally_stub_request do |request|
500
- stub_invocation_order << :nil_stub
501
- nil
502
- end
503
-
504
- WebMock.globally_stub_request do |request|
505
- stub_invocation_order << :hash_stub
506
- { body: "global stub body" }
507
- end
508
-
509
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
510
- expect(stub_invocation_order).to eq([:nil_stub, :hash_stub])
511
- end
512
-
513
- [:before, :after].each do |before_or_after|
514
- context "when there is also a non-global registered stub #{before_or_after} the global stub" do
515
- def stub_non_globally
516
- stub_request(:get, "www.example.com").to_return(body: 'non-global stub body')
517
- end
518
-
519
- define_method :register_stubs do |block|
520
- stub_non_globally if before_or_after == :before
521
- WebMock.globally_stub_request(&block)
522
- stub_non_globally if before_or_after == :after
523
- end
524
-
525
- it 'uses the response from the global stub if the block returns a non-nil value' do
526
- register_stubs(lambda { |req| { body: 'global stub body' } })
527
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
528
- end
529
-
530
- it 'uses the response from the non-global stub if the block returns a nil value' do
531
- register_stubs(lambda { |req| nil })
532
- expect(http_request(:get, "http://www.example.com/").body).to eq("non-global stub body")
533
- end
534
- end
535
- end
536
- end
537
-
538
- describe "when stubbing request with a block evaluated on request" do
539
- it "should match if block returns true" do
540
- stub_request(:get, "www.example.com").with { |request| true }
541
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
542
- end
543
-
544
- it "should not match if block returns false" do
545
- stub_request(:get, "www.example.com").with { |request| false }
546
- expect {
547
- http_request(:get, "http://www.example.com/")
548
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
549
- end
550
-
551
- it "should pass the request to the block" do
552
- stub_request(:post, "www.example.com").with { |request| request.body == "wadus" }
553
- expect(http_request(
554
- :post, "http://www.example.com/",
555
- body: "wadus").status).to eq("200")
556
- expect {
557
- http_request(:post, "http://www.example.com/", body: "jander")
558
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'))
559
- end
560
-
561
- it "should call the block only once per request" do
562
- call_count = 0
563
- stub_request(:get, "www.example.com").with { |request| call_count += 1; true }
564
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
565
- expect(call_count).to eq(1)
566
- end
567
- end
568
- end
569
-
570
- describe "when request stub was removed" do
571
- it "should raise an error on request" do
572
- stub = stub_request(:get, "www.example.com")
573
-
574
- http_request(:get, "http://www.example.com/")
575
-
576
- remove_request_stub(stub)
577
-
578
- expect {
579
- http_request(:get, "http://www.example.com/")
580
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
581
- end
582
- end
583
- end