| 1 |
require "fileutils" |
|---|
| 2 |
require File.dirname(__FILE__) + "/../abstract_unit" |
|---|
| 3 |
|
|---|
| 4 |
CACHE_DIR = 'test_cache' |
|---|
| 5 |
|
|---|
| 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 |
|
|---|
| 44 |
assert_response :error |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
assert_equal assigns["var"], "after render" |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
assert !defined?(RAILS_ROOT) |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
assert_equal ActionController::Base.page_cache_directory, "" |
|---|
| 57 |
end |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 82 |
assert_response :success |
|---|
| 83 |
|
|---|
| 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 |
|
|---|
| 94 |
def test_moved |
|---|
| 95 |
get :moved |
|---|
| 96 |
assert_response :redirect |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 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 |
|
|---|
| 116 |
ActionController::Base.page_cache_directory = FILE_STORE_PATH |
|---|
| 117 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 130 |
assert_response :success |
|---|
| 131 |
|
|---|
| 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 |
|
|---|
| 142 |
def test_moved |
|---|
| 143 |
get :moved |
|---|
| 144 |
assert_response :redirect |
|---|
| 145 |
assert !File.exists?(FILE_STORE_PATH + "/moved.html") |
|---|
| 146 |
end |
|---|
| 147 |
end |
|---|