Skip to content

Commit

Permalink
Just reading flash messages should not create a session if one does n…
Browse files Browse the repository at this point in the history
…ot exist yet.
  • Loading branch information
josevalim committed Jun 25, 2010
1 parent 6682cce commit a12b76b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
3 changes: 3 additions & 0 deletions actionpack/lib/action_dispatch/http/request.rb
Expand Up @@ -194,9 +194,12 @@ def body_stream #:nodoc:
@env['rack.input']
end

# TODO This should be broken apart into AD::Request::Session and probably
# be included by the session middleware.
def reset_session
session.destroy if session
self.session = {}
@env['action_dispatch.request.flash_hash'] = nil
end

def session=(session) #:nodoc:
Expand Down
11 changes: 9 additions & 2 deletions actionpack/lib/action_dispatch/middleware/flash.rb
Expand Up @@ -4,7 +4,7 @@ class Request
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
# to put a new one.
def flash
session['flash'] ||= Flash::FlashHash.new
@env['action_dispatch.request.flash_hash'] ||= (session["flash"] || Flash::FlashHash.new)
end
end

Expand Down Expand Up @@ -176,7 +176,14 @@ def call(env)

@app.call(env)
ensure
if (session = env['rack.session']) && session.key?('flash') && session['flash'].empty?
session = env['rack.session'] || {}
flash_hash = env['action_dispatch.request.flash_hash']

if flash_hash && (!flash_hash.empty? || session.key?('flash'))
session["flash"] = flash_hash
end

if session.key?('flash') && session['flash'].empty?
session.delete('flash')
end
end
Expand Down
20 changes: 17 additions & 3 deletions actionpack/test/controller/flash_test.rb
Expand Up @@ -236,6 +236,15 @@ def test_flash
end
end

def test_just_using_flash_does_not_stream_a_cookie_back
with_test_route_set do
get '/use_flash'
assert_response :success
assert_nil @response.headers["Set-Cookie"]
assert_equal "flash: ", @response.body
end
end

private

# Overwrite get to send SessionSecret in env hash
Expand All @@ -247,10 +256,15 @@ def get(path, parameters = nil, env = {})
def with_test_route_set
with_routing do |set|
set.draw do |map|
match ':action', :to => ActionDispatch::Session::CookieStore.new(
FlashIntegrationTest::TestController, :key => FlashIntegrationTest::SessionKey, :secret => FlashIntegrationTest::SessionSecret
)
match ':action', :to => FlashIntegrationTest::TestController
end

@app = self.class.build_app(set) do |middleware|
middleware.use ActionDispatch::Session::CookieStore, :key => SessionKey
middleware.use ActionDispatch::Flash
middleware.delete "ActionDispatch::ShowExceptions"
end

yield
end
end
Expand Down

0 comments on commit a12b76b

Please sign in to comment.