2015-05-06 13:22:23 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Grape::Middleware::Logger do
|
|
|
|
let(:app) { double('app') }
|
2015-12-12 11:55:05 +08:00
|
|
|
let(:options) { { filter: build(:param_filter), logger: Object.new } }
|
2015-05-06 13:22:23 +08:00
|
|
|
|
|
|
|
subject { described_class.new(app, options) }
|
|
|
|
|
2015-12-12 11:55:05 +08:00
|
|
|
let(:app_response) { build :app_response }
|
|
|
|
let(:grape_request) { build :grape_request }
|
|
|
|
let(:env) { build(:expected_env) }
|
2015-05-06 13:42:17 +08:00
|
|
|
|
2015-05-06 13:22:23 +08:00
|
|
|
describe '#call!' do
|
|
|
|
context 'when calling the app results in an error response' do
|
|
|
|
let(:error) { { status: 400 } }
|
|
|
|
|
|
|
|
it 'calls +after_failure+ and rethrows the error' do
|
|
|
|
expect(app).to receive(:call).with(env).and_throw(:error, error)
|
2015-05-06 13:42:17 +08:00
|
|
|
expect(subject).to receive(:before)
|
2015-05-06 13:22:23 +08:00
|
|
|
expect(subject).to receive(:after_failure).with(error)
|
|
|
|
expect(subject).to receive(:throw).with(:error, error)
|
|
|
|
subject.call!(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no error' do
|
|
|
|
it 'calls +after+ with the correct status' do
|
|
|
|
expect(app).to receive(:call).with(env).and_return(app_response)
|
2015-05-06 13:42:17 +08:00
|
|
|
expect(subject).to receive(:before)
|
2015-05-06 13:22:23 +08:00
|
|
|
expect(subject).to receive(:after).with(200)
|
|
|
|
subject.call!(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the @app_response' do
|
|
|
|
expect(app).to receive(:call).with(env).and_return(app_response)
|
2015-05-06 13:42:17 +08:00
|
|
|
allow(subject).to receive(:before)
|
2015-05-06 13:22:23 +08:00
|
|
|
allow(subject).to receive(:after)
|
|
|
|
expect(subject.call!(env)).to eq app_response
|
|
|
|
end
|
|
|
|
end
|
2015-11-18 18:56:52 +08:00
|
|
|
|
2015-11-18 19:29:46 +08:00
|
|
|
context 'when calling the app results in an array response' do
|
2015-11-18 21:15:09 +08:00
|
|
|
let(:app_response_array) { [401, {}, []] }
|
2015-11-18 18:56:52 +08:00
|
|
|
|
|
|
|
it 'calls +after+ with the correct status' do
|
|
|
|
expect(app).to receive(:call).with(env).and_return(app_response_array)
|
|
|
|
expect(subject).to receive(:before)
|
|
|
|
expect(subject).to receive(:after).with(401)
|
|
|
|
subject.call!(env)
|
|
|
|
end
|
|
|
|
|
2015-11-18 21:15:09 +08:00
|
|
|
it 'returns the @app_response' do
|
2015-11-18 18:56:52 +08:00
|
|
|
expect(app).to receive(:call).with(env).and_return(app_response_array)
|
|
|
|
allow(subject).to receive(:before)
|
|
|
|
allow(subject).to receive(:after)
|
|
|
|
expect(subject.call!(env)).to eq app_response_array
|
|
|
|
end
|
|
|
|
end
|
2015-05-06 13:22:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#after_failure' do
|
|
|
|
let(:error) { { status: 403 } }
|
|
|
|
|
|
|
|
it 'calls +after+ with the :status' do
|
|
|
|
expect(subject).to receive(:after).with(403)
|
|
|
|
subject.after_failure(error)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :message is set in the error object' do
|
|
|
|
let(:error) { { message: 'Oops, not found' } }
|
|
|
|
|
|
|
|
it 'logs the error message' do
|
|
|
|
allow(subject).to receive(:after)
|
|
|
|
expect(subject.logger).to receive(:info).with(Regexp.new(error[:message]))
|
|
|
|
subject.after_failure(error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#parameters' do
|
|
|
|
before { subject.instance_variable_set(:@env, env) }
|
|
|
|
|
|
|
|
context 'when @options[:filter] is set' do
|
|
|
|
it 'calls +filter+ with the raw parameters' do
|
|
|
|
expect(subject.options[:filter]).to receive(:filter).with({ "id" => '101001', "name" => "foo", "password" => "access" })
|
|
|
|
subject.parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the filtered results' do
|
|
|
|
expect(subject.parameters).to eq({ "id" => '101001', "name" => "foo", "password" => "[FILTERED]" })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when @options[:filter] is nil' do
|
|
|
|
let(:options) { {} }
|
|
|
|
|
|
|
|
it 'returns the params extracted out of @env' do
|
|
|
|
expect(subject.parameters).to eq({ "id" => '101001', "name" => "foo", "password" => "access" })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#logger' do
|
|
|
|
context 'when @options[:logger] is nil' do
|
|
|
|
let(:options) { {} }
|
|
|
|
|
2015-10-23 18:59:17 +08:00
|
|
|
context 'when Rails is defined' do
|
2015-12-09 08:54:45 +08:00
|
|
|
before do
|
|
|
|
module Rails
|
|
|
|
class << self
|
|
|
|
attr_accessor :logger
|
|
|
|
end
|
2015-10-23 18:59:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to the the standard Logger' do
|
|
|
|
expect(subject.logger).to be_a(Logger)
|
|
|
|
end
|
2015-10-23 01:38:51 +08:00
|
|
|
|
2015-12-12 08:34:20 +08:00
|
|
|
it 'logs each message in the correct format' do
|
|
|
|
expect(subject.logger.formatter.call('foo')).to eq "foo\n"
|
|
|
|
end
|
|
|
|
|
2015-10-23 18:59:17 +08:00
|
|
|
context 'when Rails.logger is defined' do
|
2015-12-09 08:54:45 +08:00
|
|
|
before do
|
|
|
|
Rails.logger = double('rails_logger')
|
|
|
|
end
|
2015-10-23 01:38:51 +08:00
|
|
|
|
2015-10-23 18:59:17 +08:00
|
|
|
it 'sets @logger to Rails.logger' do
|
|
|
|
expect(subject.logger).to be Rails.logger
|
|
|
|
end
|
2015-12-09 08:54:45 +08:00
|
|
|
|
|
|
|
after do
|
|
|
|
Rails.logger = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.send :remove_const, :Rails
|
2015-10-23 18:59:17 +08:00
|
|
|
end
|
2015-10-23 01:38:51 +08:00
|
|
|
end
|
2015-05-06 13:22:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when @options[:logger] is set' do
|
|
|
|
it 'returns the logger object' do
|
|
|
|
expect(subject.logger).to eq options[:logger]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|