2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
|
2018-04-03 22:54:27 -04:00
|
|
|
def initialize(logger = NULL, app: nil, taggers: nil, &block)
|
|
|
|
app ||= ->(_) { block.call; [200, {}, []] }
|
|
|
|
super(app, taggers)
|
2013-01-09 18:34:36 -05:00
|
|
|
@logger = logger
|
|
|
|
end
|
|
|
|
|
|
|
|
def development?; false; end
|
|
|
|
end
|
|
|
|
|
2018-04-03 22:54:27 -04:00
|
|
|
class TestApp < Struct.new(:response)
|
|
|
|
def call(_env)
|
|
|
|
response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-13 01:09:56 -05:00
|
|
|
Subscriber = Struct.new(:starts, :finishes) do
|
2013-01-09 18:34:36 -05:00
|
|
|
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
|
2018-09-25 13:18:20 -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
|
2018-04-03 22:54:27 -04:00
|
|
|
|
|
|
|
def test_logger_does_not_mutate_app_return
|
2018-04-06 06:50:15 -04:00
|
|
|
response = [].freeze
|
2018-04-03 22:54:27 -04:00
|
|
|
app = TestApp.new(response)
|
|
|
|
logger = TestLogger.new(app: app)
|
2018-04-04 18:17:01 -04:00
|
|
|
assert_no_changes("response") do
|
2018-04-06 06:50:15 -04:00
|
|
|
assert_nothing_raised do
|
|
|
|
logger.call("REQUEST_METHOD" => "GET")
|
|
|
|
end
|
2018-04-03 22:54:27 -04:00
|
|
|
end
|
|
|
|
end
|
2013-01-09 18:34:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|