Merge pull request #32444 from matrinox/fix-return-response-mutation-rack-logger

Stop mutating body response
This commit is contained in:
Rafael Mendonça França 2018-04-04 18:17:01 -04:00
commit 826b17cd8a
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
2 changed files with 21 additions and 5 deletions

View File

@ -35,9 +35,9 @@ module Rails
instrumenter = ActiveSupport::Notifications.instrumenter
instrumenter.start "request.action_dispatch", request: request
logger.info { started_request_message(request) }
resp = @app.call(env)
resp[2] = ::Rack::BodyProxy.new(resp[2]) { finish(request) }
resp
status, headers, body = @app.call(env)
body = ::Rack::BodyProxy.new(body) { finish(request) }
[status, headers, body]
rescue Exception
finish(request)
raise

View File

@ -14,14 +14,21 @@ module Rails
attr_reader :logger
def initialize(logger = NULL, taggers = nil, &block)
super(->(_) { block.call; [200, {}, []] }, taggers)
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
@ -72,6 +79,15 @@ module Rails
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