use +before+ because it works on Grape master

This commit is contained in:
Ryan Buckley 2015-05-05 22:42:17 -07:00
parent d65714ffd8
commit 4cac0e0ac0
3 changed files with 17 additions and 14 deletions

View File

@ -9,9 +9,17 @@ module Grape
# Overrides # Overrides
# #
def before
@start_time = Time.now
super
logger.info ''
logger.info %Q(Started #{env['grape.request'].request_method} "#{env['grape.request'].path}")
logger.info %Q( Parameters: #{parameters})
end
def call!(env) def call!(env)
@env = env @env = env
_before before
error = catch(:error) { @app_response = @app.call(@env); nil } error = catch(:error) { @app_response = @app.call(@env); nil }
if error if error
after_failure(error) after_failure(error)
@ -31,14 +39,6 @@ module Grape
# Helpers # Helpers
# #
# @todo rename to +before+ when Grape v0.12.0 is released
def _before
@start_time = Time.now
logger.info ''
logger.info %Q(Started #{env['grape.request'].request_method} "#{env['grape.request'].path}")
logger.info %Q( Parameters: #{parameters})
end
def after_failure(error) def after_failure(error)
logger.info %Q( Error: #{error[:message]}) if error[:message] logger.info %Q( Error: #{error[:message]}) if error[:message]
after(error[:status]) after(error[:status])

View File

@ -1,7 +1,7 @@
module Grape module Grape
module Middleware module Middleware
class Logger class Logger
VERSION = '0.1.0' VERSION = '0.1.1'
end end
end end
end end

View File

@ -20,13 +20,16 @@ describe Grape::Middleware::Logger do
} }
} }
} }
# @todo remove when Grape v0.12.0 is released
before(:each) { allow(subject).to receive(:before) }
describe '#call!' do describe '#call!' do
context 'when calling the app results in an error response' do context 'when calling the app results in an error response' do
let(:error) { { status: 400 } } let(:error) { { status: 400 } }
it 'calls +after_failure+ and rethrows the error' do it 'calls +after_failure+ and rethrows the error' do
expect(app).to receive(:call).with(env).and_throw(:error, error) expect(app).to receive(:call).with(env).and_throw(:error, error)
expect(subject).to receive(:_before) expect(subject).to receive(:before)
expect(subject).to receive(:after_failure).with(error) expect(subject).to receive(:after_failure).with(error)
expect(subject).to receive(:throw).with(:error, error) expect(subject).to receive(:throw).with(:error, error)
subject.call!(env) subject.call!(env)
@ -36,21 +39,21 @@ describe Grape::Middleware::Logger do
context 'when there is no error' do context 'when there is no error' do
it 'calls +after+ with the correct status' do it 'calls +after+ with the correct status' do
expect(app).to receive(:call).with(env).and_return(app_response) expect(app).to receive(:call).with(env).and_return(app_response)
expect(subject).to receive(:_before) expect(subject).to receive(:before)
expect(subject).to receive(:after).with(200) expect(subject).to receive(:after).with(200)
subject.call!(env) subject.call!(env)
end end
it 'returns the @app_response' do it 'returns the @app_response' do
expect(app).to receive(:call).with(env).and_return(app_response) expect(app).to receive(:call).with(env).and_return(app_response)
allow(subject).to receive(:_before) allow(subject).to receive(:before)
allow(subject).to receive(:after) allow(subject).to receive(:after)
expect(subject.call!(env)).to eq app_response expect(subject.call!(env)).to eq app_response
end end
end end
describe 'integration' do describe 'integration' do
it 'properly logs requests' do it 'properly logs requests', pending: 'Grape v0.12.0' do
expect(app).to receive(:call).with(env).and_return(app_response) expect(app).to receive(:call).with(env).and_return(app_response)
expect(subject.logger).to receive(:info).with('') expect(subject.logger).to receive(:info).with('')
expect(subject.logger).to receive(:info).with(%Q(Started POST "/api/1.0/users")) expect(subject.logger).to receive(:info).with(%Q(Started POST "/api/1.0/users"))