2016-08-06 13:16:09 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/testing/autorun"
|
|
|
|
require "active_support/test_case"
|
|
|
|
require "rails/rack/logger"
|
|
|
|
require "logger"
|
2013-01-09 18:34:36 -05:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Rack
|
|
|
|
class LoggerTest < ActiveSupport::TestCase
|
|
|
|
class TestLogger < Rails::Rack::Logger
|
|
|
|
NULL = ::Logger.new File::NULL
|
|
|
|
|
|
|
|
attr_reader :logger
|
|
|
|
|
|
|
|
def initialize(logger = NULL, taggers = nil, &block)
|
|
|
|
super(->(_) { block.call; [200, {}, []] }, taggers)
|
|
|
|
@logger = logger
|
|
|
|
end
|
|
|
|
|
|
|
|
def development?; false; end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Subscriber < Struct.new(:starts, :finishes)
|
|
|
|
def initialize(starts = [], finishes = [])
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def start(name, id, payload)
|
|
|
|
starts << [name, id, payload]
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish(name, id, payload)
|
|
|
|
finishes << [name, id, payload]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :subscriber, :notifier
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@subscriber = Subscriber.new
|
|
|
|
@notifier = ActiveSupport::Notifications.notifier
|
2016-08-06 13:16:09 -04:00
|
|
|
@subscription = notifier.subscribe "request.action_dispatch", subscriber
|
2013-01-09 18:34:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2014-07-11 04:38:26 -04:00
|
|
|
notifier.unsubscribe @subscription
|
2013-01-09 18:34:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_notification
|
2016-08-16 03:30:11 -04:00
|
|
|
logger = TestLogger.new {}
|
2013-01-09 18:34:36 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_difference("subscriber.starts.length") do
|
|
|
|
assert_difference("subscriber.finishes.length") do
|
|
|
|
logger.call("REQUEST_METHOD" => "GET").last.close
|
2013-01-09 18:34:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_notification_on_raise
|
2013-08-06 11:03:29 -04:00
|
|
|
logger = TestLogger.new do
|
|
|
|
# using an exception class that is not a StandardError subclass on purpose
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2013-01-09 18:34:36 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_difference("subscriber.starts.length") do
|
|
|
|
assert_difference("subscriber.finishes.length") do
|
2013-08-06 11:03:29 -04:00
|
|
|
assert_raises(NotImplementedError) do
|
2016-08-06 13:16:09 -04:00
|
|
|
logger.call "REQUEST_METHOD" => "GET"
|
2013-01-09 18:34:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|