Merge pull request #42216 from gmcgibbon/set_options_in_default_session

Set session options when initializing a basic session
This commit is contained in:
Gannon McGibbon 2021-06-02 12:30:21 -04:00 committed by GitHub
commit b9f48d5d58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 1 deletions

View File

@ -1,3 +1,7 @@
* Set session options when initializing a basic session.
*Gannon McGibbon*
* Add `cache_control: {}` option to `fresh_when` and `stale?`
Works as a shortcut to set `response.cache_control` with the above methods.

View File

@ -25,7 +25,9 @@ module ActionDispatch
end
def self.disabled(req)
new(nil, req, enabled: false)
new(nil, req, enabled: false).tap do
Session::Options.set(req, Session::Options.new(nil, { id: nil }))
end
end
def self.find(req)

View File

@ -1381,3 +1381,17 @@ class RequestInspectTest < BaseRequestTest
assert_match %r(#<ActionDispatch::Request POST "https://example.com/path/\?q=1" for 1.2.3.4>), request.inspect
end
end
class RequestSession < BaseRequestTest
def setup
super
@request = stub_request
end
test "#session" do
@request.session
assert_not_predicate(ActionDispatch::Request::Session.find(@request), :enabled?)
assert_instance_of(ActionDispatch::Request::Session::Options, ActionDispatch::Request::Session::Options.find(@request))
end
end

View File

@ -385,5 +385,36 @@ module ApplicationTests
require "#{app_path}/config/environment"
assert app.config.session_options[:cookie_only], "Expected cookie_only to be set to true"
end
test "session uses default options if previous sessions exist" do
add_to_config <<-RUBY
config.api_only = true
config.session_store :cookie_store, key: "_random_key"
config.middleware.use ActionDispatch::Cookies
config.middleware.use config.session_store, config.session_options
config.active_record.database_selector = { delay: 2.seconds }
config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
RUBY
controller :test, <<-RUBY
class TestController < ApplicationController
def test_action
head :ok
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
get "/test_action" => "test#test_action"
end
RUBY
require "#{app_path}/config/environment"
get "/test_action"
assert_equal 200, last_response.status
end
end
end