mitio-erubis_rails_helper 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/README.rdoc +66 -0
  2. data/lib/erubis_rails_helper.rb +20 -0
  3. data/lib/erubis_rails_helper/erubis/generator.rb +100 -0
  4. data/lib/erubis_rails_helper/template_handlers/erubis.rb +260 -0
  5. data/test/abstract_unit.rb +51 -0
  6. data/test/controller/action_pack_assertions_test.rb +516 -0
  7. data/test/controller/addresses_render_test.rb +37 -0
  8. data/test/controller/assert_select_test.rb +734 -0
  9. data/test/controller/base_test.rb +217 -0
  10. data/test/controller/benchmark_test.rb +32 -0
  11. data/test/controller/caching_test.rb +690 -0
  12. data/test/controller/capture_test.rb +66 -0
  13. data/test/controller/content_type_test.rb +168 -0
  14. data/test/controller/controller_fixtures/app/controllers/admin/user_controller.rb +0 -0
  15. data/test/controller/controller_fixtures/app/controllers/user_controller.rb +0 -0
  16. data/test/controller/controller_fixtures/vendor/plugins/bad_plugin/lib/plugin_controller.rb +0 -0
  17. data/test/controller/cookie_test.rb +111 -0
  18. data/test/controller/deprecation/deprecated_base_methods_test.rb +32 -0
  19. data/test/controller/dispatcher_test.rb +103 -0
  20. data/test/controller/fake_controllers.rb +33 -0
  21. data/test/controller/fake_models.rb +19 -0
  22. data/test/controller/filter_params_test.rb +49 -0
  23. data/test/controller/filters_test.rb +885 -0
  24. data/test/controller/flash_test.rb +142 -0
  25. data/test/controller/header_test.rb +14 -0
  26. data/test/controller/helper_test.rb +224 -0
  27. data/test/controller/html-scanner/cdata_node_test.rb +15 -0
  28. data/test/controller/html-scanner/document_test.rb +148 -0
  29. data/test/controller/html-scanner/node_test.rb +89 -0
  30. data/test/controller/html-scanner/sanitizer_test.rb +273 -0
  31. data/test/controller/html-scanner/tag_node_test.rb +238 -0
  32. data/test/controller/html-scanner/text_node_test.rb +50 -0
  33. data/test/controller/html-scanner/tokenizer_test.rb +131 -0
  34. data/test/controller/http_basic_authentication_test.rb +88 -0
  35. data/test/controller/http_digest_authentication_test.rb +178 -0
  36. data/test/controller/integration_test.rb +417 -0
  37. data/test/controller/layout_test.rb +204 -0
  38. data/test/controller/logging_test.rb +46 -0
  39. data/test/controller/middleware_stack_test.rb +90 -0
  40. data/test/controller/mime_responds_test.rb +536 -0
  41. data/test/controller/mime_type_test.rb +93 -0
  42. data/test/controller/polymorphic_routes_test.rb +293 -0
  43. data/test/controller/rack_test.rb +294 -0
  44. data/test/controller/record_identifier_test.rb +139 -0
  45. data/test/controller/redirect_test.rb +282 -0
  46. data/test/controller/render_test.rb +1752 -0
  47. data/test/controller/request/json_params_parsing_test.rb +45 -0
  48. data/test/controller/request/multipart_params_parsing_test.rb +223 -0
  49. data/test/controller/request/query_string_parsing_test.rb +120 -0
  50. data/test/controller/request/url_encoded_params_parsing_test.rb +184 -0
  51. data/test/controller/request/xml_params_parsing_test.rb +88 -0
  52. data/test/controller/request_forgery_protection_test.rb +246 -0
  53. data/test/controller/request_test.rb +407 -0
  54. data/test/controller/rescue_test.rb +536 -0
  55. data/test/controller/resources_test.rb +1341 -0
  56. data/test/controller/routing_test.rb +2557 -0
  57. data/test/controller/selector_test.rb +628 -0
  58. data/test/controller/send_file_test.rb +161 -0
  59. data/test/controller/session/cookie_store_test.rb +239 -0
  60. data/test/controller/session/mem_cache_store_test.rb +127 -0
  61. data/test/controller/session/test_session_test.rb +58 -0
  62. data/test/controller/test_test.rb +692 -0
  63. data/test/controller/translation_test.rb +26 -0
  64. data/test/controller/url_rewriter_test.rb +359 -0
  65. data/test/controller/verification_test.rb +270 -0
  66. data/test/controller/view_paths_test.rb +141 -0
  67. data/test/controller/webservice_test.rb +260 -0
  68. data/test/fixtures/alternate_helpers/foo_helper.rb +3 -0
  69. data/test/fixtures/company.rb +10 -0
  70. data/test/fixtures/developer.rb +9 -0
  71. data/test/fixtures/helpers/abc_helper.rb +5 -0
  72. data/test/fixtures/helpers/fun/games_helper.rb +3 -0
  73. data/test/fixtures/helpers/fun/pdf_helper.rb +3 -0
  74. data/test/fixtures/mascot.rb +3 -0
  75. data/test/fixtures/project.rb +3 -0
  76. data/test/fixtures/reply.rb +7 -0
  77. data/test/fixtures/topic.rb +3 -0
  78. data/test/template/asset_tag_helper_test.rb +713 -0
  79. data/test/template/atom_feed_helper_test.rb +286 -0
  80. data/test/template/benchmark_helper_test.rb +86 -0
  81. data/test/template/compiled_templates_test.rb +203 -0
  82. data/test/template/date_helper_i18n_test.rb +120 -0
  83. data/test/template/date_helper_test.rb +2469 -0
  84. data/test/template/erb_util_test.rb +24 -0
  85. data/test/template/form_helper_test.rb +1176 -0
  86. data/test/template/form_options_helper_test.rb +751 -0
  87. data/test/template/form_tag_helper_test.rb +327 -0
  88. data/test/template/javascript_helper_test.rb +106 -0
  89. data/test/template/number_helper_i18n_test.rb +69 -0
  90. data/test/template/number_helper_test.rb +128 -0
  91. data/test/template/prototype_helper_test.rb +639 -0
  92. data/test/template/record_tag_helper_test.rb +58 -0
  93. data/test/template/render_test.rb +291 -0
  94. data/test/template/sanitize_helper_test.rb +48 -0
  95. data/test/template/scriptaculous_helper_test.rb +90 -0
  96. data/test/template/tag_helper_test.rb +97 -0
  97. data/test/template/test_test.rb +54 -0
  98. data/test/template/text_helper_test.rb +520 -0
  99. data/test/template/translation_helper_test.rb +32 -0
  100. data/test/template/url_helper_test.rb +614 -0
  101. data/test/test_erubis_rails_helper.rb +4 -0
  102. data/test/testing_sandbox.rb +15 -0
  103. data/test/view/test_case_test.rb +8 -0
  104. metadata +298 -0
