mirror of
https://github.com/lostisland/faraday.git
synced 2025-12-04 00:03:34 -05:00
Adds em-http adapter to RSpec tests
This commit is contained in:
parent
ded4f6ef9a
commit
ab33c8ace1
@ -166,17 +166,17 @@ module Faraday
|
||||
end
|
||||
|
||||
def raise_error(msg)
|
||||
errklass = Faraday::ClientError
|
||||
if msg == Errno::ETIMEDOUT
|
||||
errklass = Faraday::TimeoutError
|
||||
error_class = Faraday::ClientError
|
||||
if msg == Errno::ETIMEDOUT || (msg.is_a?(String) && msg.include?('timeout error'))
|
||||
error_class = Faraday::TimeoutError
|
||||
msg = 'request timed out'
|
||||
elsif msg == Errno::ECONNREFUSED
|
||||
errklass = Faraday::ConnectionFailed
|
||||
error_class = Faraday::ConnectionFailed
|
||||
msg = 'connection refused'
|
||||
elsif msg == 'connection closed by server'
|
||||
errklass = Faraday::ConnectionFailed
|
||||
error_class = Faraday::ConnectionFailed
|
||||
end
|
||||
raise errklass, msg
|
||||
raise error_class, msg
|
||||
end
|
||||
|
||||
# @return [Boolean]
|
||||
|
||||
16
spec/faraday/adapter/em_http_spec.rb
Normal file
16
spec/faraday/adapter/em_http_spec.rb
Normal file
@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Faraday::Adapter::EMHttp do
|
||||
features :request_body_on_query_methods, :reason_phrase_parse, :trace_method, :connect_method,
|
||||
:skip_response_body_on_head, :parallel
|
||||
|
||||
it_behaves_like 'an adapter'
|
||||
|
||||
it 'allows to provide adapter specific configs' do
|
||||
url = URI('https://example.com:1234')
|
||||
adapter = Faraday::Adapter::EMHttp.new nil, inactivity_timeout: 20
|
||||
req = adapter.create_request(url: url, request: {})
|
||||
|
||||
expect(req.connopts.inactivity_timeout).to eq(20)
|
||||
end
|
||||
end
|
||||
@ -25,6 +25,7 @@ SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCo
|
||||
|
||||
SimpleCov.start do
|
||||
add_filter '/spec/'
|
||||
add_filter '/lib/faraday/adapter/em_http_ssl_patch'
|
||||
minimum_coverage 90
|
||||
minimum_coverage_by_file 70
|
||||
end
|
||||
|
||||
@ -11,11 +11,15 @@ module Faraday
|
||||
@features = features
|
||||
end
|
||||
|
||||
def on_feature(name, &block)
|
||||
def on_feature(name)
|
||||
yield if block_given? && feature?(name)
|
||||
end
|
||||
|
||||
def feature?(name)
|
||||
if @features.nil?
|
||||
superclass.on_feature(name, &block) if superclass.respond_to?(:on_feature)
|
||||
elsif block_given? && @features.include?(name)
|
||||
yield
|
||||
superclass.feature?(name) if superclass.respond_to?(:feature?)
|
||||
elsif @features.include?(name)
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
@ -54,11 +58,7 @@ module Faraday
|
||||
yield
|
||||
ensure
|
||||
old_env.each do |key, value|
|
||||
if value == false
|
||||
ENV.delete key
|
||||
else
|
||||
ENV[key] = value
|
||||
end
|
||||
value == false ? ENV.delete(key) : ENV[key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -4,10 +4,12 @@ shared_examples 'a request method' do |http_method|
|
||||
let(:query_or_body) { method_with_body?(http_method) ? :body : :query }
|
||||
let(:response) { conn.public_send(http_method, '/') }
|
||||
|
||||
it 'retrieves the response body' do
|
||||
res_body = 'test'
|
||||
request_stub.to_return(body: res_body)
|
||||
expect(conn.public_send(http_method, '/').body).to eq(res_body)
|
||||
unless http_method == :head && feature?(:skip_response_body_on_head)
|
||||
it 'retrieves the response body' do
|
||||
res_body = 'test'
|
||||
request_stub.to_return(body: res_body)
|
||||
expect(conn.public_send(http_method, '/').body).to eq(res_body)
|
||||
end
|
||||
end
|
||||
|
||||
it 'handles headers with multiple values' do
|
||||
@ -160,28 +162,44 @@ shared_examples 'a request method' do |http_method|
|
||||
end
|
||||
|
||||
on_feature :parallel do
|
||||
it 'handles parallel requests' do
|
||||
resp1 = nil
|
||||
resp2 = nil
|
||||
payload1 = { a: '1' }
|
||||
payload2 = { b: '2' }
|
||||
request_stub.with(Hash[query_or_body, payload1])
|
||||
.to_return(body: payload1.to_json)
|
||||
stub_request(http_method, remote).with(Hash[query_or_body, payload2])
|
||||
.to_return(body: payload2.to_json)
|
||||
context 'with parallel setup' do
|
||||
before do
|
||||
@resp1 = nil
|
||||
@resp2 = nil
|
||||
@payload1 = { a: '1' }
|
||||
@payload2 = { b: '2' }
|
||||
|
||||
conn.in_parallel do
|
||||
resp1 = conn.public_send(http_method, '/', payload1)
|
||||
resp2 = conn.public_send(http_method, '/', payload2)
|
||||
request_stub
|
||||
.with(Hash[query_or_body, @payload1])
|
||||
.to_return(body: @payload1.to_json)
|
||||
|
||||
expect(conn.in_parallel?).to be_truthy
|
||||
expect(resp1.body).to be_nil
|
||||
expect(resp2.body).to be_nil
|
||||
stub_request(http_method, remote)
|
||||
.with(Hash[query_or_body, @payload2])
|
||||
.to_return(body: @payload2.to_json)
|
||||
|
||||
conn.in_parallel do
|
||||
@resp1 = conn.public_send(http_method, '/', @payload1)
|
||||
@resp2 = conn.public_send(http_method, '/', @payload2)
|
||||
|
||||
expect(conn.in_parallel?).to be_truthy
|
||||
expect(@resp1.body).to be_nil
|
||||
expect(@resp2.body).to be_nil
|
||||
end
|
||||
|
||||
expect(conn.in_parallel?).to be_falsey
|
||||
end
|
||||
|
||||
expect(conn.in_parallel?).to be_falsey
|
||||
expect(resp1&.body).to eq(payload1.to_json)
|
||||
expect(resp2&.body).to eq(payload2.to_json)
|
||||
it 'handles parallel requests status' do
|
||||
expect(@resp1&.status).to eq(200)
|
||||
expect(@resp2&.status).to eq(200)
|
||||
end
|
||||
|
||||
unless http_method == :head && feature?(:skip_response_body_on_head)
|
||||
it 'handles parallel requests body' do
|
||||
expect(@resp1&.body).to eq(@payload1.to_json)
|
||||
expect(@resp2&.body).to eq(@payload2.to_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user