Skip to content
Browse files

extracted patch on rails fork to a plugin

  • Loading branch information...
0 parents commit a9b55e974510cd83cc8a542ab73e0df30f51042d Pedro Belo committed Mar 11, 2009
Showing with 84 additions and 0 deletions.
  1. +7 −0 Rakefile
  2. +1 −0 init.rb
  3. +24 −0 lib/caches_page_via_http.rb
  4. +52 −0 test/caches_page_via_http_spec.rb
7 Rakefile
@@ -0,0 +1,7 @@
+require 'load_multi_rails_rake_tasks'
+require 'rake'
+
+task :default => :test
+task :test do
+ ruby "test/caches_page_via_http_spec.rb"
+end
1 init.rb
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + '/lib/caches_page_via_http'
24 lib/caches_page_via_http.rb
@@ -0,0 +1,24 @@
+module ActionController
+ module Caching
+ module Pages
+ module ClassMethods
+ def expire_page(path)
+ # NOOP
+ end
+
+ def cache_page(content, path)
+ # NOOP
+ end
+
+ def caches_page(*actions)
+ return unless perform_caching
+ options = actions.respond_to?(:extract_options!) ? actions.extract_options! : {}
+ after_filter({:only => actions}.merge(options)) do |c|
+ c.response.headers['Cache-Control'] = 'public; max-age=360'
+ c.response.headers.delete('cookie') # caching the page means no cookies
+ end
+ end
+ end
+ end
+ end
+end
52 test/caches_page_via_http_spec.rb
@@ -0,0 +1,52 @@
+require 'rubygems'
+require 'multi_rails_init'
+
+require 'test/unit'
+require 'mocha'
+require 'action_controller'
+
+require File.dirname(__FILE__) + '/../lib/caches_page_via_http'
+
+ActionController::Routing::Routes.draw do |map|
+ map.connect ':controller/:action/:id'
+end
+
+class TestController < ActionController::Base
+ caches_page :index
+ def index
+ render :text => 'ok'
+ end
+ def nocache
+ render :text => 'ok'
+ end
+end
+
+class CachesPageTest < Test::Unit::TestCase
+ def setup
+ @controller = TestController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ FileUtils.expects(:makedirs).never
+ File.expects(:open).never
+ File.expects(:delete).never
+ end
+
+ def test_not_crashing
+ get 'index'
+ assert_equal(200, @response.headers['Status'].to_i)
+ assert_equal('ok', @response.body)
+ end
+
+ def test_cache_control_header
+ get 'index'
+ assert_equal('public; max-age=360', @response.headers['Cache-Control'])
+ end
+
+ def test_doesnt_affect_actions_not_cached
+ get 'nocache'
+ assert_equal(200, @response.headers['Status'].to_i)
+ assert_equal('ok', @response.body)
+ assert ['no-cache', 'private, max-age=0, must-revalidate'].include?(@response.headers['Cache-Control'])
+ end
+end

0 comments on commit a9b55e9

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