@@ -0,0 +1,217 @@
1
+ require 'abstract_unit'
2
+ require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
3
+
4
+ # Provide some controller to run the tests on.
5
+ module Submodule
6
+ class ContainedEmptyController < ActionController::Base
7
+ end
8
+ class ContainedNonEmptyController < ActionController::Base
9
+ def public_action
10
+ render :nothing => true
11
+ end
12
+
13
+ hide_action :hidden_action
14
+ def hidden_action
15
+ raise "Noooo!"
16
+ end
17
+
18
+ def another_hidden_action
19
+ end
20
+ hide_action :another_hidden_action
21
+ end
22
+ class SubclassedController < ContainedNonEmptyController
23
+ hide_action :public_action # Hiding it here should not affect the superclass.
24
+ end
25
+ end
26
+ class EmptyController < ActionController::Base
27
+ end
28
+ class NonEmptyController < ActionController::Base
29
+ def public_action
30
+ end
31
+
32
+ hide_action :hidden_action
33
+ def hidden_action
34
+ end
35
+ end
36
+
37
+ class MethodMissingController < ActionController::Base
38
+
39
+ hide_action :shouldnt_be_called
40
+ def shouldnt_be_called
41
+ raise "NO WAY!"
42
+ end
43
+
44
+ protected
45
+
46
+ def method_missing(selector)
47
+ render :text => selector.to_s
48
+ end
49
+
50
+ end
51
+
52
+ class DefaultUrlOptionsController < ActionController::Base
53
+ def default_url_options_action
54
+ end
55
+
56
+ def default_url_options(options = nil)
57
+ { :host => 'www.override.com', :action => 'new', :bacon => 'chunky' }
58
+ end
59
+ end
60
+
61
+ class ControllerClassTests < Test::Unit::TestCase
62
+ def test_controller_path
63
+ assert_equal 'empty', EmptyController.controller_path
64
+ assert_equal EmptyController.controller_path, EmptyController.new.controller_path
65
+ assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
66
+ assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
67
+ end
68
+ def test_controller_name
69
+ assert_equal 'empty', EmptyController.controller_name
70
+ assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
71
+ end
72
+ end
73
+
74
+ class ControllerInstanceTests < Test::Unit::TestCase
75
+ def setup
76
+ @empty = EmptyController.new
77
+ @contained = Submodule::ContainedEmptyController.new
78
+ @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
79
+
80
+ @non_empty_controllers = [NonEmptyController.new,
81
+ Submodule::ContainedNonEmptyController.new]
82
+ end
83
+
84
+ def test_action_methods
85
+ @empty_controllers.each do |c|
86
+ hide_mocha_methods_from_controller(c)
87
+ assert_equal Set.new, c.__send__(:action_methods), "#{c.controller_path} should be empty!"
88
+ end
89
+ @non_empty_controllers.each do |c|
90
+ hide_mocha_methods_from_controller(c)
91
+ assert_equal Set.new(%w(public_action)), c.__send__(:action_methods), "#{c.controller_path} should not be empty!"
92
+ end
93
+ end
94
+
95
+ protected
96
+ # Mocha adds some public instance methods to Object that would be
97
+ # considered actions, so explicitly hide_action them.
98
+ def hide_mocha_methods_from_controller(controller)
99
+ mocha_methods = [
100
+ :expects, :mocha, :mocha_inspect, :reset_mocha, :stubba_object,
101
+ :stubba_method, :stubs, :verify, :__metaclass__, :__is_a__, :to_matcher,
102
+ ]
103
+ controller.class.__send__(:hide_action, *mocha_methods)
104
+ end
105
+ end
106
+
107
+
108
+ class PerformActionTest < ActionController::TestCase
109
+ class MockLogger
110
+ attr_reader :logged
111
+
112
+ def initialize
113
+ @logged = []
114
+ end
115
+
116
+ def method_missing(method, *args)
117
+ @logged << args.first
118
+ end
119
+ end
120
+
121
+ def use_controller(controller_class)
122
+ @controller = controller_class.new
123
+
124
+ # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
125
+ # a more accurate simulation of what happens in "real life".
126
+ @controller.logger = Logger.new(nil)
127
+
128
+ @request = ActionController::TestRequest.new
129
+ @response = ActionController::TestResponse.new
130
+
131
+ @request.host = "www.nextangle.com"
132
+
133
+ rescue_action_in_public!
134
+ end
135
+
136
+ def test_get_on_priv_should_show_selector
137
+ use_controller MethodMissingController
138
+ get :shouldnt_be_called
139
+ assert_response :success
140
+ assert_equal 'shouldnt_be_called', @response.body
141
+ end
142
+
143
+ def test_method_missing_is_not_an_action_name
144
+ use_controller MethodMissingController
145
+ assert ! @controller.__send__(:action_methods).include?('method_missing')
146
+
147
+ get :method_missing
148
+ assert_response :success
149
+ assert_equal 'method_missing', @response.body
150
+ end
151
+
152
+ def test_get_on_hidden_should_fail
153
+ use_controller NonEmptyController
154
+ get :hidden_action
155
+ assert_response 404
156
+
157
+ get :another_hidden_action
158
+ assert_response 404
159
+ end
160
+
161
+ def test_namespaced_action_should_log_module_name
162
+ use_controller Submodule::ContainedNonEmptyController
163
+ @controller.logger = MockLogger.new
164
+ get :public_action
165
+ assert_match /Processing\sSubmodule::ContainedNonEmptyController#public_action/, @controller.logger.logged[1]
166
+ end
167
+ end
168
+
169
+ class DefaultUrlOptionsTest < ActionController::TestCase
170
+ tests DefaultUrlOptionsController
171
+
172
+ def setup
173
+ @request.host = 'www.example.com'
174
+ rescue_action_in_public!
175
+ end
176
+
177
+ def test_default_url_options_are_used_if_set
178
+ ActionController::Routing::Routes.draw do |map|
179
+ map.default_url_options 'default_url_options', :controller => 'default_url_options'
180
+ map.connect ':controller/:action/:id'
181
+ end
182
+
183
+ get :default_url_options_action # Make a dummy request so that the controller is initialized properly.
184
+
185
+ assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options')
186
+ assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url)
187
+ ensure
188
+ ActionController::Routing::Routes.load!
189
+ end
190
+ end
191
+
192
+ class EmptyUrlOptionsTest < ActionController::TestCase
193
+ tests NonEmptyController
194
+
195
+ def setup
196
+ @request.host = 'www.example.com'
197
+ rescue_action_in_public!
198
+ end
199
+
200
+ def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
201
+ get :public_action
202
+ assert_equal "http://www.example.com/non_empty/public_action", @controller.url_for
203
+ end
204
+ end
205
+
206
+ class EnsureNamedRoutesWorksTicket22BugTest < Test::Unit::TestCase
207
+ def test_named_routes_still_work
208
+ ActionController::Routing::Routes.draw do |map|
209
+ map.resources :things
210
+ end
211
+ EmptyController.send :include, ActionController::UrlWriter
212
+
213
+ assert_equal '/things', EmptyController.new.send(:things_path)
214
+ ensure
215
+ ActionController::Routing::Routes.load!
216
+ end
217
+ end
@@ -0,0 +1,32 @@
1
+ require 'abstract_unit'
2
+
3
+ # Provide some static controllers.
4
+ class BenchmarkedController < ActionController::Base
5
+ def public_action
6
+ render :nothing => true
7
+ end
8
+
9
+ def rescue_action(e)
10
+ raise e
11
+ end
12
+ end
13
+
14
+ class BenchmarkTest < ActionController::TestCase
15
+ tests BenchmarkedController
16
+
17
+ class MockLogger
18
+ def method_missing(*args)
19
+ end
20
+ end
21
+
22
+ def setup
23
+ # benchmark doesn't do anything unless a logger is set
24
+ @controller.logger = MockLogger.new
25
+ @request.host = "test.actioncontroller.i"
26
+ end
27
+
28
+ def test_with_http_1_0_request
29
+ @request.host = nil
30
+ assert_nothing_raised { get :public_action }
31
+ end
32
+ end
@@ -0,0 +1,690 @@
1
+ require 'fileutils'
2
+ require 'abstract_unit'
3
+
4
+ CACHE_DIR = 'test_cache'
5
+ # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
6
+ FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
7
+ ActionController::Base.page_cache_directory = FILE_STORE_PATH
8
+ ActionController::Base.cache_store = :file_store, FILE_STORE_PATH
9
+
10
+ class PageCachingTestController < ActionController::Base
11
+ caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? }
12
+ caches_page :found, :not_found
13
+
14
+ def ok
15
+ head :ok
16
+ end
17
+
18
+ def no_content
19
+ head :no_content
20
+ end
21
+
22
+ def found
23
+ redirect_to :action => 'ok'
24
+ end
25
+
26
+ def not_found
27
+ head :not_found
28
+ end
29
+
30
+ def custom_path
31
+ render :text => "Super soaker"
32
+ cache_page("Super soaker", "/index.html")
33
+ end
34
+
35
+ def expire_custom_path
36
+ expire_page("/index.html")
37
+ head :ok
38
+ end
39
+
40
+ def trailing_slash
41
+ render :text => "Sneak attack"
42
+ end
43
+ end
44
+
45
+ class PageCachingTest < ActionController::TestCase
46
+ def setup
47
+ ActionController::Base.perform_caching = true
48
+
49
+ ActionController::Routing::Routes.draw do |map|
50
+ map.main '', :controller => 'posts'
51
+ map.formatted_posts 'posts.:format', :controller => 'posts'
52
+ map.resources :posts
53
+ map.connect ':controller/:action/:id'
54
+ end
55
+
56
+ @request = ActionController::TestRequest.new
57
+ @request.host = 'hostname.com'
58
+
59
+ @response = ActionController::TestResponse.new
60
+ @controller = PageCachingTestController.new
61
+
62
+ @params = {:controller => 'posts', :action => 'index', :only_path => true, :skip_relative_url_root => true}
63
+ @rewriter = ActionController::UrlRewriter.new(@request, @params)
64
+
65
+ FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
66
+ FileUtils.mkdir_p(FILE_STORE_PATH)
67
+ end
68
+
69
+ def teardown
70
+ FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
71
+ ActionController::Routing::Routes.clear!
72
+ ActionController::Base.perform_caching = false
73
+ end
74
+
75
+ def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route
76
+ @params[:format] = 'rss'
77
+ assert_equal '/posts.rss', @rewriter.rewrite(@params)
78
+ @params[:format] = nil
79
+ assert_equal '/', @rewriter.rewrite(@params)
80
+ end
81
+
82
+ def test_should_cache_get_with_ok_status
83
+ get :ok
84
+ assert_response :ok
85
+ assert_page_cached :ok, "get with ok status should have been cached"
86
+ end
87
+
88
+ def test_should_cache_with_custom_path
89
+ get :custom_path
90
+ assert File.exist?("#{FILE_STORE_PATH}/index.html")
91
+ end
92
+
93
+ def test_should_expire_cache_with_custom_path
94
+ get :custom_path
95
+ assert File.exist?("#{FILE_STORE_PATH}/index.html")
96
+
97
+ get :expire_custom_path
98
+ assert !File.exist?("#{FILE_STORE_PATH}/index.html")
99
+ end
100
+
101
+ def test_should_cache_without_trailing_slash_on_url
102
+ @controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash'
103
+ assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html")
104
+ end
105
+
106
+ def test_should_cache_with_trailing_slash_on_url
107
+ @controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash/'
108
+ assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html")
109
+ end
110
+
111
+ def test_should_cache_ok_at_custom_path
112
+ @request.stubs(:path).returns("/index.html")
113
+ get :ok
114
+ assert_response :ok
115
+ assert File.exist?("#{FILE_STORE_PATH}/index.html")
116
+ end
117
+
118
+ [:ok, :no_content, :found, :not_found].each do |status|
119
+ [:get, :post, :put, :delete].each do |method|
120
+ unless method == :get and status == :ok
121
+ define_method "test_shouldnt_cache_#{method}_with_#{status}_status" do
122
+ send(method, status)
123
+ assert_response status
124
+ assert_page_not_cached status, "#{method} with #{status} status shouldn't have been cached"
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ def test_page_caching_conditional_options
131
+ get :ok, :format=>'json'
132
+ assert_page_not_cached :ok
133
+ end
134
+
135
+ private
136
+ def assert_page_cached(action, message = "#{action} should have been cached")
137
+ assert page_cached?(action), message
138
+ end
139
+
140
+ def assert_page_not_cached(action, message = "#{action} shouldn't have been cached")
141
+ assert !page_cached?(action), message
142
+ end
143
+
144
+ def page_cached?(action)
145
+ File.exist? "#{FILE_STORE_PATH}/page_caching_test/#{action}.html"
146
+ end
147
+ end
148
+
149
+ class ActionCachingTestController < ActionController::Base
150
+ caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour
151
+ caches_action :show, :cache_path => 'http://test.host/custom/show'
152
+ caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
153
+ caches_action :with_layout
154
+ caches_action :layout_false, :layout => false
155
+
156
+ layout 'talk_from_action.erb'
157
+
158
+ def index
159
+ @cache_this = MockTime.now.to_f.to_s
160
+ render :text => @cache_this
161
+ end
162
+
163
+ def redirected
164
+ redirect_to :action => 'index'
165
+ end
166
+
167
+ def forbidden
168
+ render :text => "Forbidden"
169
+ response.status = "403 Forbidden"
170
+ end
171
+
172
+ def with_layout
173
+ @cache_this = MockTime.now.to_f.to_s
174
+ render :text => @cache_this, :layout => true
175
+ end
176
+
177
+ alias_method :show, :index
178
+ alias_method :edit, :index
179
+ alias_method :destroy, :index
180
+ alias_method :layout_false, :with_layout
181
+
182
+ def expire
183
+ expire_action :controller => 'action_caching_test', :action => 'index'
184
+ render :nothing => true
185
+ end
186
+
187
+ def expire_xml
188
+ expire_action :controller => 'action_caching_test', :action => 'index', :format => 'xml'
189
+ render :nothing => true
190
+ end
191
+ end
192
+
193
+ class MockTime < Time
194
+ # Let Time spicy to assure that Time.now != Time.now
195
+ def to_f
196
+ super+rand
197
+ end
198
+ end
199
+
200
+ class ActionCachingMockController
201
+ attr_accessor :mock_url_for
202
+ attr_accessor :mock_path
203
+
204
+ def initialize
205
+ yield self if block_given?
206
+ end
207
+
208
+ def url_for(*args)
209
+ @mock_url_for
210
+ end
211
+
212
+ def request
213
+ mocked_path = @mock_path
214
+ Object.new.instance_eval(<<-EVAL)
215
+ def path; '#{@mock_path}' end
216
+ def format; 'all' end
217
+ def cache_format; nil end
218
+ self
219
+ EVAL
220
+ end
221
+ end
222
+
223
+ class ActionCacheTest < ActionController::TestCase
224
+ def setup
225
+ reset!
226
+ FileUtils.mkdir_p(FILE_STORE_PATH)
227
+ @path_class = ActionController::Caching::Actions::ActionCachePath
228
+ @mock_controller = ActionCachingMockController.new
229
+ end
230
+
231
+ def teardown
232
+ FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
233
+ end
234
+
235
+ def test_simple_action_cache
236
+ get :index
237
+ cached_time = content_to_cache
238
+ assert_equal cached_time, @response.body
239
+ assert fragment_exist?('hostname.com/action_caching_test')
240
+ reset!
241
+
242
+ get :index
243
+ assert_equal cached_time, @response.body
244
+ end
245
+
246
+ def test_simple_action_not_cached
247
+ get :destroy
248
+ cached_time = content_to_cache
249
+ assert_equal cached_time, @response.body
250
+ assert !fragment_exist?('hostname.com/action_caching_test/destroy')
251
+ reset!
252
+
253
+ get :destroy
254
+ assert_not_equal cached_time, @response.body
255
+ end
256
+
257
+ def test_action_cache_with_layout
258
+ get :with_layout
259
+ cached_time = content_to_cache
260
+ assert_not_equal cached_time, @response.body
261
+ assert fragment_exist?('hostname.com/action_caching_test/with_layout')
262
+ reset!
263
+
264
+ get :with_layout
265
+ assert_not_equal cached_time, @response.body
266
+
267
+ assert_equal @response.body, read_fragment('hostname.com/action_caching_test/with_layout')
268
+ end
269
+
270
+ def test_action_cache_with_layout_and_layout_cache_false
271
+ get :layout_false
272
+ cached_time = content_to_cache
273
+ assert_not_equal cached_time, @response.body
274
+ assert fragment_exist?('hostname.com/action_caching_test/layout_false')
275
+ reset!
276
+
277
+ get :layout_false
278
+ assert_not_equal cached_time, @response.body
279
+
280
+ assert_equal cached_time, read_fragment('hostname.com/action_caching_test/layout_false')
281
+ end
282
+
283
+ def test_action_cache_conditional_options
284
+ old_use_accept_header = ActionController::Base.use_accept_header
285
+ ActionController::Base.use_accept_header = true
286
+ @request.env['HTTP_ACCEPT'] = 'application/json'
287
+ get :index
288
+ assert !fragment_exist?('hostname.com/action_caching_test')
289
+ ActionController::Base.use_accept_header = old_use_accept_header
290
+ end
291
+
292
+ def test_action_cache_with_store_options
293
+ MockTime.expects(:now).returns(12345).once
294
+ @controller.expects(:read_fragment).with('hostname.com/action_caching_test', :expires_in => 1.hour).once
295
+ @controller.expects(:write_fragment).with('hostname.com/action_caching_test', '12345.0', :expires_in => 1.hour).once
296
+ get :index
297
+ end
298
+
299
+ def test_action_cache_with_custom_cache_path
300
+ get :show
301
+ cached_time = content_to_cache
302
+ assert_equal cached_time, @response.body
303
+ assert fragment_exist?('test.host/custom/show')
304
+ reset!
305
+
306
+ get :show
307
+ assert_equal cached_time, @response.body
308
+ end
309
+
310
+ def test_action_cache_with_custom_cache_path_in_block
311
+ get :edit
312
+ assert fragment_exist?('test.host/edit')
313
+ reset!
314
+
315
+ get :edit, :id => 1
316
+ assert fragment_exist?('test.host/1;edit')
317
+ end
318
+
319
+ def test_cache_expiration
320
+ get :index
321
+ cached_time = content_to_cache
322
+ reset!
323
+
324
+ get :index
325
+ assert_equal cached_time, @response.body
326
+ reset!
327
+
328
+ get :expire
329
+ reset!
330
+
331
+ get :index
332
+ new_cached_time = content_to_cache
333
+ assert_not_equal cached_time, @response.body
334
+ reset!
335
+
336
+ get :index
337
+ assert_response :success
338
+ assert_equal new_cached_time, @response.body
339
+ end
340
+
341
+ def test_cache_expiration_isnt_affected_by_request_format
342
+ get :index
343
+ cached_time = content_to_cache
344
+ reset!
345
+
346
+ @request.set_REQUEST_URI "/action_caching_test/expire.xml"
347
+ get :expire, :format => :xml
348
+ reset!
349
+
350
+ get :index
351
+ new_cached_time = content_to_cache
352
+ assert_not_equal cached_time, @response.body
353
+ end
354
+
355
+ def test_cache_is_scoped_by_subdomain
356
+ @request.host = 'jamis.hostname.com'
357
+ get :index
358
+ jamis_cache = content_to_cache
359
+
360
+ reset!
361
+
362
+ @request.host = 'david.hostname.com'
363
+ get :index
364
+ david_cache = content_to_cache
365
+ assert_not_equal jamis_cache, @response.body
366
+
367
+ reset!
368
+
369
+ @request.host = 'jamis.hostname.com'
370
+ get :index
371
+ assert_equal jamis_cache, @response.body
372
+
373
+ reset!
374
+
375
+ @request.host = 'david.hostname.com'
376
+ get :index
377
+ assert_equal david_cache, @response.body
378
+ end
379
+
380
+ def test_redirect_is_not_cached
381
+ get :redirected
382
+ assert_response :redirect
383
+ reset!
384
+
385
+ get :redirected
386
+ assert_response :redirect
387
+ end
388
+
389
+ def test_forbidden_is_not_cached
390
+ get :forbidden
391
+ assert_response :forbidden
392
+ reset!
393
+
394
+ get :forbidden
395
+ assert_response :forbidden
396
+ end
397
+
398
+ def test_xml_version_of_resource_is_treated_as_different_cache
399
+ with_routing do |set|
400
+ set.draw do |map|
401
+ map.connect ':controller/:action.:format'
402
+ map.connect ':controller/:action'
403
+ end
404
+
405
+ get :index, :format => 'xml'
406
+ cached_time = content_to_cache
407
+ assert_equal cached_time, @response.body
408
+ assert fragment_exist?('hostname.com/action_caching_test/index.xml')
409
+ reset!
410
+
411
+ get :index, :format => 'xml'
412
+ assert_equal cached_time, @response.body
413
+ assert_equal 'application/xml', @response.content_type
414
+ reset!
415
+
416
+ get :expire_xml
417
+ reset!
418
+
419
+ get :index, :format => 'xml'
420
+ assert_not_equal cached_time, @response.body
421
+ end
422
+ end
423
+
424
+ def test_correct_content_type_is_returned_for_cache_hit
425
+ # run it twice to cache it the first time
426
+ get :index, :id => 'content-type.xml'
427
+ get :index, :id => 'content-type.xml'
428
+ assert_equal 'application/xml', @response.content_type
429
+ end
430
+
431
+ def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_key
432
+ # run it twice to cache it the first time
433
+ get :show, :format => 'xml'
434
+ get :show, :format => 'xml'
435
+ assert_equal 'application/xml', @response.content_type
436
+ end
437
+
438
+ def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_key_from_proc
439
+ # run it twice to cache it the first time
440
+ get :edit, :id => 1, :format => 'xml'
441
+ get :edit, :id => 1, :format => 'xml'
442
+ assert_equal 'application/xml', @response.content_type
443
+ end
444
+
445
+ def test_empty_path_is_normalized
446
+ @mock_controller.mock_url_for = 'http://example.org/'
447
+ @mock_controller.mock_path = '/'
448
+
449
+ assert_equal 'example.org/index', @path_class.path_for(@mock_controller, {})
450
+ end
451
+
452
+ def test_file_extensions
453
+ get :index, :id => 'kitten.jpg'
454
+ get :index, :id => 'kitten.jpg'
455
+
456
+ assert_response :success
457
+ end
458
+
459
+ private
460
+ def content_to_cache
461
+ assigns(:cache_this)
462
+ end
463
+
464
+ def reset!
465
+ @request = ActionController::TestRequest.new
466
+ @response = ActionController::TestResponse.new
467
+ @controller = ActionCachingTestController.new
468
+ @request.host = 'hostname.com'
469
+ end
470
+
471
+ def fragment_exist?(path)
472
+ @controller.fragment_exist?(path)
473
+ end
474
+
475
+ def read_fragment(path)
476
+ @controller.read_fragment(path)
477
+ end
478
+ end
479
+
480
+ class FragmentCachingTestController < ActionController::Base
481
+ def some_action; end;
482
+ end
483
+
484
+ class FragmentCachingTest < ActionController::TestCase
485
+ def setup
486
+ ActionController::Base.perform_caching = true
487
+ @store = ActiveSupport::Cache::MemoryStore.new
488
+ ActionController::Base.cache_store = @store
489
+ @controller = FragmentCachingTestController.new
490
+ @params = {:controller => 'posts', :action => 'index'}
491
+ @request = ActionController::TestRequest.new
492
+ @response = ActionController::TestResponse.new
493
+ @controller.params = @params
494
+ @controller.request = @request
495
+ @controller.response = @response
496
+ @controller.send(:initialize_current_url)
497
+ @controller.send(:initialize_template_class, @response)
498
+ @controller.send(:assign_shortcuts, @request, @response)
499
+ end
500
+
501
+ def test_fragment_cache_key
502
+ assert_equal 'views/what a key', @controller.fragment_cache_key('what a key')
503
+ assert_equal "views/test.host/fragment_caching_test/some_action",
504
+ @controller.fragment_cache_key(:controller => 'fragment_caching_test',:action => 'some_action')
505
+ end
506
+
507
+ def test_read_fragment_with_caching_enabled
508
+ @store.write('views/name', 'value')
509
+ assert_equal 'value', @controller.read_fragment('name')
510
+ end
511
+
512
+ def test_read_fragment_with_caching_disabled
513
+ ActionController::Base.perform_caching = false
514
+ @store.write('views/name', 'value')
515
+ assert_nil @controller.read_fragment('name')
516
+ end
517
+
518
+ def test_fragment_exist_with_caching_enabled
519
+ @store.write('views/name', 'value')
520
+ assert @controller.fragment_exist?('name')
521
+ assert !@controller.fragment_exist?('other_name')
522
+ end
523
+
524
+ def test_fragment_exist_with_caching_disabled
525
+ ActionController::Base.perform_caching = false
526
+ @store.write('views/name', 'value')
527
+ assert !@controller.fragment_exist?('name')
528
+ assert !@controller.fragment_exist?('other_name')
529
+ end
530
+
531
+ def test_write_fragment_with_caching_enabled
532
+ assert_nil @store.read('views/name')
533
+ assert_equal 'value', @controller.write_fragment('name', 'value')
534
+ assert_equal 'value', @store.read('views/name')
535
+ end
536
+
537
+ def test_write_fragment_with_caching_disabled
538
+ assert_nil @store.read('views/name')
539
+ ActionController::Base.perform_caching = false
540
+ assert_equal 'value', @controller.write_fragment('name', 'value')
541
+ assert_nil @store.read('views/name')
542
+ end
543
+
544
+ def test_expire_fragment_with_simple_key
545
+ @store.write('views/name', 'value')
546
+ @controller.expire_fragment 'name'
547
+ assert_nil @store.read('views/name')
548
+ end
549
+
550
+ def test_expire_fragment_with_regexp
551
+ @store.write('views/name', 'value')
552
+ @store.write('views/another_name', 'another_value')
553
+ @store.write('views/primalgrasp', 'will not expire ;-)')
554
+
555
+ @controller.expire_fragment /name/
556
+
557
+ assert_nil @store.read('views/name')
558
+ assert_nil @store.read('views/another_name')
559
+ assert_equal 'will not expire ;-)', @store.read('views/primalgrasp')
560
+ end
561
+
562
+ def test_fragment_for_with_disabled_caching
563
+ ActionController::Base.perform_caching = false
564
+
565
+ @store.write('views/expensive', 'fragment content')
566
+ fragment_computed = false
567
+
568
+ buffer = 'generated till now -> '
569
+ @controller.fragment_for(buffer, 'expensive') { fragment_computed = true }
570
+
571
+ assert fragment_computed
572
+ assert_equal 'generated till now -> ', buffer
573
+ end
574
+
575
+ def test_fragment_for
576
+ @store.write('views/expensive', 'fragment content')
577
+ fragment_computed = false
578
+
579
+ buffer = 'generated till now -> '
580
+ @controller.fragment_for(buffer, 'expensive') { fragment_computed = true }
581
+
582
+ assert !fragment_computed
583
+ assert_equal 'generated till now -> fragment content', buffer
584
+ end
585
+ end
586
+
587
+ class FunctionalCachingController < ActionController::Base
588
+ def fragment_cached
589
+ end
590
+
591
+ def html_fragment_cached_with_partial
592
+ respond_to do |format|
593
+ format.html
594
+ end
595
+ end
596
+
597
+ def js_fragment_cached_with_partial
598
+ respond_to do |format|
599
+ format.js
600
+ end
601
+ end
602
+
603
+ def formatted_fragment_cached
604
+ respond_to do |format|
605
+ format.html
606
+ format.xml
607
+ format.js
608
+ end
609
+ end
610
+
611
+ def rescue_action(e)
612
+ raise e
613
+ end
614
+ end
615
+
616
+ class FunctionalFragmentCachingTest < ActionController::TestCase
617
+ def setup
618
+ ActionController::Base.perform_caching = true
619
+ @store = ActiveSupport::Cache::MemoryStore.new
620
+ ActionController::Base.cache_store = @store
621
+ @controller = FunctionalCachingController.new
622
+ @request = ActionController::TestRequest.new
623
+ @response = ActionController::TestResponse.new
624
+ end
625
+
626
+ def test_fragment_caching
627
+ get :fragment_cached
628
+ assert_response :success
629
+ expected_body = <<-CACHED
630
+ Hello
631
+ This bit's fragment cached
632
+ CACHED
633
+ assert_equal expected_body, @response.body
634
+
635
+ assert_equal "This bit's fragment cached", @store.read('views/test.host/functional_caching/fragment_cached')
636
+ end
637
+
638
+ def test_fragment_caching_in_partials
639
+ get :html_fragment_cached_with_partial
640
+ assert_response :success
641
+ assert_match /Fragment caching in a partial/, @response.body
642
+ assert_match "Fragment caching in a partial", @store.read('views/test.host/functional_caching/html_fragment_cached_with_partial')
643
+ end
644
+
645
+ def test_render_inline_before_fragment_caching
646
+ get :inline_fragment_cached
647
+ assert_response :success
648
+ assert_match /Some inline content/, @response.body
649
+ assert_match /Some cached content/, @response.body
650
+ assert_match "Some cached content", @store.read('views/test.host/functional_caching/inline_fragment_cached')
651
+ end
652
+
653
+ def test_fragment_caching_in_rjs_partials
654
+ xhr :get, :js_fragment_cached_with_partial
655
+ assert_response :success
656
+ assert_match /Fragment caching in a partial/, @response.body
657
+ assert_match "Fragment caching in a partial", @store.read('views/test.host/functional_caching/js_fragment_cached_with_partial')
658
+ end
659
+
660
+ def test_html_formatted_fragment_caching
661
+ get :formatted_fragment_cached, :format => "html"
662
+ assert_response :success
663
+ expected_body = "<body>\n<p>ERB</p>\n</body>"
664
+
665
+ assert_equal expected_body, @response.body
666
+
667
+ assert_equal "<p>ERB</p>", @store.read('views/test.host/functional_caching/formatted_fragment_cached')
668
+ end
669
+
670
+ def test_xml_formatted_fragment_caching
671
+ get :formatted_fragment_cached, :format => "xml"
672
+ assert_response :success
673
+ expected_body = "<body>\n <p>Builder</p>\n</body>\n"
674
+
675
+ assert_equal expected_body, @response.body
676
+
677
+ assert_equal " <p>Builder</p>\n", @store.read('views/test.host/functional_caching/formatted_fragment_cached')
678
+ end
679
+
680
+ def test_js_formatted_fragment_caching
681
+ get :formatted_fragment_cached, :format => "js"
682
+ assert_response :success
683
+ expected_body = %(title = "Hey";\n$("element_1").visualEffect("highlight");\n) +
684
+ %($("element_2").visualEffect("highlight");\nfooter = "Bye";)
685
+ assert_equal expected_body, @response.body
686
+
687
+ assert_equal ['$("element_1").visualEffect("highlight");', '$("element_2").visualEffect("highlight");'],
688
+ @store.read('views/test.host/functional_caching/formatted_fragment_cached')
689
+ end
690
+ end