From ab33c8ace10f6c4c3a127c30a043238e3b54364c Mon Sep 17 00:00:00 2001 From: iMacTia Date: Tue, 5 Mar 2019 18:26:46 +0000 Subject: [PATCH] Adds em-http adapter to RSpec tests --- lib/faraday/adapter/em_http.rb | 12 ++-- spec/faraday/adapter/em_http_spec.rb | 16 +++++ spec/spec_helper.rb | 1 + spec/support/helper_methods.rb | 18 +++--- .../support/shared_examples/request_method.rb | 62 ++++++++++++------- 5 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 spec/faraday/adapter/em_http_spec.rb diff --git a/lib/faraday/adapter/em_http.rb b/lib/faraday/adapter/em_http.rb index 264b9287..53e86ef3 100644 --- a/lib/faraday/adapter/em_http.rb +++ b/lib/faraday/adapter/em_http.rb @@ -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] diff --git a/spec/faraday/adapter/em_http_spec.rb b/spec/faraday/adapter/em_http_spec.rb new file mode 100644 index 00000000..d11ac92d --- /dev/null +++ b/spec/faraday/adapter/em_http_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9bdf7b2f..1ec4df77 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/support/helper_methods.rb b/spec/support/helper_methods.rb index 6560d686..af2f1d67 100644 --- a/spec/support/helper_methods.rb +++ b/spec/support/helper_methods.rb @@ -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 diff --git a/spec/support/shared_examples/request_method.rb b/spec/support/shared_examples/request_method.rb index 3bfe96cf..a8ab0d4e 100644 --- a/spec/support/shared_examples/request_method.rb +++ b/spec/support/shared_examples/request_method.rb @@ -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