提交 b0580175 编写于 作者: E eileencodes

Fix `make_response!` when called by `serve` in `RouteSet`

All of our tests were testing the `ActionController::Live` behavior in a
standalone environment, without going through the router or behaving
like a real application.

This resulted in `ActionController::Live` throwing the exception
`undefined method 'request' for #<ActionDispatch::Request:0x00000003ad1148>`
because `make_response!` was expecting a response instead of a request.

The expectation of a response came from `set_response!` in non-router
tests setting the response and passing it to `make_response!`. In the
case of an application we would hit `serve` in `RouteSet` first which
would send us to `make_response!` with a request sent instead of a
response.

The changes here remove `set_response!` so `make_response!` always
receives a request.

Thanks to KalabiYau for help with the investigation and solution.

Fixes #22524

[Eileen M. Uchitelle & KalabiYau]
上级 92a9744f
......@@ -36,8 +36,7 @@ module Live
extend ActiveSupport::Concern
module ClassMethods
def make_response!(response)
request = response.request
def make_response!(request)
if request.get_header("HTTP_VERSION") == "HTTP/1.0"
super
else
......@@ -287,9 +286,5 @@ def response_body=(body)
super
response.close if response
end
def set_response!(response)
@_response = self.class.make_response! response
end
end
end
......@@ -442,3 +442,42 @@ def test_nil_callback
end
end
end
class LiveStreamRouterTest < ActionDispatch::IntegrationTest
class TestController < ActionController::Base
include ActionController::Live
def index
response.headers['Content-Type'] = 'text/event-stream'
sse = SSE.new(response.stream)
sse.write("{\"name\":\"John\"}")
sse.write({ name: "Ryan" })
ensure
sse.close
end
end
def self.call(env)
routes.call(env)
end
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
routes.draw do
get '/test' => 'live_stream_router_test/test#index'
end
def app
self.class
end
test "streaming served through the router" do
get "/test"
assert_response :ok
assert_match(/data: {\"name\":\"John\"}/, response.body)
assert_match(/data: {\"name\":\"Ryan\"}/, response.body)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册