Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6514: caching_patch_tests.rb

File caching_patch_tests.rb, 5.1 kB (added by RSL, 2 years ago)

Unit tests for my patch's changes

Line 
1 require "fileutils"
2 require File.dirname(__FILE__) + "/../abstract_unit"
3
4 CACHE_DIR = 'test_cache'
5 # Don't change '/../temp/' cavalierly or you might hoze something you don't want hozed
6 FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
7 ActionController::Base.perform_caching = true
8
9 class MakeBelieveController < ActionController::Base
10   caches_page :index, :moved, :target
11  
12   def index
13     @var = "before render"
14     render :text => "index"
15     @var = "after render"
16   end
17  
18   def moved
19     redirect_to :action => "target"
20   end
21  
22   def target
23     render :text => "target"
24   end
25 end
26
27 class PageCachingFixOneTest < Test::Unit::TestCase
28   def setup
29     @controller = MakeBelieveController.new
30     @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
31     ActionController::Routing::Routes.draw do |map|
32       map.index "/index", :controller => "make_believe", :action => "index"
33     end
34     FileUtils.mkdir_p(FILE_STORE_PATH)
35   end
36  
37   def teardown
38     FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
39   end
40  
41   def test_index_and_reasons_for_failure
42     get :index
43     # Woops! Looks like there's something wrong.
44     assert_response :error
45     # Because @var == "after render", the problem cannot be inside the method
46     # but with something that occurs after it.
47     assert_equal assigns["var"], "after render"
48     # I figured that must be the after_filter generated by caches_page.
49     # I did some poking around and discovered that the class variable
50     # ActionController::Base.page_cache_directory doesn't get set in the testing
51     # environment because RAILS_ROOT isn't set there either. I'm throwing these next
52     # assertions in to back me up.
53     assert !defined?(RAILS_ROOT)
54     # @@page_cache_directory is a class variable of ActionController::Base,
55     # to my surprise.
56     assert_equal ActionController::Base.page_cache_directory, ""
57   end
58 end
59
60 # Let's try this again!
61 class PageCachingFixTwoTest < Test::Unit::TestCase
62   def setup
63     @controller = MakeBelieveController.new
64     @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
65     ActionController::Routing::Routes.draw do |map|
66       map.index "/index", :controller => "make_believe", :action => "index"
67       map.redirect_me "/moved", :controller => "make_believe", :action => "moved"
68       map.target "/target", :controller => "make_believe", :action => "target"
69     end
70     FileUtils.mkdir_p(FILE_STORE_PATH)
71     # This time, let's make sure to set the page_cache_directory.
72     ActionController::Base.page_cache_directory = FILE_STORE_PATH
73   end
74  
75   def teardown
76     FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
77   end
78  
79   def test_index
80     get :index
81     # No errors this time.
82     assert_response :success
83     # Is the file there though? Yep.
84     assert File.exists?(FILE_STORE_PATH + "/index.html")
85   end
86  
87   def test_target
88     get :target
89     assert_response :success
90     assert File.exists?(FILE_STORE_PATH + "/target.html")
91   end
92  
93   # Now let's see what happens with a redirect.
94   def test_moved
95     get :moved
96     assert_response :redirect
97     # The page still gets cached and every subsequent request for that URI will
98     # return the content of /moved.html instead of performing a redirect.
99     # This is not actually the case in the test environment [Surprise!] but it is
100     # in the production environment.
101     assert File.exists?(FILE_STORE_PATH + "/moved.html")
102   end
103 end
104
105 class PageCachingFixThreeTest < Test::Unit::TestCase
106   def setup
107     @controller = MakeBelieveController.new
108     @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
109     ActionController::Routing::Routes.draw do |map|
110       map.index "/index", :controller => "make_believe", :action => "index"
111       map.redirect_me "/moved", :controller => "make_believe", :action => "moved"
112       map.target "/target", :controller => "make_believe", :action => "target"
113     end
114     FileUtils.mkdir_p(FILE_STORE_PATH)
115     # Still make sure to set the page_cache_directory.
116     ActionController::Base.page_cache_directory = FILE_STORE_PATH
117     # And also change that we redefine caching_allowed
118     ActionController::Caching::Pages.module_eval { def caching_allowed(); !request.post? && response.headers['Status'] && response.headers['Status'].to_i == 200; end}
119   end
120  
121   def teardown
122     # Restore that bad method because you never know who is going to be called first!
123     ActionController::Caching::Pages.module_eval { def caching_allowed(); !request.post? && response.headers['Status'] && response.headers['Status'].to_i < 400; end}
124     FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
125   end
126  
127   def test_index
128     get :index
129     # No errors this time.
130     assert_response :success
131     # Is the file there though? Yep.
132     assert File.exists?(FILE_STORE_PATH + "/index.html")
133   end
134  
135   def test_target
136     get :target
137     assert_response :success
138     assert File.exists?(FILE_STORE_PATH + "/target.html")
139   end
140  
141   # This time it doesn't cache the file! Hooray!
142   def test_moved
143     get :moved
144     assert_response :redirect
145     assert !File.exists?(FILE_STORE_PATH + "/moved.html")
146   end
147 end