mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix cookie access in integration tests with other host names
This commit is contained in:
parent
d270da569e
commit
570c54c39a
2 changed files with 66 additions and 7 deletions
|
@ -137,7 +137,10 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
# The hostname used in the last request.
|
# The hostname used in the last request.
|
||||||
attr_accessor :host
|
def host
|
||||||
|
@host || DEFAULT_HOST
|
||||||
|
end
|
||||||
|
attr_writer :host
|
||||||
|
|
||||||
# The remote_addr used in the last request.
|
# The remote_addr used in the last request.
|
||||||
attr_accessor :remote_addr
|
attr_accessor :remote_addr
|
||||||
|
@ -148,7 +151,7 @@ module ActionDispatch
|
||||||
# A map of the cookies returned by the last response, and which will be
|
# A map of the cookies returned by the last response, and which will be
|
||||||
# sent with the next request.
|
# sent with the next request.
|
||||||
def cookies
|
def cookies
|
||||||
@mock_session.cookie_jar
|
_mock_session.cookie_jar
|
||||||
end
|
end
|
||||||
|
|
||||||
# A reference to the controller instance used by the last request.
|
# A reference to the controller instance used by the last request.
|
||||||
|
@ -189,8 +192,8 @@ module ActionDispatch
|
||||||
# session.reset!
|
# session.reset!
|
||||||
def reset!
|
def reset!
|
||||||
@https = false
|
@https = false
|
||||||
@mock_session = Rack::MockSession.new(@app, DEFAULT_HOST)
|
|
||||||
@controller = @request = @response = nil
|
@controller = @request = @response = nil
|
||||||
|
@_mock_session = nil
|
||||||
@request_count = 0
|
@request_count = 0
|
||||||
|
|
||||||
self.host = DEFAULT_HOST
|
self.host = DEFAULT_HOST
|
||||||
|
@ -234,6 +237,9 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def _mock_session
|
||||||
|
@_mock_session ||= Rack::MockSession.new(@app, host)
|
||||||
|
end
|
||||||
|
|
||||||
# Performs the actual request.
|
# Performs the actual request.
|
||||||
def process(method, path, parameters = nil, rack_environment = nil)
|
def process(method, path, parameters = nil, rack_environment = nil)
|
||||||
|
@ -254,7 +260,7 @@ module ActionDispatch
|
||||||
:method => method,
|
:method => method,
|
||||||
:params => parameters,
|
:params => parameters,
|
||||||
|
|
||||||
"SERVER_NAME" => host,
|
"SERVER_NAME" => host.split(':')[0],
|
||||||
"SERVER_PORT" => (https? ? "443" : "80"),
|
"SERVER_PORT" => (https? ? "443" : "80"),
|
||||||
"HTTPS" => https? ? "on" : "off",
|
"HTTPS" => https? ? "on" : "off",
|
||||||
"rack.url_scheme" => https? ? "https" : "http",
|
"rack.url_scheme" => https? ? "https" : "http",
|
||||||
|
@ -266,17 +272,25 @@ module ActionDispatch
|
||||||
"HTTP_ACCEPT" => accept
|
"HTTP_ACCEPT" => accept
|
||||||
}
|
}
|
||||||
|
|
||||||
session = Rack::Test::Session.new(@mock_session)
|
session = Rack::Test::Session.new(_mock_session)
|
||||||
|
|
||||||
(rack_environment || {}).each do |key, value|
|
(rack_environment || {}).each do |key, value|
|
||||||
env[key] = value
|
env[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
session.request(path, env)
|
# NOTE: rack-test v0.5 doesn't build a default uri correctly
|
||||||
|
# Make sure requested path is always a full uri
|
||||||
|
uri = URI.parse('/')
|
||||||
|
uri.scheme ||= env['rack.url_scheme']
|
||||||
|
uri.host ||= env['SERVER_NAME']
|
||||||
|
uri.port ||= env['SERVER_PORT'].try(:to_i)
|
||||||
|
uri += path
|
||||||
|
|
||||||
|
session.request(uri.to_s, env)
|
||||||
|
|
||||||
@request_count += 1
|
@request_count += 1
|
||||||
@request = ActionDispatch::Request.new(session.last_request.env)
|
@request = ActionDispatch::Request.new(session.last_request.env)
|
||||||
response = @mock_session.last_response
|
response = _mock_session.last_response
|
||||||
@response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
|
@response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
|
||||||
@html_document = nil
|
@html_document = nil
|
||||||
|
|
||||||
|
|
|
@ -245,6 +245,15 @@ class IntegrationProcessTest < ActionController::IntegrationTest
|
||||||
render :text => "Gone", :status => 410
|
render :text => "Gone", :status => 410
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_cookie
|
||||||
|
cookies["foo"] = 'bar'
|
||||||
|
head :ok
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_cookie
|
||||||
|
render :text => cookies["foo"]
|
||||||
|
end
|
||||||
|
|
||||||
def redirect
|
def redirect
|
||||||
redirect_to :action => "get"
|
redirect_to :action => "get"
|
||||||
end
|
end
|
||||||
|
@ -292,6 +301,42 @@ class IntegrationProcessTest < ActionController::IntegrationTest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'cookie persist to next request' do
|
||||||
|
with_test_route_set do
|
||||||
|
get '/set_cookie'
|
||||||
|
assert_response :success
|
||||||
|
|
||||||
|
assert_equal "foo=bar; path=/", headers["Set-Cookie"]
|
||||||
|
assert_equal({"foo"=>"bar"}, cookies.to_hash)
|
||||||
|
|
||||||
|
get '/get_cookie'
|
||||||
|
assert_response :success
|
||||||
|
assert_equal "bar", body
|
||||||
|
|
||||||
|
assert_equal nil, headers["Set-Cookie"]
|
||||||
|
assert_equal({"foo"=>"bar"}, cookies.to_hash)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'cookie persist to next request on another domain' do
|
||||||
|
with_test_route_set do
|
||||||
|
host! "37s.backpack.test"
|
||||||
|
|
||||||
|
get '/set_cookie'
|
||||||
|
assert_response :success
|
||||||
|
|
||||||
|
assert_equal "foo=bar; path=/", headers["Set-Cookie"]
|
||||||
|
assert_equal({"foo"=>"bar"}, cookies.to_hash)
|
||||||
|
|
||||||
|
get '/get_cookie'
|
||||||
|
assert_response :success
|
||||||
|
assert_equal "bar", body
|
||||||
|
|
||||||
|
assert_equal nil, headers["Set-Cookie"]
|
||||||
|
assert_equal({"foo"=>"bar"}, cookies.to_hash)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_redirect
|
def test_redirect
|
||||||
with_test_route_set do
|
with_test_route_set do
|
||||||
get '/redirect'
|
get '/redirect'
|
||||||
|
|
Loading…
Reference in a new issue