1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/test/rack_logger_test.rb
Geoff Lee 0ac64470ea
Stop mutating body response
If @app.call returns an object that is saved (for e.g., in a constant), the mutation results in a continuing cycle of wrapping the body in Rack::BodyProxy, eventually leading to SystemStackError
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
On branch fix-return-response-mutation-rack-logger - Tue  3 Apr 2018 19:54:28 PDT by Geoff Lee <geoff.lee@lendesk.com>
2018-04-03 19:54:27 -07:00

93 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require "abstract_unit"
require "active_support/testing/autorun"
require "active_support/test_case"
require "rails/rack/logger"
require "logger"
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, app: nil, taggers: nil, &block)
app ||= ->(_) { block.call; [200, {}, []] }
super(app, taggers)
@logger = logger
end
def development?; false; end
end
class TestApp < Struct.new(:response)
def call(_env)
response
end
end
Subscriber = Struct.new(:starts, :finishes) do
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
@subscription = notifier.subscribe "request.action_dispatch", subscriber
end
def teardown
notifier.unsubscribe @subscription
end
def test_notification
logger = TestLogger.new {}
assert_difference("subscriber.starts.length") do
assert_difference("subscriber.finishes.length") do
logger.call("REQUEST_METHOD" => "GET").last.close
end
end
end
def test_notification_on_raise
logger = TestLogger.new do
# using an exception class that is not a StandardError subclass on purpose
raise NotImplementedError
end
assert_difference("subscriber.starts.length") do
assert_difference("subscriber.finishes.length") do
assert_raises(NotImplementedError) do
logger.call "REQUEST_METHOD" => "GET"
end
end
end
end
def test_logger_does_not_mutate_app_return
response = []
app = TestApp.new(response)
logger = TestLogger.new(app: app)
assert_no_changes('response') do
logger.call('REQUEST_METHOD' => 'GET')
end
end
end
end
end