Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mrkt/concerns/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def init_connection

conn.response :logger, @logger, (@log_options || {}) if @debug
conn.response :mkto, content_type: /\bjson$/
conn.response :mkto_exceptional_response

conn.options.timeout = @options[:read_timeout] if @options.key?(:read_timeout)
conn.options.open_timeout = @options[:open_timeout] if @options.key?(:open_timeout)
Expand Down
2 changes: 2 additions & 0 deletions lib/mrkt/faraday_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
module Mrkt
module FaradayMiddleware
autoload :Response, 'mrkt/faraday_middleware/response'
autoload :ExceptionalResponse, 'mrkt/faraday_middleware/exceptional_response'
end

if Faraday::Middleware.respond_to? :register_middleware
Faraday::Response.register_middleware mkto: -> { Mrkt::FaradayMiddleware::Response }
Faraday::Response.register_middleware mkto_exceptional_response: -> { Mrkt::FaradayMiddleware::ExceptionalResponse }
end
end
25 changes: 25 additions & 0 deletions lib/mrkt/faraday_middleware/exceptional_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Mrkt
module FaradayMiddleware
# A middleware to handle exceptional non-json responses.
# In some cases, for example in trouble of marketo servers, we confirmed
# there is possibility that servers could respond with non json response
# unexpectedly.
class ExceptionalResponse < Faraday::Response::Middleware
ServerErrorStatuses = 400...600

def on_complete(env)
# Nothing to do by this handler if response content type is like json
return if env[:response_headers]['Content-Type'] =~ /\bjson$/

case env[:status]
when ServerErrorStatuses
raise Mrkt::Errors::Unknown, response_values(env)
end
end

def response_values(env)
{:status => env.status, :headers => env.response_headers, :body => env.body}
end
end
end
end
41 changes: 41 additions & 0 deletions spec/faraday_middleware/exceptional_response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe Mrkt::FaradayMiddleware::ExceptionalResponse do
before do
response = lambda { |env|
[status, {'Content-Type' => content_type}, body]
}
@conn = Faraday.new do |b|
b.use Mrkt::FaradayMiddleware::ExceptionalResponse
b.adapter :test do |stub|
stub.get('/', &response)
end
end
end

context 'with application/json and with server error' do
let(:content_type) { 'application/json' }
let(:status) { 500 }
let(:body) { '{}' }

it 'does nothing' do
expect(@conn.get('/').body).to eq body
end
end

context 'with non json content-type and with server error' do
let(:content_type) { 'text/plain' }
let(:status) { 500 }
let(:body) { 'something wrong' }

it { expect { @conn.get('/') }.to raise_error(Mrkt::Errors::Unknown) }
end

context 'with non json content-type but with successful response' do
let(:content_type) { 'text/html' }
let(:status) { 200 }
let(:body) { 'ok' }

it 'does nothing' do
expect(@conn.get('/').body).to eq body
end
end
end