grape-middleware-logger/spec/lib/grape/middleware/logger_spec.rb

151 lines
4.4 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe Grape::Middleware::Logger do
let(:app) { double('app') }
let(:options) { { filter: build(:param_filter), logger: Object.new } }
subject { described_class.new(app, options) }
let(:app_response) { build :app_response }
let(:grape_request) { build :grape_request }
let(:env) { build(:expected_env) }
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)
expect(subject).to receive(:before)
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)
expect(subject).to receive(:before)
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)
allow(subject).to receive(:before)
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
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
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
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) { {} }
context 'when Rails is defined' do
2015-12-09 08:54:45 +08:00
before do
module Rails
class << self
attr_accessor :logger
end
end
end
it 'defaults to the the standard Logger' do
expect(subject.logger).to be_a(Logger)
end
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
context 'when Rails.logger is defined' do
2015-12-09 08:54:45 +08:00
before do
Rails.logger = double('rails_logger')
end
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
end
end
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