webmock 3.11.1 → 3.18.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/CI.yml +38 -0
- data/CHANGELOG.md +126 -2
- data/Gemfile +1 -1
- data/README.md +33 -16
- data/Rakefile +12 -2
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +9 -2
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +2 -2
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +6 -3
- data/lib/webmock/http_lib_adapters/http_rb/client.rb +1 -3
- data/lib/webmock/http_lib_adapters/http_rb/response.rb +17 -3
- data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +4 -2
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +6 -2
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +8 -1
- data/lib/webmock/http_lib_adapters/net_http.rb +29 -115
- data/lib/webmock/request_pattern.rb +30 -8
- data/lib/webmock/request_signature.rb +2 -2
- data/lib/webmock/request_stub.rb +15 -0
- data/lib/webmock/response.rb +8 -8
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +10 -0
- data/minitest/webmock_spec.rb +1 -1
- data/spec/acceptance/async_http_client/async_http_client_spec.rb +27 -5
- data/spec/acceptance/curb/curb_spec.rb +11 -0
- data/spec/acceptance/em_http_request/em_http_request_spec.rb +57 -1
- data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +1 -1
- data/spec/acceptance/excon/excon_spec.rb +2 -2
- data/spec/acceptance/manticore/manticore_spec.rb +32 -0
- data/spec/acceptance/net_http/net_http_shared.rb +46 -9
- data/spec/acceptance/net_http/net_http_spec.rb +75 -23
- data/spec/acceptance/net_http/real_net_http_spec.rb +1 -1
- data/spec/acceptance/patron/patron_spec.rb +19 -21
- data/spec/acceptance/patron/patron_spec_helper.rb +2 -2
- data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +14 -14
- data/spec/acceptance/shared/callbacks.rb +2 -2
- data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +1 -1
- data/spec/unit/request_pattern_spec.rb +82 -46
- data/spec/unit/request_signature_spec.rb +21 -1
- data/spec/unit/request_stub_spec.rb +35 -0
- data/spec/unit/response_spec.rb +29 -1
- data/spec/unit/webmock_spec.rb +54 -0
- data/webmock.gemspec +6 -5
- metadata +46 -32
- data/.travis.yml +0 -24
@@ -10,24 +10,19 @@ module WebMock
|
|
10
10
|
adapter_for :net_http
|
11
11
|
|
12
12
|
OriginalNetHTTP = Net::HTTP unless const_defined?(:OriginalNetHTTP)
|
13
|
-
OriginalNetBufferedIO = Net::BufferedIO unless const_defined?(:OriginalNetBufferedIO)
|
14
13
|
|
15
14
|
def self.enable!
|
16
|
-
Net.send(:remove_const, :BufferedIO)
|
17
15
|
Net.send(:remove_const, :HTTP)
|
18
16
|
Net.send(:remove_const, :HTTPSession)
|
19
17
|
Net.send(:const_set, :HTTP, @webMockNetHTTP)
|
20
18
|
Net.send(:const_set, :HTTPSession, @webMockNetHTTP)
|
21
|
-
Net.send(:const_set, :BufferedIO, Net::WebMockNetBufferedIO)
|
22
19
|
end
|
23
20
|
|
24
21
|
def self.disable!
|
25
|
-
Net.send(:remove_const, :BufferedIO)
|
26
22
|
Net.send(:remove_const, :HTTP)
|
27
23
|
Net.send(:remove_const, :HTTPSession)
|
28
24
|
Net.send(:const_set, :HTTP, OriginalNetHTTP)
|
29
25
|
Net.send(:const_set, :HTTPSession, OriginalNetHTTP)
|
30
|
-
Net.send(:const_set, :BufferedIO, OriginalNetBufferedIO)
|
31
26
|
|
32
27
|
#copy all constants from @webMockNetHTTP to original Net::HTTP
|
33
28
|
#in case any constants were added to @webMockNetHTTP instead of Net::HTTP
|
@@ -98,13 +93,8 @@ module WebMock
|
|
98
93
|
after_request.call(response)
|
99
94
|
}
|
100
95
|
if started?
|
101
|
-
|
102
|
-
|
103
|
-
else
|
104
|
-
start_with_connect_without_finish {
|
105
|
-
super_with_after_request.call
|
106
|
-
}
|
107
|
-
end
|
96
|
+
ensure_actual_connection
|
97
|
+
super_with_after_request.call
|
108
98
|
else
|
109
99
|
start_with_connect {
|
110
100
|
super_with_after_request.call
|
@@ -119,32 +109,33 @@ module WebMock
|
|
119
109
|
raise IOError, 'HTTP session already opened' if @started
|
120
110
|
if block_given?
|
121
111
|
begin
|
112
|
+
@socket = Net::HTTP.socket_type.new
|
122
113
|
@started = true
|
123
114
|
return yield(self)
|
124
115
|
ensure
|
125
116
|
do_finish
|
126
117
|
end
|
127
118
|
end
|
119
|
+
@socket = Net::HTTP.socket_type.new
|
128
120
|
@started = true
|
129
121
|
self
|
130
122
|
end
|
131
123
|
|
132
124
|
|
133
|
-
def
|
134
|
-
if
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
end
|
125
|
+
def ensure_actual_connection
|
126
|
+
if @socket.is_a?(StubSocket)
|
127
|
+
@socket&.close
|
128
|
+
@socket = nil
|
129
|
+
do_start
|
139
130
|
end
|
140
|
-
do_start
|
141
|
-
self
|
142
131
|
end
|
143
132
|
|
144
133
|
alias_method :start_with_connect, :start
|
145
134
|
|
146
135
|
def start(&block)
|
147
|
-
|
136
|
+
uri = Addressable::URI.parse(WebMock::NetHTTPUtility.get_uri(self))
|
137
|
+
|
138
|
+
if WebMock.net_http_connect_on_start?(uri)
|
148
139
|
super(&block)
|
149
140
|
else
|
150
141
|
start_without_connect(&block)
|
@@ -169,7 +160,7 @@ module WebMock
|
|
169
160
|
response.extend Net::WebMockHTTPResponse
|
170
161
|
|
171
162
|
if webmock_response.should_timeout
|
172
|
-
raise
|
163
|
+
raise Net::OpenTimeout, "execution expired"
|
173
164
|
end
|
174
165
|
|
175
166
|
webmock_response.raise_error_if_any
|
@@ -179,16 +170,6 @@ module WebMock
|
|
179
170
|
response
|
180
171
|
end
|
181
172
|
|
182
|
-
def timeout_exception
|
183
|
-
if defined?(Net::OpenTimeout)
|
184
|
-
# Ruby 2.x
|
185
|
-
Net::OpenTimeout
|
186
|
-
else
|
187
|
-
# Fallback, if things change
|
188
|
-
Timeout::Error
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
173
|
def build_webmock_response(net_http_response)
|
193
174
|
webmock_response = WebMock::Response.new
|
194
175
|
webmock_response.status = [
|
@@ -222,31 +203,21 @@ module WebMock
|
|
222
203
|
end
|
223
204
|
end
|
224
205
|
|
225
|
-
# patch for StringIO behavior in Ruby 2.2.3
|
226
|
-
# https://github.com/bblimke/webmock/issues/558
|
227
|
-
class PatchedStringIO < StringIO #:nodoc:
|
228
|
-
|
229
|
-
alias_method :orig_read_nonblock, :read_nonblock
|
230
|
-
|
231
|
-
def read_nonblock(size, *args, **kwargs)
|
232
|
-
args.reject! {|arg| !arg.is_a?(Hash)}
|
233
|
-
orig_read_nonblock(size, *args, **kwargs)
|
234
|
-
end
|
235
|
-
|
236
|
-
end
|
237
|
-
|
238
206
|
class StubSocket #:nodoc:
|
239
207
|
|
240
208
|
attr_accessor :read_timeout, :continue_timeout, :write_timeout
|
241
209
|
|
242
210
|
def initialize(*args)
|
211
|
+
@closed = false
|
243
212
|
end
|
244
213
|
|
245
214
|
def closed?
|
246
|
-
@closed
|
215
|
+
@closed
|
247
216
|
end
|
248
217
|
|
249
218
|
def close
|
219
|
+
@closed = true
|
220
|
+
nil
|
250
221
|
end
|
251
222
|
|
252
223
|
def readuntil(*args)
|
@@ -258,63 +229,17 @@ class StubSocket #:nodoc:
|
|
258
229
|
|
259
230
|
class StubIO
|
260
231
|
def setsockopt(*args); end
|
232
|
+
def peer_cert; end
|
233
|
+
def peeraddr; ["AF_INET", 443, "127.0.0.1", "127.0.0.1"] end
|
234
|
+
def ssl_version; "TLSv1.3" end
|
235
|
+
def cipher; ["TLS_AES_128_GCM_SHA256", "TLSv1.3", 128, 128] end
|
261
236
|
end
|
262
237
|
end
|
263
238
|
|
264
|
-
module Net #:nodoc: all
|
265
|
-
|
266
|
-
class WebMockNetBufferedIO < BufferedIO
|
267
|
-
def initialize(io, *args, **kwargs)
|
268
|
-
io = case io
|
269
|
-
when Socket, OpenSSL::SSL::SSLSocket, IO
|
270
|
-
io
|
271
|
-
when StringIO
|
272
|
-
PatchedStringIO.new(io.string)
|
273
|
-
when String
|
274
|
-
PatchedStringIO.new(io)
|
275
|
-
end
|
276
|
-
raise "Unable to create local socket" unless io
|
277
|
-
|
278
|
-
# Prior to 2.4.0 `BufferedIO` only takes a single argument (`io`) with no
|
279
|
-
# options. Here we pass through our full set of arguments only if we're
|
280
|
-
# on 2.4.0 or later, and use a simplified invocation otherwise.
|
281
|
-
if RUBY_VERSION >= '2.4.0'
|
282
|
-
super
|
283
|
-
else
|
284
|
-
super(io)
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
if RUBY_VERSION >= '2.6.0'
|
289
|
-
# https://github.com/ruby/ruby/blob/7d02441f0d6e5c9d0a73a024519eba4f69e36dce/lib/net/protocol.rb#L208
|
290
|
-
# Modified version of method from ruby, so that nil is always passed into orig_read_nonblock to avoid timeout
|
291
|
-
def rbuf_fill
|
292
|
-
case rv = @io.read_nonblock(BUFSIZE, nil, exception: false)
|
293
|
-
when String
|
294
|
-
return if rv.nil?
|
295
|
-
@rbuf << rv
|
296
|
-
rv.clear
|
297
|
-
return
|
298
|
-
when :wait_readable
|
299
|
-
@io.to_io.wait_readable(@read_timeout) or raise Net::ReadTimeout
|
300
|
-
when :wait_writable
|
301
|
-
@io.to_io.wait_writable(@read_timeout) or raise Net::ReadTimeout
|
302
|
-
when nil
|
303
|
-
raise EOFError, 'end of file reached'
|
304
|
-
end while true
|
305
|
-
end
|
306
|
-
end
|
307
|
-
end
|
308
|
-
|
309
|
-
end
|
310
|
-
|
311
|
-
|
312
239
|
module WebMock
|
313
240
|
module NetHTTPUtility
|
314
241
|
|
315
242
|
def self.request_signature_from_request(net_http, request, body = nil)
|
316
|
-
protocol = net_http.use_ssl? ? "https" : "http"
|
317
|
-
|
318
243
|
path = request.path
|
319
244
|
|
320
245
|
if path.respond_to?(:request_uri) #https://github.com/bblimke/webmock/issues/288
|
@@ -323,11 +248,10 @@ module WebMock
|
|
323
248
|
|
324
249
|
path = WebMock::Util::URI.heuristic_parse(path).request_uri if path =~ /^http/
|
325
250
|
|
326
|
-
uri =
|
251
|
+
uri = get_uri(net_http, path)
|
327
252
|
method = request.method.downcase.to_sym
|
328
253
|
|
329
254
|
headers = Hash[*request.to_hash.map {|k,v| [k, v]}.inject([]) {|r,x| r + x}]
|
330
|
-
validate_headers(headers)
|
331
255
|
|
332
256
|
if request.body_stream
|
333
257
|
body = request.body_stream.read
|
@@ -343,23 +267,13 @@ module WebMock
|
|
343
267
|
WebMock::RequestSignature.new(method, uri, body: request.body, headers: headers)
|
344
268
|
end
|
345
269
|
|
346
|
-
def self.
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
#
|
351
|
-
|
352
|
-
#
|
353
|
-
# This could create a false positive in a test suite with WebMock.
|
354
|
-
#
|
355
|
-
# So before this point, WebMock raises an ArgumentError if any of the headers are symbols
|
356
|
-
# instead of the cryptic NoMethodError "undefined method `split' ...` from Net::HTTP
|
357
|
-
if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.3.0')
|
358
|
-
header_as_symbol = headers.keys.find {|header| header.is_a? Symbol}
|
359
|
-
if header_as_symbol
|
360
|
-
raise ArgumentError.new("Net:HTTP does not accept headers as symbols")
|
361
|
-
end
|
362
|
-
end
|
270
|
+
def self.get_uri(net_http, path = nil)
|
271
|
+
protocol = net_http.use_ssl? ? "https" : "http"
|
272
|
+
|
273
|
+
hostname = net_http.address
|
274
|
+
hostname = "[#{hostname}]" if /\A\[.*\]\z/ !~ hostname && /:/ =~ hostname
|
275
|
+
|
276
|
+
"#{protocol}://#{hostname}:#{net_http.port}#{path}"
|
363
277
|
end
|
364
278
|
|
365
279
|
def self.check_right_http_connection
|
@@ -281,6 +281,8 @@ module WebMock
|
|
281
281
|
if (@pattern).is_a?(Hash)
|
282
282
|
return true if @pattern.empty?
|
283
283
|
matching_body_hashes?(body_as_hash(body, content_type), @pattern, content_type)
|
284
|
+
elsif (@pattern).is_a?(Array)
|
285
|
+
matching_body_array?(body_as_hash(body, content_type), @pattern, content_type)
|
284
286
|
elsif (@pattern).is_a?(WebMock::Matchers::HashIncludingMatcher)
|
285
287
|
@pattern == body_as_hash(body, content_type)
|
286
288
|
else
|
@@ -295,8 +297,9 @@ module WebMock
|
|
295
297
|
end
|
296
298
|
|
297
299
|
private
|
300
|
+
|
298
301
|
def body_as_hash(body, content_type)
|
299
|
-
case
|
302
|
+
case body_format(content_type)
|
300
303
|
when :json then
|
301
304
|
WebMock::Util::JSON.parse(body)
|
302
305
|
when :xml then
|
@@ -306,6 +309,11 @@ module WebMock
|
|
306
309
|
end
|
307
310
|
end
|
308
311
|
|
312
|
+
def body_format(content_type)
|
313
|
+
normalized_content_type = content_type.sub(/\A(application\/)[a-zA-Z0-9.-]+\+(json|xml)\Z/,'\1\2')
|
314
|
+
BODY_FORMATS[normalized_content_type]
|
315
|
+
end
|
316
|
+
|
309
317
|
def assert_non_multipart_body(content_type)
|
310
318
|
if content_type =~ %r{^multipart/form-data}
|
311
319
|
raise ArgumentError.new("WebMock does not support matching body for multipart/form-data requests yet :(")
|
@@ -338,19 +346,33 @@ module WebMock
|
|
338
346
|
def matching_body_hashes?(query_parameters, pattern, content_type)
|
339
347
|
return false unless query_parameters.is_a?(Hash)
|
340
348
|
return false unless query_parameters.keys.sort == pattern.keys.sort
|
341
|
-
|
349
|
+
|
350
|
+
query_parameters.all? do |key, actual|
|
342
351
|
expected = pattern[key]
|
352
|
+
matching_values(actual, expected, content_type)
|
353
|
+
end
|
354
|
+
end
|
343
355
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
356
|
+
def matching_body_array?(query_parameters, pattern, content_type)
|
357
|
+
return false unless query_parameters.is_a?(Array)
|
358
|
+
return false unless query_parameters.length == pattern.length
|
359
|
+
|
360
|
+
query_parameters.each_with_index do |actual, index|
|
361
|
+
expected = pattern[index]
|
362
|
+
return false unless matching_values(actual, expected, content_type)
|
350
363
|
end
|
364
|
+
|
351
365
|
true
|
352
366
|
end
|
353
367
|
|
368
|
+
def matching_values(actual, expected, content_type)
|
369
|
+
return matching_body_hashes?(actual, expected, content_type) if actual.is_a?(Hash) && expected.is_a?(Hash)
|
370
|
+
return matching_body_array?(actual, expected, content_type) if actual.is_a?(Array) && expected.is_a?(Array)
|
371
|
+
|
372
|
+
expected = WebMock::Util::ValuesStringifier.stringify_values(expected) if url_encoded_body?(content_type)
|
373
|
+
expected === actual
|
374
|
+
end
|
375
|
+
|
354
376
|
def empty_string?(string)
|
355
377
|
string.nil? || string == ""
|
356
378
|
end
|
@@ -35,11 +35,11 @@ module WebMock
|
|
35
35
|
alias == eql?
|
36
36
|
|
37
37
|
def url_encoded?
|
38
|
-
!!(headers
|
38
|
+
!!(headers&.fetch('Content-Type', nil)&.start_with?('application/x-www-form-urlencoded'))
|
39
39
|
end
|
40
40
|
|
41
41
|
def json_headers?
|
42
|
-
!!(headers
|
42
|
+
!!(headers&.fetch('Content-Type', nil)&.start_with?('application/json'))
|
43
43
|
end
|
44
44
|
|
45
45
|
private
|
data/lib/webmock/request_stub.rb
CHANGED
@@ -24,6 +24,21 @@ module WebMock
|
|
24
24
|
end
|
25
25
|
alias_method :and_return, :to_return
|
26
26
|
|
27
|
+
def to_return_json(*response_hashes)
|
28
|
+
raise ArgumentError, '#to_return_json does not support passing a block' if block_given?
|
29
|
+
|
30
|
+
json_response_hashes = [*response_hashes].flatten.map do |resp_h|
|
31
|
+
headers, body = resp_h.values_at(:headers, :body)
|
32
|
+
resp_h.merge(
|
33
|
+
headers: {content_type: 'application/json'}.merge(headers.to_h),
|
34
|
+
body: body.is_a?(Hash) ? body.to_json : body
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
to_return(json_response_hashes)
|
39
|
+
end
|
40
|
+
alias_method :and_return_json, :to_return_json
|
41
|
+
|
27
42
|
def to_rack(app, options={})
|
28
43
|
@responses_sequences << ResponsesSequence.new([RackResponse.new(app)])
|
29
44
|
end
|
data/lib/webmock/response.rb
CHANGED
@@ -14,8 +14,11 @@ module WebMock
|
|
14
14
|
|
15
15
|
class Response
|
16
16
|
def initialize(options = {})
|
17
|
-
|
17
|
+
case options
|
18
|
+
when IO, StringIO
|
18
19
|
self.options = read_raw_response(options)
|
20
|
+
when String
|
21
|
+
self.options = read_raw_response(StringIO.new(options))
|
19
22
|
else
|
20
23
|
self.options = options
|
21
24
|
end
|
@@ -120,13 +123,8 @@ module WebMock
|
|
120
123
|
end
|
121
124
|
end
|
122
125
|
|
123
|
-
def read_raw_response(
|
124
|
-
|
125
|
-
string = raw_response.read
|
126
|
-
raw_response.close
|
127
|
-
raw_response = string
|
128
|
-
end
|
129
|
-
socket = ::Net::BufferedIO.new(raw_response)
|
126
|
+
def read_raw_response(io)
|
127
|
+
socket = ::Net::BufferedIO.new(io)
|
130
128
|
response = ::Net::HTTPResponse.read_new(socket)
|
131
129
|
transfer_encoding = response.delete('transfer-encoding') #chunks were already read by curl
|
132
130
|
response.reading_body(socket, true) {}
|
@@ -138,6 +136,8 @@ module WebMock
|
|
138
136
|
options[:body] = response.read_body
|
139
137
|
options[:status] = [response.code.to_i, response.message]
|
140
138
|
options
|
139
|
+
ensure
|
140
|
+
socket.close
|
141
141
|
end
|
142
142
|
|
143
143
|
InvalidBody = Class.new(StandardError)
|
data/lib/webmock/version.rb
CHANGED
data/lib/webmock/webmock.rb
CHANGED
@@ -70,6 +70,16 @@ module WebMock
|
|
70
70
|
Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri) )
|
71
71
|
end
|
72
72
|
|
73
|
+
def self.net_http_connect_on_start?(uri)
|
74
|
+
allowed = Config.instance.net_http_connect_on_start || false
|
75
|
+
|
76
|
+
if [true, false].include?(allowed)
|
77
|
+
allowed
|
78
|
+
else
|
79
|
+
net_connect_explicit_allowed?(allowed, uri)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
73
83
|
def self.net_connect_explicit_allowed?(allowed, uri=nil)
|
74
84
|
case allowed
|
75
85
|
when Array
|
data/minitest/webmock_spec.rb
CHANGED
@@ -20,7 +20,7 @@ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should raise error on non stubbed request" do
|
23
|
-
|
23
|
+
expect { http_request(:get, "http://www.example.net/") }.must_raise(WebMock::NetConnectNotAllowedError)
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should verify that expected request occured" do
|
@@ -135,6 +135,28 @@ unless RUBY_PLATFORM =~ /java/
|
|
135
135
|
expect { make_request(:get, 'http://www.example.com') }.to raise_error Async::TimeoutError
|
136
136
|
end
|
137
137
|
|
138
|
+
it 'does not invoke "after real request" callbacks for stubbed requests' do
|
139
|
+
WebMock.allow_net_connect!
|
140
|
+
stub_request(:get, 'http://www.example.com').to_return(body: 'abc')
|
141
|
+
|
142
|
+
callback_invoked = false
|
143
|
+
WebMock.after_request(real_requests_only: true) { |_| callback_invoked = true }
|
144
|
+
|
145
|
+
make_request(:get, 'http://www.example.com')
|
146
|
+
expect(callback_invoked).to eq(false)
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'does invoke "after request" callbacks for stubbed requests' do
|
150
|
+
WebMock.allow_net_connect!
|
151
|
+
stub_request(:get, 'http://www.example.com').to_return(body: 'abc')
|
152
|
+
|
153
|
+
callback_invoked = false
|
154
|
+
WebMock.after_request(real_requests_only: false) { |_| callback_invoked = true }
|
155
|
+
|
156
|
+
make_request(:get, 'http://www.example.com')
|
157
|
+
expect(callback_invoked).to eq(true)
|
158
|
+
end
|
159
|
+
|
138
160
|
context 'scheme and protocol' do
|
139
161
|
let(:default_response_headers) { {} }
|
140
162
|
|
@@ -226,7 +248,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
226
248
|
end
|
227
249
|
|
228
250
|
context 'multiple requests' do
|
229
|
-
let(:endpoint) { Async::HTTP::Endpoint.parse('http://www.example.com') }
|
251
|
+
let!(:endpoint) { Async::HTTP::Endpoint.parse('http://www.example.com') }
|
230
252
|
let(:requests_count) { 3 }
|
231
253
|
|
232
254
|
shared_examples :common do
|
@@ -278,13 +300,13 @@ unless RUBY_PLATFORM =~ /java/
|
|
278
300
|
end
|
279
301
|
|
280
302
|
context 'HTTP1 protocol' do
|
281
|
-
let(:protocol) { Async::HTTP::Protocol::HTTP1 }
|
303
|
+
let!(:protocol) { Async::HTTP::Protocol::HTTP1 }
|
282
304
|
|
283
305
|
include_examples :common
|
284
306
|
end
|
285
307
|
|
286
308
|
context 'HTTP2 protocol' do
|
287
|
-
let(:protocol) { Async::HTTP::Protocol::HTTP2 }
|
309
|
+
let!(:protocol) { Async::HTTP::Protocol::HTTP2 }
|
288
310
|
|
289
311
|
include_examples :common
|
290
312
|
end
|
@@ -309,13 +331,13 @@ unless RUBY_PLATFORM =~ /java/
|
|
309
331
|
end
|
310
332
|
|
311
333
|
context 'HTTP1 protocol' do
|
312
|
-
let(:protocol) { Async::HTTP::Protocol::HTTP1 }
|
334
|
+
let!(:protocol) { Async::HTTP::Protocol::HTTP1 }
|
313
335
|
|
314
336
|
include_examples :common
|
315
337
|
end
|
316
338
|
|
317
339
|
context 'HTTP2 protocol' do
|
318
|
-
let(:protocol) { Async::HTTP::Protocol::HTTP2 }
|
340
|
+
let!(:protocol) { Async::HTTP::Protocol::HTTP2 }
|
319
341
|
|
320
342
|
include_examples :common
|
321
343
|
end
|
@@ -383,6 +383,17 @@ unless RUBY_PLATFORM =~ /java/
|
|
383
383
|
expect(c.body).to eq("abc")
|
384
384
|
end
|
385
385
|
|
386
|
+
it "supports headers containing the ':' character" do
|
387
|
+
stub_request(:get, "www.example.com").with(headers: {'Referer'=>'http://www.example.com'}).to_return(body: "abc")
|
388
|
+
|
389
|
+
c = Curl::Easy.new
|
390
|
+
c.url = "http://www.example.com"
|
391
|
+
c.headers = ["Referer: http://www.example.com"]
|
392
|
+
c.http(:GET)
|
393
|
+
expect(c.body).to eq("abc")
|
394
|
+
expect(c.headers).to eq(["Referer: http://www.example.com"])
|
395
|
+
end
|
396
|
+
|
386
397
|
describe 'match request body' do
|
387
398
|
it 'for post' do
|
388
399
|
stub_request(:post, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")
|
@@ -22,7 +22,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
22
22
|
|
23
23
|
def make_request
|
24
24
|
EM.run do
|
25
|
-
request = EM::HttpRequest.new(http_url).get(redirects: 1)
|
25
|
+
request = EM::HttpRequest.new(http_url, ssl: {verify_peer: true}).get(redirects: 1)
|
26
26
|
request.callback { EM.stop }
|
27
27
|
end
|
28
28
|
end
|
@@ -71,6 +71,35 @@ unless RUBY_PLATFORM =~ /java/
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
+
it "only calls request middleware once" do
|
75
|
+
stub_request(:get, "www.example.com")
|
76
|
+
|
77
|
+
middleware = Class.new do
|
78
|
+
def self.called!
|
79
|
+
@called = called + 1
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.called
|
83
|
+
@called || 0
|
84
|
+
end
|
85
|
+
|
86
|
+
def request(client, head, body)
|
87
|
+
self.class.called!
|
88
|
+
[head, body]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
EM.run do
|
93
|
+
conn = EventMachine::HttpRequest.new('http://www.example.com/')
|
94
|
+
conn.use middleware
|
95
|
+
http = conn.get
|
96
|
+
http.callback do
|
97
|
+
expect(middleware.called).to eq(1)
|
98
|
+
EM.stop
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
74
103
|
let(:response_middleware) do
|
75
104
|
Class.new do
|
76
105
|
def response(resp)
|
@@ -119,6 +148,33 @@ unless RUBY_PLATFORM =~ /java/
|
|
119
148
|
context 'making a real request', net_connect: true do
|
120
149
|
before { WebMock.allow_net_connect! }
|
121
150
|
include_examples "em-http-request middleware/after_request hook integration"
|
151
|
+
|
152
|
+
it "only calls request middleware once" do
|
153
|
+
middleware = Class.new do
|
154
|
+
def self.called!
|
155
|
+
@called = called + 1
|
156
|
+
end
|
157
|
+
|
158
|
+
def self.called
|
159
|
+
@called || 0
|
160
|
+
end
|
161
|
+
|
162
|
+
def request(client, head, body)
|
163
|
+
self.class.called!
|
164
|
+
[head, body]
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
EM.run do
|
169
|
+
conn = EventMachine::HttpRequest.new(webmock_server_url)
|
170
|
+
conn.use middleware
|
171
|
+
http = conn.get
|
172
|
+
http.callback do
|
173
|
+
expect(middleware.called).to eq(1)
|
174
|
+
EM.stop
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
122
178
|
end
|
123
179
|
|
124
180
|
context 'when the request is stubbed' do
|
@@ -16,7 +16,7 @@ module EMHttpRequestSpecHelper
|
|
16
16
|
error_set = false
|
17
17
|
uri = Addressable::URI.heuristic_parse(uri)
|
18
18
|
EventMachine.run {
|
19
|
-
request = EventMachine::HttpRequest.new("#{uri.normalize.to_s}")
|
19
|
+
request = EventMachine::HttpRequest.new("#{uri.normalize.to_s}", ssl: {verify_peer: true})
|
20
20
|
http = request.send(method, {
|
21
21
|
timeout: 30,
|
22
22
|
body: options[:body],
|
@@ -20,7 +20,7 @@ describe "Excon" do
|
|
20
20
|
it "should support excon response_block for real requests", net_connect: true do
|
21
21
|
a = []
|
22
22
|
WebMock.allow_net_connect!
|
23
|
-
r = Excon.new('
|
23
|
+
r = Excon.new('https://httpstat.us/200', headers: { "Accept" => "*" }).
|
24
24
|
get(response_block: lambda {|e, remaining, total| a << e}, chunk_size: 1)
|
25
25
|
expect(a).to eq(["2", "0", "0", " ", "O", "K"])
|
26
26
|
expect(r.body).to eq("")
|
@@ -41,7 +41,7 @@ describe "Excon" do
|
|
41
41
|
WebMock.after_request { |_, res|
|
42
42
|
response = res
|
43
43
|
}
|
44
|
-
r = Excon.new('
|
44
|
+
r = Excon.new('https://httpstat.us/200', headers: { "Accept" => "*" }).
|
45
45
|
get(response_block: lambda {|e, remaining, total| a << e}, chunk_size: 1)
|
46
46
|
expect(response.body).to eq("200 OK")
|
47
47
|
expect(a).to eq(["2", "0", "0", " ", "O", "K"])
|
@@ -70,6 +70,38 @@ if RUBY_PLATFORM =~ /java/
|
|
70
70
|
expect(failure_handler).to have_received(:call)
|
71
71
|
end
|
72
72
|
end
|
73
|
+
|
74
|
+
context 'when used in a streaming mode' do
|
75
|
+
let(:webmock_server_url) {"http://#{WebMockServer.instance.host_with_port}/"}
|
76
|
+
let(:result_chunks) { [] }
|
77
|
+
|
78
|
+
def manticore_streaming_get
|
79
|
+
Manticore.get(webmock_server_url).tap do |req|
|
80
|
+
req.on_success do |response|
|
81
|
+
response.body do |chunk|
|
82
|
+
result_chunks << chunk
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when connections are allowed' do
|
89
|
+
it 'works' do
|
90
|
+
WebMock.allow_net_connect!
|
91
|
+
expect { manticore_streaming_get.call }.to_not raise_error
|
92
|
+
expect(result_chunks).to_not be_empty
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'when stubbed' do
|
97
|
+
it 'works' do
|
98
|
+
stub_body = 'hello!'
|
99
|
+
stub_request(:get, webmock_server_url).to_return(body: stub_body)
|
100
|
+
expect { manticore_streaming_get.call }.to_not raise_error
|
101
|
+
expect(result_chunks).to eq [stub_body]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
73
105
|
end
|
74
106
|
end
|
75
107
|
end
|