Refactor logger conditional to use coercion and parallel assignment

This commit is contained in:
Ryan Buckley 2015-12-14 18:10:26 -08:00
parent e6e06556aa
commit 7969c9daca
2 changed files with 6 additions and 11 deletions

View File

@ -55,13 +55,8 @@ class Grape::Middleware::Logger < Grape::Middleware::Globals
after_failure(error)
throw(:error, error)
else
# Usually a rack response object is returned: https://github.com/ruby-grape/grape/blob/master/UPGRADING.md#changes-in-middleware
# However, rack/auth/abstract/handler.rb still returns an array instead of a rack response object.
if @app_response.is_a?(Array)
after(@app_response[0])
else
after(@app_response.status)
end
status, _, _ = *@app_response
after(status)
end
@app_response
end

View File

@ -40,20 +40,20 @@ describe Grape::Middleware::Logger do
end
context 'when calling the app results in an array response' do
let(:app_response_array) { [401, {}, []] }
let(:app_response) { [401, {}, []] }
it 'calls +after+ with the correct status' do
expect(app).to receive(:call).with(env).and_return(app_response_array)
expect(app).to receive(:call).with(env).and_return(app_response)
expect(subject).to receive(:before)
expect(subject).to receive(:after).with(401)
subject.call!(env)
end
it 'returns the @app_response' do
expect(app).to receive(:call).with(env).and_return(app_response_array)
expect(app).to receive(:call).with(env).and_return(app_response)
allow(subject).to receive(:before)
allow(subject).to receive(:after)
expect(subject.call!(env)).to eq app_response_array
expect(subject.call!(env)).to eq app_response
end
end
end