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,388 +0,0 @@
1
- class MyException < StandardError; end;
2
-
3
- class Responder
4
- def call(request)
5
- {body: request.body}
6
- end
7
- end
8
-
9
- shared_context "declared responses" do |*adapter_info|
10
- describe "when request stub declares that request should raise exception" do
11
- it "should raise exception" do
12
- stub_request(:get, "www.example.com").to_raise(MyException)
13
- expect {
14
- http_request(:get, "http://www.example.com/")
15
- }.to raise_error(MyException, "Exception from WebMock")
16
- end
17
-
18
- it "should raise exception if declared as and exception instance" do
19
- stub_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
20
- expect {
21
- http_request(:get, "http://www.example.com/")
22
- }.to raise_error(MyException, "hello world")
23
- end
24
-
25
- it "should raise exception if declared as an exception instance" do
26
- stub_request(:get, "www.example.com").to_raise("hello world")
27
- expect {
28
- http_request(:get, "http://www.example.com/")
29
- }.to raise_error("hello world")
30
- end
31
-
32
- it "should raise exception after returning declared successful response first" do
33
- stub_request(:get, "www.example.com").to_return(body: "abc").then.to_raise(MyException)
34
- expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
35
- expect {
36
- http_request(:get, "http://www.example.com/")
37
- }.to raise_error(MyException, "Exception from WebMock")
38
- end
39
- end
40
-
41
- describe "when request stub declares that request should timeout" do
42
- it "should timeout" do
43
- stub_request(:get, "www.example.com").to_timeout
44
- expect {
45
- http_request(:get, "http://www.example.com/")
46
- }.to raise_error(client_timeout_exception_class)
47
- end
48
-
49
- it "should timeout after returning declared successful response" do
50
- stub_request(:get, "www.example.com").to_return(body: "abc").then.to_timeout
51
- expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
52
- expect {
53
- http_request(:get, "http://www.example.com/")
54
- }.to raise_error(client_timeout_exception_class)
55
- end
56
- end
57
-
58
- describe "when request stub declares that request should return a response" do
59
- it "should return response with declared body" do
60
- stub_request(:get, "www.example.com").to_return(body: "abc")
61
- expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
62
- end
63
-
64
- it "should return response with declared headers" do
65
- stub_request(:get, "www.example.com").to_return(headers: SAMPLE_HEADERS)
66
- response = http_request(:get, "http://www.example.com/")
67
- expect(response.headers["Content-Length"]).to eq("8888")
68
- end
69
-
70
- it "should return response with declared headers even if there are multiple headers with the same key" do
71
- stub_request(:get, "www.example.com").to_return(headers: {"a" => ["b", "c"]})
72
- response = http_request(:get, "http://www.example.com/")
73
- expect(response.headers["A"]).to eq("b, c")
74
- end
75
-
76
- it "should return response with declared status code" do
77
- stub_request(:get, "www.example.com").to_return(status: 500)
78
- expect(http_request(:get, "http://www.example.com/").status).to eq("500")
79
- end
80
-
81
- it "should return response with declared status message", unless: (adapter_info.include?(:no_status_message)) do
82
- stub_request(:get, "www.example.com").to_return(status: [500, "Internal Server Error"])
83
- response = http_request(:get, "http://www.example.com/")
84
- expect(response.message).to eq("Internal Server Error")
85
- end
86
-
87
- it "should return response with a default status code" do
88
- stub_request(:get, "www.example.com")
89
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
90
- end
91
-
92
- it "should return default response with empty message if response was not declared", unless: (adapter_info.include?(:no_status_message)) do
93
- stub_request(:get, "www.example.com")
94
- response = http_request(:get, "http://www.example.com/")
95
- expect(response.message).to eq("")
96
- end
97
-
98
- describe "when response body was declared as IO" do
99
- it "should return response body" do
100
- stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
101
- expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
102
- end
103
-
104
- it "should return response body if requested many times" do
105
- stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
106
- 2.times do
107
- expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
108
- end
109
- end
110
-
111
- it "should close IO after request" do
112
- stub_request(:get, "www.example.com").to_return(body: @file = File.new(__FILE__))
113
- expect(@file).to be_closed
114
- end
115
- end
116
-
117
- describe "when response parts were declared as lambdas" do
118
- it "should return evaluated response body" do
119
- stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
120
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
121
- end
122
-
123
- it "should return evaluated response headers" do
124
- stub_request(:post, "www.example.com").to_return(headers: lambda { |request| request.headers })
125
- expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'B'}).headers['A']).to eq('B')
126
- expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'C'}).headers['A']).to eq('C')
127
- end
128
-
129
- it "should evaluate response body for each request" do
130
- stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
131
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
132
- expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
133
- end
134
- end
135
-
136
- describe "when response was declared as lambda" do
137
- it "should return evaluated response body" do
138
- stub_request(:post, "www.example.com").to_return(lambda {|request|
139
- {body: request.body}
140
- })
141
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
142
- expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
143
- end
144
-
145
- it "should return evaluated response headers" do
146
- stub_request(:get, "www.example.com").to_return(lambda { |request|
147
- {headers: request.headers}
148
- })
149
- expect(http_request(:get, "http://www.example.com/", headers: {'A' => 'B'}).headers['A']).to eq('B')
150
- end
151
-
152
- it "should return dynamic response declared as a block" do
153
- stub_request(:post, "www.example.com").to_return do |request|
154
- {body: request.body}
155
- end
156
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
157
- end
158
-
159
- it "should return dynamic response declared as an object responding to call" do
160
- stub_request(:post, "www.example.com").to_return(Responder.new)
161
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
162
- end
163
- end
164
-
165
-
166
- describe "when response was declared as a file with a raw response" do
167
- before(:each) do
168
- @file = File.new(CURL_EXAMPLE_OUTPUT_PATH)
169
- stub_request(:get, "www.example.com").to_return(@file)
170
- @response = http_request(:get, "http://www.example.com/")
171
- end
172
-
173
- it "should return recorded headers" do
174
- expect(@response.headers).to eq({
175
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
176
- "Content-Type"=>"text/html; charset=UTF-8",
177
- "Content-Length"=>"419",
178
- "Connection"=>"Keep-Alive",
179
- "Accept"=>"image/jpeg, image/png"
180
- })
181
- end
182
-
183
- it "should return recorded body" do
184
- expect(@response.body.size).to eq(419)
185
- end
186
-
187
- it "should return recorded status" do
188
- expect(@response.status).to eq("202")
189
- end
190
-
191
- it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
192
- expect(@response.message).to eq("OK")
193
- end
194
-
195
- it "should ensure file is closed" do
196
- expect(@file).to be_closed
197
- end
198
- end
199
-
200
- describe "when response was declared as a string with a raw response" do
201
- before(:each) do
202
- @input = File.read(CURL_EXAMPLE_OUTPUT_PATH)
203
- stub_request(:get, "www.example.com").to_return(@input)
204
- @response = http_request(:get, "http://www.example.com/")
205
- end
206
-
207
- it "should return recorded headers" do
208
- expect(@response.headers).to eq({
209
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
210
- "Content-Type"=>"text/html; charset=UTF-8",
211
- "Content-Length"=>"419",
212
- "Connection"=>"Keep-Alive",
213
- "Accept"=>"image/jpeg, image/png"
214
- })
215
- end
216
-
217
- it "should return recorded body" do
218
- expect(@response.body.size).to eq(419)
219
- end
220
-
221
- it "should return recorded status" do
222
- expect(@response.status).to eq("202")
223
- end
224
-
225
- it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
226
- expect(@response.message).to eq("OK")
227
- end
228
- end
229
-
230
- describe "when response was declared as lambda evaluating to a string with a raw response" do
231
- before(:each) do
232
- @files = {
233
- "www.example.com" => File.new(CURL_EXAMPLE_OUTPUT_PATH)
234
- }
235
- end
236
-
237
- it "should return response from evaluated file" do
238
- stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
239
- expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
240
- end
241
-
242
- it "should return response from evaluated string" do
243
- stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
244
- expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
245
- end
246
- end
247
-
248
- describe "when response is declared as an Rack app" do
249
- it "should return response returned by the rack app" do
250
- stub_request(:any, "http://www.example.com/greet").to_rack(MyRackApp)
251
- expect(http_request(:post, 'http://www.example.com/greet', body: 'name=Jimmy').body).to eq('Good to meet you, Jimmy!')
252
- end
253
-
254
- it "should pass along the port number to the rack app" do
255
- stub_request(:get, "http://www.example.com/compute").to_rack(MyRackApp)
256
- expect(http_request(:get, "http://www.example.com/compute").status).to eq("200")
257
- end
258
-
259
- it "preserves content-type header when proxying to a rack app" do
260
- stub_request(:any, //).to_rack(lambda {|req| [200, {}, ["OK"]] })
261
-
262
- url = "https://google.com/hi/there"
263
- headers = {
264
- "Accept" => "application/json",
265
- "Content-Type" => "application/json"
266
- }
267
-
268
- http_request(:get, url, headers: headers)
269
- expect(WebMock).to have_requested(:get, url).with(headers: headers)
270
- end
271
- end
272
-
273
- describe "when sequences of responses are declared" do
274
- it "should return responses one by one if declared in array" do
275
- stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"}, {body: "3"} ])
276
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
277
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
278
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
279
- end
280
-
281
- it "should repeat returning last declared response from a sequence after all responses were returned" do
282
- stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"} ])
283
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
284
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
285
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
286
- end
287
-
288
- it "should return responses one by one if declared as comma separated params" do
289
- stub_request(:get, "www.example.com").to_return({body: "1"}, {body: "2"}, {body: "3"})
290
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
291
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
292
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
293
- end
294
-
295
- it "should return responses one by one if declared with several to_return invokations" do
296
- stub_request(:get, "www.example.com").
297
- to_return({body: "1"}).
298
- to_return({body: "2"}).
299
- to_return({body: "3"})
300
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
301
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
302
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
303
- end
304
-
305
- it "should return responses one by one if declared with to_return invocations separated with then syntactic sugar" do
306
- stub_request(:get, "www.example.com").to_return({body: "1"}).then.
307
- to_return({body: "2"}).then.to_return({body: "3"})
308
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
309
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
310
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
311
- end
312
-
313
- end
314
-
315
- describe "when responses are declared to return more than once" do
316
- it "should repeat one response declared number of times" do
317
- stub_request(:get, "www.example.com").
318
- to_return({body: "1"}).times(2).
319
- to_return({body: "2"})
320
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
321
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
322
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
323
- end
324
-
325
-
326
- it "should repeat sequence of response declared number of times" do
327
- stub_request(:get, "www.example.com").
328
- to_return({body: "1"}, {body: "2"}).times(2).
329
- to_return({body: "3"})
330
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
331
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
332
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
333
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
334
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
335
- end
336
-
337
-
338
- it "should repeat infinitely last response even if number of declared times is lower" do
339
- stub_request(:get, "www.example.com").
340
- to_return({body: "1"}).times(2)
341
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
342
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
343
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
344
- end
345
-
346
- it "should give error if times is declared without specifying response" do
347
- expect {
348
- stub_request(:get, "www.example.com").times(3)
349
- }.to raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
350
- end
351
-
352
- end
353
-
354
- describe "when exception is declared to be raised more than once" do
355
- it "should repeat raising exception declared number of times" do
356
- stub_request(:get, "www.example.com").
357
- to_raise(MyException).times(2).
358
- to_return({body: "2"})
359
- expect {
360
- http_request(:get, "http://www.example.com/")
361
- }.to raise_error(MyException, "Exception from WebMock")
362
- expect {
363
- http_request(:get, "http://www.example.com/")
364
- }.to raise_error(MyException, "Exception from WebMock")
365
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
366
- end
367
-
368
- it "should repeat raising sequence of exceptions declared number of times" do
369
- stub_request(:get, "www.example.com").
370
- to_raise(MyException, ArgumentError).times(2).
371
- to_return({body: "2"})
372
- expect {
373
- http_request(:get, "http://www.example.com/")
374
- }.to raise_error(MyException, "Exception from WebMock")
375
- expect {
376
- http_request(:get, "http://www.example.com/")
377
- }.to raise_error(ArgumentError)
378
- expect {
379
- http_request(:get, "http://www.example.com/")
380
- }.to raise_error(MyException, "Exception from WebMock")
381
- expect {
382
- http_request(:get, "http://www.example.com/")
383
- }.to raise_error(ArgumentError)
384
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
385
- end
386
- end
387
- end
388
- end