Skip to content
This repository
Browse code

Merge pull request #3739 from lest/config-always-write-cookie

configuration option to always write cookie
  • Loading branch information...
commit e6cfd853613f9d9e0232512146e40db4ee232a3c 2 parents 40b19e0 + 98a1717
José Valim josevalim authored
5 actionpack/lib/action_dispatch/middleware/cookies.rb
@@ -243,10 +243,13 @@ def recycle! #:nodoc:
243 243 @delete_cookies.clear
244 244 end
245 245
  246 + mattr_accessor :always_write_cookie
  247 + self.always_write_cookie = false
  248 +
246 249 private
247 250
248 251 def write_cookie?(cookie)
249   - @secure || !cookie[:secure] || defined?(Rails.env) && Rails.env.development?
  252 + @secure || !cookie[:secure] || always_write_cookie
250 253 end
251 254 end
252 255
4 actionpack/lib/action_dispatch/railtie.rb
@@ -10,10 +10,12 @@ class Railtie < Rails::Railtie
10 10 config.action_dispatch.tld_length = 1
11 11 config.action_dispatch.ignore_accept_header = false
12 12 config.action_dispatch.rack_cache = {:metastore => "rails:/", :entitystore => "rails:/", :verbose => true}
13   -
14 13 initializer "action_dispatch.configure" do |app|
15 14 ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length
16 15 ActionDispatch::Request.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
  16 +
  17 + config.action_dispatch.always_write_cookie = Rails.env.development? if config.action_dispatch.always_write_cookie.nil?
  18 + ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie
17 19 end
18 20 end
19 21 end
4 actionpack/test/dispatch/cookies_test.rb
@@ -210,8 +210,8 @@ def test_setting_cookie_with_secure
210 210 assert_equal({"user_name" => "david"}, @response.cookies)
211 211 end
212 212
213   - def test_setting_cookie_with_secure_in_development
214   - Rails.env.stubs(:development?).returns(true)
  213 + def test_setting_cookie_with_secure_when_always_write_cookie_is_true
  214 + ActionDispatch::Cookies::CookieJar.any_instance.stubs(:always_write_cookie).returns(true)
215 215 get :authenticate_with_secure
216 216 assert_cookie_header "user_name=david; path=/; secure"
217 217 assert_equal({"user_name" => "david"}, @response.cookies)
47 railties/test/application/middleware/cookies_test.rb
... ... @@ -0,0 +1,47 @@
  1 +require 'isolation/abstract_unit'
  2 +
  3 +module ApplicationTests
  4 + class CookiesTest < Test::Unit::TestCase
  5 + include ActiveSupport::Testing::Isolation
  6 +
  7 + def new_app
  8 + File.expand_path("#{app_path}/../new_app")
  9 + end
  10 +
  11 + def setup
  12 + build_app
  13 + boot_rails
  14 + FileUtils.rm_rf("#{app_path}/config/environments")
  15 + end
  16 +
  17 + def teardown
  18 + teardown_app
  19 + FileUtils.rm_rf(new_app) if File.directory?(new_app)
  20 + end
  21 +
  22 + test 'always_write_cookie is true by default in development' do
  23 + require 'rails'
  24 + Rails.env = 'development'
  25 + require "#{app_path}/config/environment"
  26 + assert_equal true, ActionDispatch::Cookies::CookieJar.always_write_cookie
  27 + end
  28 +
  29 + test 'always_write_cookie is false by default in production' do
  30 + require 'rails'
  31 + Rails.env = 'production'
  32 + require "#{app_path}/config/environment"
  33 + assert_equal false, ActionDispatch::Cookies::CookieJar.always_write_cookie
  34 + end
  35 +
  36 + test 'always_write_cookie can be overrided' do
  37 + add_to_config <<-RUBY
  38 + config.action_dispatch.always_write_cookie = false
  39 + RUBY
  40 +
  41 + require 'rails'
  42 + Rails.env = 'development'
  43 + require "#{app_path}/config/environment"
  44 + assert_equal false, ActionDispatch::Cookies::CookieJar.always_write_cookie
  45 + end
  46 + end
  47 +end

0 comments on commit e6cfd85

Please sign in to comment.
Something went wrong with that request. Please try again.