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