Permalink
Browse files

Freeze cookies when streaming.

Also freeze session and flash if using the cookie store.
  • Loading branch information...
1 parent 294ad66 commit 816dfd05956bb944045a54ae04dcc81aa41d46c6 @oggy committed Apr 19, 2011
Showing with 115 additions and 0 deletions.
  1. +20 −0 lib/template_streaming.rb
  2. +95 −0 spec/template_streaming_spec.rb
@@ -50,6 +50,12 @@ def render_with_template_streaming(*args, &block)
@template.render_progressively = true
@performed_render = true
@streaming_body = StreamingBody.new(progressive_rendering_threshold) do
+ cookies.freeze
+ if self.class.session_store.sent_with_headers?
+ session.freeze
+ flash.freeze
+ end
+
@performed_render = false
last_piece = render_without_template_streaming(*args, &block)
# The original render will clobber our response.body, so
@@ -336,10 +342,24 @@ def padding(length)
end
end
+ module AbstractSessionStoreExtension
+ def sent_with_headers?
+ false
+ end
+ end
+
+ module CookieSessionStoreExtension
+ def sent_with_headers?
+ true
+ end
+ end
+
ActionView::Base.send :include, View
ActionController::Base.send :include, Controller
ActionController::Response.send :include, Response
ActionController::Dispatcher.middleware.insert 0, Rack::Chunked
+ ActionController::Session::AbstractStore.extend AbstractSessionStoreExtension
+ ActionController::Session::CookieStore.extend CookieSessionStoreExtension
end
# Please let there be a better way to do this...
@@ -576,6 +576,20 @@
end
end
+ class BlackHoleSessionStore < ActionController::Session::AbstractStore
+ def get_session(env, sid)
+ ['id', {}]
+ end
+
+ def set_session(env, sid, data)
+ true
+ end
+
+ def destroy(env)
+ true
+ end
+ end
+
describe "#flash" do
describe "when rendering progressively" do
it "should behave correctly when referenced in the controller" do
@@ -613,6 +627,22 @@
run('HTTP_COOKIE' => session_cookie)
received.should == chunks('()', :end => true)
end
+
+ it "should be frozen in the view if the session is sent with the headers" do
+ view "<% data.frozen = flash.frozen? %>"
+ action { render :progressive => true }
+ run
+ data.frozen.should be_true
+ end
+
+ it "should not be frozen in the view if the session is not sent with the headers" do
+ with_attribute_value ActionController::Base, :session_store, BlackHoleSessionStore do
+ view "<% data.frozen = flash.frozen? %>"
+ action { render :progressive => true }
+ run
+ data.frozen.should be_false
+ end
+ end
end
describe "when not rendering progressively" do
@@ -651,6 +681,13 @@
run('HTTP_COOKIE' => session_cookie)
received.should == '()'
end
+
+ it "should not be frozen in the view" do
+ view "<% data.frozen = flash.frozen? %>"
+ action { render :progressive => false }
+ run
+ data.frozen.should be_false
+ end
end
describe "#flash.now" do
@@ -716,6 +753,64 @@
end
end
+ describe "#cookies" do
+ describe "when rendering progressively" do
+ it "should be frozen in the view" do
+ view "<% data.frozen = cookies.frozen? %>"
+ action { render :progressive => true }
+ run
+ data.frozen.should be_true
+ end
+
+ it "should be frozen in the view irrespective of session store" do
+ with_attribute_value ActionController::Base, :session_store, BlackHoleSessionStore do
+ view "<% data.frozen = cookies.frozen? %>"
+ action { render :progressive => true }
+ run
+ data.frozen.should be_true
+ end
+ end
+ end
+
+ describe "when not rendering progressively" do
+ it "should not be frozen in the view" do
+ view "<% data.frozen = session.frozen? %>"
+ action { render :progressive => false }
+ run
+ data.frozen.should be_false
+ end
+ end
+ end
+
+ describe "#session" do
+ describe "when rendering progressively" do
+ it "should be frozen in the view if the session is sent with the headers" do
+ view "<% data.frozen = session.frozen? %>"
+ action { render :progressive => true }
+ run
+ data.frozen.should be_true
+ end
+
+ it "should not be frozen in the view if the session is not sent with the headers" do
+ with_attribute_value ActionController::Base, :session_store, BlackHoleSessionStore do
+ view "<% data.frozen = session.frozen? %>"
+ action { render :progressive => true }
+ run
+ data.frozen.should be_false
+ end
+ end
+ end
+
+ describe "when not rendering progressively" do
+ it "should not be frozen in the view" do
+ view "<% data.frozen = session.frozen? %>"
+ action { render :progressive => false }
+ run
+ data.frozen.should be_false
+ end
+ end
+ end
+
describe "#form_authenticity_token" do
describe "when rendering progressively" do
it "should match what is in the session when referenced in the controller" do

0 comments on commit 816dfd0

Please sign in to comment.