webmock 3.18.1 → 3.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -0
  3. data/README.md +34 -15
  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 +2 -0
  12. data/lib/webmock/http_lib_adapters/curb_adapter.rb +2 -0
  13. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +14 -6
  14. data/lib/webmock/http_lib_adapters/excon_adapter.rb +2 -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 +2 -0
  18. data/lib/webmock/http_lib_adapters/http_rb/request.rb +3 -1
  19. data/lib/webmock/http_lib_adapters/http_rb/response.rb +2 -0
  20. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +2 -0
  21. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +2 -0
  22. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +2 -0
  23. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +2 -0
  24. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +2 -0
  25. data/lib/webmock/http_lib_adapters/net_http.rb +2 -0
  26. data/lib/webmock/http_lib_adapters/net_http_response.rb +2 -0
  27. data/lib/webmock/http_lib_adapters/patron_adapter.rb +2 -0
  28. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +2 -0
  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 +2 -0
  35. data/lib/webmock/request_body_diff.rb +2 -0
  36. data/lib/webmock/request_execution_verifier.rb +2 -0
  37. data/lib/webmock/request_pattern.rb +3 -1
  38. data/lib/webmock/request_registry.rb +2 -0
  39. data/lib/webmock/request_signature.rb +2 -0
  40. data/lib/webmock/request_signature_snippet.rb +2 -0
  41. data/lib/webmock/request_stub.rb +7 -1
  42. data/lib/webmock/response.rb +2 -0
  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 +2 -0
  48. data/lib/webmock/stub_registry.rb +2 -0
  49. data/lib/webmock/stub_request_snippet.rb +2 -0
  50. data/lib/webmock/test_unit.rb +2 -0
  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 +22 -8
  55. data/lib/webmock/util/json.rb +2 -0
  56. data/lib/webmock/util/query_mapper.rb +2 -0
  57. data/lib/webmock/util/uri.rb +3 -1
  58. data/lib/webmock/util/values_stringifier.rb +2 -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 +2 -0
  62. data/lib/webmock.rb +2 -0
  63. metadata +36 -168
  64. data/.gemtest +0 -0
  65. data/.github/workflows/CI.yml +0 -38
  66. data/.gitignore +0 -34
  67. data/.rspec-tm +0 -2
  68. data/Gemfile +0 -9
  69. data/Rakefile +0 -38
  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/async_http_client/async_http_client_spec.rb +0 -375
  74. data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +0 -73
  75. data/spec/acceptance/curb/curb_spec.rb +0 -510
  76. data/spec/acceptance/curb/curb_spec_helper.rb +0 -147
  77. data/spec/acceptance/em_http_request/em_http_request_spec.rb +0 -462
  78. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +0 -77
  79. data/spec/acceptance/excon/excon_spec.rb +0 -77
  80. data/spec/acceptance/excon/excon_spec_helper.rb +0 -52
  81. data/spec/acceptance/http_rb/http_rb_spec.rb +0 -93
  82. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +0 -54
  83. data/spec/acceptance/httpclient/httpclient_spec.rb +0 -217
  84. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +0 -57
  85. data/spec/acceptance/manticore/manticore_spec.rb +0 -107
  86. data/spec/acceptance/manticore/manticore_spec_helper.rb +0 -35
  87. data/spec/acceptance/net_http/net_http_shared.rb +0 -190
  88. data/spec/acceptance/net_http/net_http_spec.rb +0 -395
  89. data/spec/acceptance/net_http/net_http_spec_helper.rb +0 -64
  90. data/spec/acceptance/net_http/real_net_http_spec.rb +0 -20
  91. data/spec/acceptance/patron/patron_spec.rb +0 -123
  92. data/spec/acceptance/patron/patron_spec_helper.rb +0 -54
  93. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +0 -313
  94. data/spec/acceptance/shared/callbacks.rb +0 -148
  95. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +0 -36
  96. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +0 -95
  97. data/spec/acceptance/shared/precedence_of_stubs.rb +0 -15
  98. data/spec/acceptance/shared/request_expectations.rb +0 -930
  99. data/spec/acceptance/shared/returning_declared_responses.rb +0 -409
  100. data/spec/acceptance/shared/stubbing_requests.rb +0 -678
  101. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +0 -135
  102. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +0 -60
  103. data/spec/acceptance/webmock_shared.rb +0 -41
  104. data/spec/fixtures/test.txt +0 -1
  105. data/spec/quality_spec.rb +0 -84
  106. data/spec/spec_helper.rb +0 -48
  107. data/spec/support/example_curl_output.txt +0 -22
  108. data/spec/support/failures.rb +0 -9
  109. data/spec/support/my_rack_app.rb +0 -53
  110. data/spec/support/network_connection.rb +0 -19
  111. data/spec/support/webmock_server.rb +0 -70
  112. data/spec/unit/api_spec.rb +0 -175
  113. data/spec/unit/errors_spec.rb +0 -129
  114. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +0 -17
  115. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +0 -12
  116. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +0 -61
  117. data/spec/unit/matchers/hash_including_matcher_spec.rb +0 -87
  118. data/spec/unit/rack_response_spec.rb +0 -112
  119. data/spec/unit/request_body_diff_spec.rb +0 -90
  120. data/spec/unit/request_execution_verifier_spec.rb +0 -208
  121. data/spec/unit/request_pattern_spec.rb +0 -748
  122. data/spec/unit/request_registry_spec.rb +0 -95
  123. data/spec/unit/request_signature_snippet_spec.rb +0 -89
  124. data/spec/unit/request_signature_spec.rb +0 -175
  125. data/spec/unit/request_stub_spec.rb +0 -234
  126. data/spec/unit/response_spec.rb +0 -314
  127. data/spec/unit/stub_registry_spec.rb +0 -103
  128. data/spec/unit/stub_request_snippet_spec.rb +0 -115
  129. data/spec/unit/util/hash_counter_spec.rb +0 -39
  130. data/spec/unit/util/hash_keys_stringifier_spec.rb +0 -27
  131. data/spec/unit/util/headers_spec.rb +0 -28
  132. data/spec/unit/util/json_spec.rb +0 -33
  133. data/spec/unit/util/query_mapper_spec.rb +0 -157
  134. data/spec/unit/util/uri_spec.rb +0 -371
  135. data/spec/unit/util/version_checker_spec.rb +0 -65
  136. data/spec/unit/webmock_spec.rb +0 -114
  137. data/test/http_request.rb +0 -24
  138. data/test/shared_test.rb +0 -108
  139. data/test/test_helper.rb +0 -23
  140. data/test/test_webmock.rb +0 -12
  141. data/webmock.gemspec +0 -54
