diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index e43ebaad2c..6f50b9fd2e 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -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. diff --git a/actionpack/lib/action_dispatch/request/session.rb b/actionpack/lib/action_dispatch/request/session.rb index ea00d77fab..8d070b42c9 100644 --- a/actionpack/lib/action_dispatch/request/session.rb +++ b/actionpack/lib/action_dispatch/request/session.rb @@ -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) diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index cab42c3cd9..bed024f00a 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -1381,3 +1381,17 @@ class RequestInspectTest < BaseRequestTest assert_match %r(#), 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 diff --git a/railties/test/application/middleware/session_test.rb b/railties/test/application/middleware/session_test.rb index bcfda062f4..c97fcd03b3 100644 --- a/railties/test/application/middleware/session_test.rb +++ b/railties/test/application/middleware/session_test.rb @@ -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