-
Notifications
You must be signed in to change notification settings - Fork 11
/
webmock_test.rb
341 lines (285 loc) · 12.5 KB
/
webmock_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# frozen_string_literal: true
require "webmock/minitest"
require "httpx/adapters/webmock"
require "test_helper"
require "support/http_helpers"
class WebmockTest < Minitest::Test
include HTTPHelpers
MOCK_URL_HTTP = "http://www.example.com"
MOCK_URL_HTTP_EXCEPTION = "http://exception.example.com"
MOCK_URL_HTTP_TIMEOUT = "http://timeout.example.com"
MOCK_URL_HTTP_TIMEOUT_RETRIES = "http://timeout-x-times.example.com"
def setup
super
WebMock.enable!
WebMock.disable_net_connect!
@stub_http = stub_http_request(:any, MOCK_URL_HTTP)
@exception_class = Class.new(StandardError)
@stub_exception = stub_http_request(:any, MOCK_URL_HTTP_EXCEPTION).to_raise(@exception_class.new("exception message"))
@stub_timeout = stub_http_request(:any, MOCK_URL_HTTP_TIMEOUT).to_timeout
@stub_timeout_retries = stub_http_request(:any, MOCK_URL_HTTP_TIMEOUT_RETRIES).to_timeout.times(2).then.to_return(body: "body")
end
def teardown
super
WebMock.reset!
WebMock.allow_net_connect!
WebMock.disable!
end
def test_assert_requested_with_stub_and_block_raises_error
assert_raises ArgumentError do
assert_requested(@stub_http) {}
end
end
def test_assert_not_requested_with_stub_and_block_raises_error
assert_raises ArgumentError do
assert_not_requested(@stub_http) {}
end
end
def test_to_raise
response = http_request(:get, MOCK_URL_HTTP_EXCEPTION)
assert_requested(@stub_exception)
assert_equal(@exception_class.new("exception message"), response.error)
end
def test_response_not_decoded
request = stub_request(:get, MOCK_URL_HTTP).to_return(body: "body", headers: { content_encoding: "gzip" })
response = HTTPX.get(MOCK_URL_HTTP)
assert_equal("body", response.body.to_s)
assert_requested(request)
end
def test_to_timeout
response = http_request(:get, MOCK_URL_HTTP_TIMEOUT)
assert_requested(@stub_timeout)
assert_equal(HTTPX::TimeoutError.new(1, "Timed out"), response.error)
end
def test_to_timeout_with_retries
response = HTTPX.plugin(:retries, max_retries: 3).get(MOCK_URL_HTTP_TIMEOUT_RETRIES)
assert_requested(@stub_timeout_retries, times: 3)
assert !response.is_a?(HTTPX::ErrorResponse)
assert_equal("body", response.to_s)
end
def test_error_on_non_stubbed_request
assert_raise_with_message(WebMock::NetConnectNotAllowedError, Regexp.new(
"Real HTTP connections are disabled. " \
"Unregistered request: GET http://www.example.net/ with headers"
)) do
http_request(:get, "http://www.example.net/")
end
end
def test_verification_that_expected_request_occured
http_request(:get, "#{MOCK_URL_HTTP}/")
assert_requested(:get, MOCK_URL_HTTP, times: 1)
assert_requested(:get, MOCK_URL_HTTP)
end
def test_verification_that_expected_stub_occured
http_request(:get, "#{MOCK_URL_HTTP}/")
assert_requested(@stub_http, times: 1)
assert_requested(@stub_http)
end
def test_verification_that_expected_request_didnt_occur
expected_message = "The request GET #{MOCK_URL_HTTP}/ was expected to execute 1 time but it executed 0 times" \
"\n\nThe following requests were made:\n\nNo requests were made.\n" \
"============================================================"
assert_raise_with_message(Minitest::Assertion, expected_message) do
assert_requested(:get, MOCK_URL_HTTP)
end
end
def test_verification_that_expected_stub_didnt_occur
expected_message = "The request ANY #{MOCK_URL_HTTP}/ was expected to execute 1 time but it executed 0 times" \
"\n\nThe following requests were made:\n\nNo requests were made.\n" \
"============================================================"
assert_raise_with_message(Minitest::Assertion, expected_message) do
assert_requested(@stub_http)
end
end
def test_verification_that_expected_request_occured_with_body_and_headers
http_request(:get, "#{MOCK_URL_HTTP}/",
body: "abc", headers: { "A" => "a" })
assert_requested(:get, MOCK_URL_HTTP,
body: "abc", headers: { "A" => "a" })
end
def test_verification_that_expected_request_occured_with_query_params
stub_request(:any, MOCK_URL_HTTP).with(query: hash_including("a" => %w[b c]))
http_request(:get, "#{MOCK_URL_HTTP}/?a[]=b&a[]=c&x=1")
assert_requested(:get, MOCK_URL_HTTP,
query: hash_including("a" => %w[b c]))
end
def test_verification_that_requests_with_query_parameters_correctly_called
stub_request(:get, MOCK_URL_HTTP).to_return(body: "1")
stub_request(:get, "#{MOCK_URL_HTTP}/?a[]=b&a[]=c").to_return(body: "2")
stub_request(:get, "#{MOCK_URL_HTTP}/?test=value").to_return(body: "3")
response_1 = http_request(:get, MOCK_URL_HTTP)
response_2 = http_request(:get, MOCK_URL_HTTP, params: { "a" => %w[b c] })
response_3 = http_request(:get, MOCK_URL_HTTP, params: { test: "value" })
assert_equal "1", response_1.body.to_s
assert_equal "2", response_2.body.to_s
assert_equal "3", response_3.body.to_s
assert_requested(:get, MOCK_URL_HTTP)
assert_requested(:get, "#{MOCK_URL_HTTP}/?a[]=b&a[]=c")
assert_requested(:get, "#{MOCK_URL_HTTP}/?test=value")
end
def test_verification_that_expected_request_not_occured_with_query_params
stub_request(:any, MOCK_URL_HTTP).with(query: hash_including(a: %w[b c]))
stub_request(:any, MOCK_URL_HTTP).with(query: hash_excluding(a: %w[b c]))
http_request(:get, "#{MOCK_URL_HTTP}/?a[]=b&a[]=c&x=1")
assert_not_requested(:get, MOCK_URL_HTTP, query: hash_excluding("a" => %w[b c]))
end
def test_verification_that_expected_request_occured_with_excluding_query_params
stub_request(:any, MOCK_URL_HTTP).with(query: hash_excluding("a" => %w[b c]))
http_request(:get, "#{MOCK_URL_HTTP}/?a[]=x&a[]=y&x=1")
assert_requested(:get, MOCK_URL_HTTP, query: hash_excluding("a" => %w[b c]))
end
def test_verification_that_expected_request_with_hash_as_body
stub_request(:post, MOCK_URL_HTTP).with(body: { foo: "bar" })
http_request(:post, MOCK_URL_HTTP, form: { foo: "bar" })
assert_requested(:post, MOCK_URL_HTTP, body: { foo: "bar" })
end
def test_verification_that_expected_request_occured_with_form_file
file = File.new(fixture_file_path)
stub_request(:post, MOCK_URL_HTTP)
http_request(:post, MOCK_URL_HTTP, form: { file: file })
# TODO: webmock does not support matching multipart request body
assert_requested(:post, MOCK_URL_HTTP)
end
def test_verification_that_expected_request_occured_with_form_tempfile
stub_request(:post, MOCK_URL_HTTP)
Tempfile.open("tmp") do |file|
http_request(:post, MOCK_URL_HTTP, form: { file: file })
end
# TODO: webmock does not support matching multipart request body
assert_requested(:post, MOCK_URL_HTTP)
end
def test_verification_that_non_expected_request_didnt_occur
expected_message = Regexp.new(
"The request GET #{MOCK_URL_HTTP}/ was not expected to execute but it executed 1 time\n\n" \
"The following requests were made:\n\nGET #{MOCK_URL_HTTP}/ with headers .+ was made 1 time\n\n" \
"============================================================"
)
assert_raise_with_message(Minitest::Assertion, expected_message) do
http_request(:get, "http://www.example.com/")
assert_not_requested(:get, "http://www.example.com")
end
end
def test_refute_requested_alias
expected_message = Regexp.new(
"The request GET #{MOCK_URL_HTTP}/ was not expected to execute but it executed 1 time\n\n" \
"The following requests were made:\n\nGET #{MOCK_URL_HTTP}/ with headers .+ was made 1 time\n\n" \
"============================================================"
)
assert_raise_with_message(Minitest::Assertion, expected_message) do
http_request(:get, "#{MOCK_URL_HTTP}/")
refute_requested(:get, MOCK_URL_HTTP)
end
end
def test_verification_that_non_expected_stub_didnt_occur
expected_message = Regexp.new(
"The request ANY #{MOCK_URL_HTTP}/ was not expected to execute but it executed 1 time\n\n" \
"The following requests were made:\n\nGET #{MOCK_URL_HTTP}/ with headers .+ was made 1 time\n\n" \
"============================================================"
)
assert_raise_with_message(Minitest::Assertion, expected_message) do
http_request(:get, "#{MOCK_URL_HTTP}/")
assert_not_requested(@stub_http)
end
end
def test_webmock_allows_real_request
WebMock.allow_net_connect!
uri = build_uri("/get?foo=bar")
response = HTTPX.get(uri)
verify_status(response, 200)
verify_body_length(response)
assert_requested(:get, uri, query: { "foo" => "bar" })
end
def test_webmock_allows_real_request_with_body
WebMock.allow_net_connect!
uri = build_uri("/post")
response = HTTPX.post(uri, form: { foo: "bar" })
verify_status(response, 200)
verify_body_length(response)
assert_requested(:post, uri, headers: { "Content-Type" => "application/x-www-form-urlencoded" }, body: "foo=bar")
end
def test_webmock_allows_real_request_with_file_body
WebMock.allow_net_connect!
uri = build_uri("/post")
response = HTTPX.post(uri, form: { image: File.new(fixture_file_path) })
verify_status(response, 200)
verify_body_length(response)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "multipart/form-data")
verify_uploaded_image(body, "image", "image/jpeg")
# TODO: webmock does not support matching multipart request body
# assert_requested(:post, uri, headers: { "Content-Type" => "multipart/form-data" }, form: { "image" => File.new(fixture_file_path) })
end
def test_webmock_mix_mock_and_real_request
WebMock.allow_net_connect!
@stub_http.to_return(status: 200)
# test webmock callback as well
responses = {}
WebMock.after_request do |request_signature, response|
responses[request_signature.uri.to_s] = response
end
# this one ain't stubbed
real_request_uri = build_uri("/get", "http://#{httpbin}")
http_request(:get, "#{MOCK_URL_HTTP}/", real_request_uri)
assert responses.size == 2, "received #{responses.size} instead (#{responses.keys})"
assert(responses.values.all? { |r| r.status.first == 200 })
ensure
WebMock.reset_callbacks
WebMock.disable_net_connect!
end
def test_webmock_disable_after_enable
WebMock.disable!
# WebMock is disabled so this will make a real http request
http_request(:get, "http://#{httpbin}")
# WebMock is disabled so it should not have registered the request
assert_not_requested(:get, "http://#{httpbin}")
end
def test_webmock_follow_redirects_with_stream_plugin_each
session = HTTPX.plugin(:follow_redirects).plugin(:stream)
redirect_url = "#{MOCK_URL_HTTP}/redirect"
initial_request = stub_request(:get, MOCK_URL_HTTP).to_return(status: 302, headers: { location: redirect_url }, body: "redirecting")
redirect_request = stub_request(:get, redirect_url).to_return(status: 200, body: "body")
response = session.get(MOCK_URL_HTTP, stream: true)
body = "".b
response.each do |chunk|
next if (300..399).cover?(response.status)
body << chunk
end
assert_equal("body", body)
assert_requested(initial_request)
assert_requested(redirect_request)
end
def test_webmock_with_stream_plugin_each
session = HTTPX.plugin(:stream)
request = stub_request(:get, MOCK_URL_HTTP).to_return(body: "body")
body = "".b
response = session.get(MOCK_URL_HTTP, stream: true)
response.each do |chunk|
next if (300..399).cover?(response.status)
body << chunk
end
assert_equal("body", body)
assert_requested(request)
end
def test_webmock_with_stream_plugin_each_line
session = HTTPX.plugin(:stream)
request = stub_request(:get, MOCK_URL_HTTP).to_return(body: "First line\nSecond line")
response = session.get(MOCK_URL_HTTP, stream: true)
assert_equal(["First line", "Second line"], response.each_line.to_a)
assert_requested(request)
end
private
def assert_raise_with_message(e, message, &block)
e = assert_raises(e, &block)
if message.is_a?(Regexp)
assert_match(message, e.message)
else
assert_equal(message, e.message)
end
end
def http_request(meth, *uris, **options)
HTTPX.__send__(meth, *uris, **options)
end
def scheme
"http://"
end
end