@@ -1,409 +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["Accept"]).to eq("application/json")
68
- unless adapter_info.include?(:no_content_length_header)
69
- expect(response.headers["Content-Length"]).to eq("8888")
70
- end
71
- end
72
-
73
- it "should return response with declared headers even if there are multiple headers with the same key" do
74
- stub_request(:get, "www.example.com").to_return(headers: {"a" => ["b", "c"]})
75
- response = http_request(:get, "http://www.example.com/")
76
- expect(response.headers["A"]).to eq("b, c")
77
- end
78
-
79
- it "should return response with declared status code" do
80
- stub_request(:get, "www.example.com").to_return(status: 500)
81
- expect(http_request(:get, "http://www.example.com/").status).to eq("500")
82
- end
83
-
84
- it "should return response with declared status message", unless: (adapter_info.include?(:no_status_message)) do
85
- stub_request(:get, "www.example.com").to_return(status: [500, "Internal Server Error"])
86
- response = http_request(:get, "http://www.example.com/")
87
- expect(response.message).to eq("Internal Server Error")
88
- end
89
-
90
- it "should return response with a default status code" do
91
- stub_request(:get, "www.example.com")
92
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
93
- end
94
-
95
- it "should return default response with empty message if response was not declared", unless: (adapter_info.include?(:no_status_message)) do
96
- stub_request(:get, "www.example.com")
97
- response = http_request(:get, "http://www.example.com/")
98
- expect(response.message).to eq("")
99
- end
100
-
101
- describe "when response body was declared as IO" do
102
- it "should return response body" do
103
- stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
104
- expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
105
- end
106
-
107
- it "should return response body if requested many times" do
108
- stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
109
- 2.times do
110
- expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
111
- end
112
- end
113
-
114
- it "should close IO after request" do
115
- stub_request(:get, "www.example.com").to_return(body: @file = File.new(__FILE__))
116
- expect(@file).to be_closed
117
- end
118
- end
119
-
120
- describe "when response parts were declared as lambdas" do
121
- it "should return evaluated response body" do
122
- stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
123
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
124
- end
125
-
126
- it "should return evaluated response headers" do
127
- stub_request(:post, "www.example.com").to_return(headers: lambda { |request| request.headers })
128
- expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'B'}).headers['A']).to eq('B')
129
- expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'C'}).headers['A']).to eq('C')
130
- end
131
-
132
- it "should evaluate response body for each request" do
133
- stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
134
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
135
- expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
136
- end
137
- end
138
-
139
- describe "when response was declared as lambda" do
140
- it "should return evaluated response body" do
141
- stub_request(:post, "www.example.com").to_return(lambda {|request|
142
- {body: request.body}
143
- })
144
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
145
- expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
146
- end
147
-
148
- it "should return evaluated response headers" do
149
- stub_request(:get, "www.example.com").to_return(lambda { |request|
150
- {headers: request.headers}
151
- })
152
- expect(http_request(:get, "http://www.example.com/", headers: {'A' => 'B'}).headers['A']).to eq('B')
153
- end
154
-
155
- it "should return dynamic response declared as a block" do
156
- stub_request(:post, "www.example.com").to_return do |request|
157
- {body: request.body}
158
- end
159
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
160
- end
161
-
162
- it "should return dynamic response declared as an object responding to call" do
163
- stub_request(:post, "www.example.com").to_return(Responder.new)
164
- expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
165
- end
166
- end
167
-
168
-
169
- describe "when response was declared as a file with a raw response" do
170
- before(:each) do
171
- @file = File.new(CURL_EXAMPLE_OUTPUT_PATH)
172
- stub_request(:get, "www.example.com").to_return(@file)
173
- @response = http_request(:get, "http://www.example.com/")
174
- end
175
-
176
- it "should return recorded headers" do
177
- if adapter_info.include?(:no_content_length_header)
178
- expect(@response.headers).to eq({
179
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
180
- "Content-Type"=>"text/html; charset=UTF-8",
181
- "Connection"=>"Keep-Alive",
182
- "Accept"=>"image/jpeg, image/png"
183
- })
184
- else
185
- expect(@response.headers).to eq({
186
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
187
- "Content-Type"=>"text/html; charset=UTF-8",
188
- "Content-Length"=>"419",
189
- "Connection"=>"Keep-Alive",
190
- "Accept"=>"image/jpeg, image/png"
191
- })
192
- end
193
- end
194
-
195
- it "should return recorded body" do
196
- expect(@response.body.size).to eq(419)
197
- end
198
-
199
- it "should return recorded status" do
200
- expect(@response.status).to eq("202")
201
- end
202
-
203
- it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
204
- expect(@response.message).to eq("OK")
205
- end
206
-
207
- it "should ensure file is closed" do
208
- expect(@file).to be_closed
209
- end
210
- end
211
-
212
- describe "when response was declared as a string with a raw response" do
213
- before(:each) do
214
- @input = File.read(CURL_EXAMPLE_OUTPUT_PATH)
215
- stub_request(:get, "www.example.com").to_return(@input)
216
- @response = http_request(:get, "http://www.example.com/")
217
- end
218
-
219
- it "should return recorded headers" do
220
- if adapter_info.include?(:no_content_length_header)
221
- expect(@response.headers).to eq({
222
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
223
- "Content-Type"=>"text/html; charset=UTF-8",
224
- "Connection"=>"Keep-Alive",
225
- "Accept"=>"image/jpeg, image/png"
226
- })
227
- else
228
- expect(@response.headers).to eq({
229
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
230
- "Content-Type"=>"text/html; charset=UTF-8",
231
- "Content-Length"=>"419",
232
- "Connection"=>"Keep-Alive",
233
- "Accept"=>"image/jpeg, image/png"
234
- })
235
- end
236
- end
237
-
238
- it "should return recorded body" do
239
- expect(@response.body.size).to eq(419)
240
- end
241
-
242
- it "should return recorded status" do
243
- expect(@response.status).to eq("202")
244
- end
245
-
246
- it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
247
- expect(@response.message).to eq("OK")
248
- end
249
- end
250
-
251
- describe "when response was declared as lambda evaluating to a string with a raw response" do
252
- before(:each) do
253
- @files = {
254
- "www.example.com" => File.new(CURL_EXAMPLE_OUTPUT_PATH)
255
- }
256
- end
257
-
258
- it "should return response from evaluated file" do
259
- stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
260
- expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
261
- end
262
-
263
- it "should return response from evaluated string" do
264
- stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
265
- expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
266
- end
267
- end
268
-
269
- describe "when response is declared as an Rack app" do
270
- it "should return response returned by the rack app" do
271
- stub_request(:any, "http://www.example.com/greet").to_rack(MyRackApp)
272
- expect(http_request(:post, 'http://www.example.com/greet', body: 'name=Jimmy').body).to eq('Good to meet you, Jimmy!')
273
- end
274
-
275
- it "should pass along the port number to the rack app" do
276
- stub_request(:get, "http://www.example.com/compute").to_rack(MyRackApp)
277
- expect(http_request(:get, "http://www.example.com/compute").status).to eq("200")
278
- end
279
-
280
- it "preserves content-type header when proxying to a rack app" do
281
- stub_request(:any, //).to_rack(lambda {|req| [200, {}, ["OK"]] })
282
-
283
- url = "https://google.com/hi/there"
284
- headers = {
285
- "Accept" => "application/json",
286
- "Content-Type" => "application/json"
287
- }
288
-
289
- http_request(:get, url, headers: headers)
290
- expect(WebMock).to have_requested(:get, url).with(headers: headers)
291
- end
292
- end
293
-
294
- describe "when sequences of responses are declared" do
295
- it "should return responses one by one if declared in array" do
296
- stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"}, {body: "3"} ])
297
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
298
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
299
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
300
- end
301
-
302
- it "should repeat returning last declared response from a sequence after all responses were returned" do
303
- stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"} ])
304
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
305
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
306
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
307
- end
308
-
309
- it "should return responses one by one if declared as comma separated params" do
310
- stub_request(:get, "www.example.com").to_return({body: "1"}, {body: "2"}, {body: "3"})
311
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
312
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
313
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
314
- end
315
-
316
- it "should return responses one by one if declared with several to_return invokations" do
317
- stub_request(:get, "www.example.com").
318
- to_return({body: "1"}).
319
- to_return({body: "2"}).
320
- to_return({body: "3"})
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
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
324
- end
325
-
326
- it "should return responses one by one if declared with to_return invocations separated with then syntactic sugar" do
327
- stub_request(:get, "www.example.com").to_return({body: "1"}).then.
328
- to_return({body: "2"}).then.to_return({body: "3"})
329
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
330
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
331
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
332
- end
333
-
334
- end
335
-
336
- describe "when responses are declared to return more than once" do
337
- it "should repeat one response declared number of times" do
338
- stub_request(:get, "www.example.com").
339
- to_return({body: "1"}).times(2).
340
- to_return({body: "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("2")
344
- end
345
-
346
-
347
- it "should repeat sequence of response declared number of times" do
348
- stub_request(:get, "www.example.com").
349
- to_return({body: "1"}, {body: "2"}).times(2).
350
- to_return({body: "3"})
351
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
352
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
353
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
354
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
355
- expect(http_request(:get, "http://www.example.com/").body).to eq("3")
356
- end
357
-
358
-
359
- it "should repeat infinitely last response even if number of declared times is lower" do
360
- stub_request(:get, "www.example.com").
361
- to_return({body: "1"}).times(2)
362
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
363
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
364
- expect(http_request(:get, "http://www.example.com/").body).to eq("1")
365
- end
366
-
367
- it "should give error if times is declared without specifying response" do
368
- expect {
369
- stub_request(:get, "www.example.com").times(3)
370
- }.to raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
371
- end
372
-
373
- end
374
-
375
- describe "when exception is declared to be raised more than once" do
376
- it "should repeat raising exception declared number of times" do
377
- stub_request(:get, "www.example.com").
378
- to_raise(MyException).times(2).
379
- to_return({body: "2"})
380
- expect {
381
- http_request(:get, "http://www.example.com/")
382
- }.to raise_error(MyException, "Exception from WebMock")
383
- expect {
384
- http_request(:get, "http://www.example.com/")
385
- }.to raise_error(MyException, "Exception from WebMock")
386
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
387
- end
388
-
389
- it "should repeat raising sequence of exceptions declared number of times" do
390
- stub_request(:get, "www.example.com").
391
- to_raise(MyException, ArgumentError).times(2).
392
- to_return({body: "2"})
393
- expect {
394
- http_request(:get, "http://www.example.com/")
395
- }.to raise_error(MyException, "Exception from WebMock")
396
- expect {
397
- http_request(:get, "http://www.example.com/")
398
- }.to raise_error(ArgumentError)
399
- expect {
400
- http_request(:get, "http://www.example.com/")
401
- }.to raise_error(MyException, "Exception from WebMock")
402
- expect {
403
- http_request(:get, "http://www.example.com/")
404
- }.to raise_error(ArgumentError)
405
- expect(http_request(:get, "http://www.example.com/").body).to eq("2")
406
- end
407
- end
408
- end
409
- end