actionpack-action_caching 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
- gemspec path: '..'
3
+ gemspec path: ".."
4
4
 
5
- gem 'rails', github: 'rails/rails', branch: '4-0-stable'
5
+ gem "rails", github: "rails/rails", branch: "4-0-stable"
6
+ gem "mime-types", "< 3"
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec path: ".."
4
+
5
+ gem "rails", github: "rails/rails", branch: "4-1-stable"
6
+ gem "mime-types", "< 3"
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec path: ".."
4
+
5
+ gem "rails", github: "rails/rails", branch: "4-2-stable"
6
+ gem "mime-types", "< 3"
7
+
8
+ if RUBY_VERSION < "2.1"
9
+ gem "nokogiri", "< 1.7"
10
+ end
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec path: ".."
4
+
5
+ gem "rails", github: "rails/rails", branch: "5-0-stable"
@@ -1,5 +1,6 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
- gemspec path: '..'
3
+ gemspec path: ".."
4
4
 
5
- gem 'rails', github: 'rails/rails', branch: 'master'
5
+ gem "rails", github: "rails/rails", branch: "master"
6
+ gem "arel", github: "rails/arel", branch: "master"
@@ -1,4 +1,4 @@
1
- require 'action_controller/caching/actions'
1
+ require "action_controller/caching/actions"
2
2
 
3
3
  module ActionController
4
4
  module Caching
@@ -1,4 +1,4 @@
1
- require 'set'
1
+ require "set"
2
2
 
3
3
  module ActionController
4
4
  module Caching
@@ -10,7 +10,7 @@ module ActionController
10
10
  # to execute such action.
11
11
  #
12
12
  # class ListsController < ApplicationController
13
- # before_filter :authenticate, except: :public
13
+ # before_action :authenticate, except: :public
14
14
  #
15
15
  # caches_page :public
16
16
  # caches_action :index, :show
@@ -35,8 +35,8 @@ module ActionController
35
35
  # <tt>http://david.example.com/lists.xml</tt>
36
36
  # are treated like separate requests and so are cached separately.
37
37
  # Keep in mind when expiring an action cache that
38
- # <tt>action: 'lists'</tt> is not the same as
39
- # <tt>action: 'lists', format: :xml</tt>.
38
+ # <tt>action: "lists"</tt> is not the same as
39
+ # <tt>action: "lists", format: :xml</tt>.
40
40
  #
41
41
  # You can modify the default action cache path by passing a
42
42
  # <tt>:cache_path</tt> option. This will be passed directly to
@@ -66,7 +66,7 @@ module ActionController
66
66
  #
67
67
  #
68
68
  # class ListsController < ApplicationController
69
- # before_filter :authenticate, except: :public
69
+ # before_action :authenticate, except: :public
70
70
  #
71
71
  # caches_page :public
72
72
  #
@@ -85,7 +85,7 @@ module ActionController
85
85
  # end
86
86
  # end
87
87
  #
88
- # caches_action :posts, cache_path: CachePathCreator.new('posts')
88
+ # caches_action :posts, cache_path: CachePathCreator.new("posts")
89
89
  # end
90
90
  #
91
91
  # If you pass <tt>layout: false</tt>, it will only cache your action
@@ -113,12 +113,12 @@ module ActionController
113
113
  filter_options = options.extract!(:if, :unless).merge(only: actions)
114
114
  cache_options = options.extract!(:layout, :cache_path).merge(store_options: options)
115
115
 
116
- around_filter ActionCacheFilter.new(cache_options), filter_options
116
+ around_action ActionCacheFilter.new(cache_options), filter_options
117
117
  end
118
118
  end
119
119
 
120
120
  def _save_fragment(name, options)
121
- content = ''
121
+ content = ""
122
122
  response_body.each do |parts|
123
123
  content << parts
124
124
  end
@@ -152,16 +152,8 @@ module ActionController
152
152
  end
153
153
 
154
154
  def around(controller)
155
- cache_layout = @cache_layout.respond_to?(:call) ? @cache_layout.call(controller) : @cache_layout
156
-
157
- path_options = if @cache_path.is_a?(Proc)
158
- controller.instance_exec(controller, &@cache_path)
159
- elsif @cache_path.respond_to?(:call)
160
- @cache_path.call(controller)
161
- else
162
- @cache_path
163
- end
164
-
155
+ cache_layout = expand_option(controller, @cache_layout)
156
+ path_options = expand_option(controller, @cache_path)
165
157
  cache_path = ActionCachePath.new(controller, path_options || {})
166
158
 
167
159
  body = controller.read_fragment(cache_path.path, @store_options)
@@ -173,11 +165,41 @@ module ActionController
173
165
  body = controller._save_fragment(cache_path.path, @store_options)
174
166
  end
175
167
 
176
- body = controller.render_to_string(text: body, layout: true) unless cache_layout
168
+ body = render_to_string(controller, body) unless cache_layout
177
169
 
178
170
  controller.response_body = body
179
171
  controller.content_type = Mime[cache_path.extension || :html]
180
172
  end
173
+
174
+ if ActionPack::VERSION::STRING < "4.1"
175
+ def render_to_string(controller, body)
176
+ controller.render_to_string(text: body, layout: true)
177
+ end
178
+ else
179
+ def render_to_string(controller, body)
180
+ controller.render_to_string(html: body, layout: true)
181
+ end
182
+ end
183
+
184
+ private
185
+ def expand_option(controller, option)
186
+ option = option.to_proc if option.respond_to?(:to_proc)
187
+
188
+ if option.is_a?(Proc)
189
+ case option.arity
190
+ when -2, -1, 1
191
+ controller.instance_exec(controller, &option)
192
+ when 0
193
+ controller.instance_exec(&option)
194
+ else
195
+ raise ArgumentError, "Invalid proc arity of #{option.arity} - proc options should have an arity of 0 or 1"
196
+ end
197
+ elsif option.respond_to?(:call)
198
+ option.call(controller)
199
+ else
200
+ option
201
+ end
202
+ end
181
203
  end
182
204
 
183
205
  class ActionCachePath
@@ -189,19 +211,26 @@ module ActionController
189
211
  # request format.
190
212
  def initialize(controller, options = {}, infer_extension = true)
191
213
  if infer_extension
192
- @extension = controller.params[:format]
214
+ if controller.params.key?(:format)
215
+ @extension = controller.params[:format]
216
+ elsif !controller.request.format.html?
217
+ @extension = controller.request.format.to_sym
218
+ else
219
+ @extension = nil
220
+ end
221
+
193
222
  options.reverse_merge!(format: @extension) if options.is_a?(Hash)
194
223
  end
195
224
 
196
- path = controller.url_for(options).split('://', 2).last
225
+ path = controller.url_for(options).split("://", 2).last
197
226
  @path = normalize!(path)
198
227
  end
199
228
 
200
229
  private
201
230
  def normalize!(path)
202
- ext = URI.parser.escape(extension) if extension
203
- path << 'index' if path[-1] == ?/
204
- path << ".#{ext}" if extension and !path.split('?', 2).first.ends_with?(".#{ext}")
231
+ ext = URI.parser.escape(extension.to_s) if extension
232
+ path << "index" if path[-1] == ?/
233
+ path << ".#{ext}" if extension && !path.split("?", 2).first.ends_with?(".#{ext}")
205
234
  URI.parser.unescape(path)
206
235
  end
207
236
  end
@@ -1 +1 @@
1
- require 'action_controller/action_caching'
1
+ require "actionpack/action_caching/railtie"
@@ -1,11 +1,11 @@
1
- require 'rails/railtie'
1
+ require "rails/railtie"
2
2
 
3
3
  module ActionPack
4
4
  module ActionCaching
5
5
  class Railtie < Rails::Railtie
6
- initializer 'action_pack.action_caching' do
6
+ initializer "action_pack.action_caching" do
7
7
  ActiveSupport.on_load(:action_controller) do
8
- require 'action_controller/action_caching'
8
+ require "action_controller/action_caching"
9
9
  end
10
10
  end
11
11
  end
@@ -1,40 +1,11 @@
1
- require 'bundler/setup'
2
- require 'minitest/autorun'
3
- require 'action_controller'
4
- require 'active_record'
5
- require 'action_controller/action_caching'
1
+ require "bundler/setup"
2
+ require "minitest/autorun"
3
+ require "action_controller"
4
+ require "active_record"
5
+ require "action_controller/action_caching"
6
6
 
7
- FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
7
+ FIXTURE_LOAD_PATH = File.expand_path("../fixtures", __FILE__)
8
8
 
9
- SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
10
-
11
- module ActionController
12
- class Base
13
- include SharedTestRoutes.url_helpers
14
-
15
- self.view_paths = FIXTURE_LOAD_PATH
16
- end
17
-
18
- class TestCase
19
- def setup
20
- @routes = SharedTestRoutes
21
-
22
- @routes.draw do
23
- get ':controller(/:action)'
24
- end
25
- end
26
- end
27
- end
28
-
29
- module RackTestUtils
30
- def body_to_string(body)
31
- if body.respond_to?(:each)
32
- str = ''
33
- body.each {|s| str << s }
34
- str
35
- else
36
- body
37
- end
38
- end
39
- extend self
9
+ if ActiveSupport.respond_to?(:test_order)
10
+ ActiveSupport.test_order = :random
40
11
  end
@@ -1,8 +1,10 @@
1
- require 'abstract_unit'
1
+ require "abstract_unit"
2
+ require "mocha/setup"
2
3
 
3
- CACHE_DIR = 'test_cache'
4
- # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
5
- FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
4
+ CACHE_DIR = "test_cache"
5
+ # Don't change "../tmp" cavalierly or you might hose something you don't want hosed
6
+ TEST_TMP_DIR = File.expand_path("../tmp", __FILE__)
7
+ FILE_STORE_PATH = File.join(TEST_TMP_DIR, CACHE_DIR)
6
8
 
7
9
  class CachingController < ActionController::Base
8
10
  abstract!
@@ -12,7 +14,7 @@ end
12
14
 
13
15
  class CachePath
14
16
  def call(controller)
15
- ['controller', controller.params[:id]].compact.join('-')
17
+ ["controller", controller.params[:id]].compact.join("-")
16
18
  end
17
19
  end
18
20
 
@@ -23,99 +25,159 @@ class ActionCachingTestController < CachingController
23
25
  rescue_from(ActiveRecord::RecordNotFound) { head :not_found }
24
26
  end
25
27
 
26
- # Eliminate uninitialized ivar warning
27
- before_filter { @title = nil }
28
+ self.view_paths = FIXTURE_LOAD_PATH
28
29
 
29
- caches_action :index, :redirected, :forbidden, if: Proc.new { |c| c.request.format && !c.request.format.json? }, expires_in: 1.hour
30
- caches_action :show, cache_path: 'http://test.host/custom/show'
31
- caches_action :edit, cache_path: Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : 'http://test.host/edit' }
30
+ before_action only: :with_symbol_format do
31
+ request.params[:format] = :json
32
+ end
33
+
34
+ caches_action :index, :redirected, :forbidden, if: ->(c) { c.request.format && !c.request.format.json? }, expires_in: 1.hour
35
+ caches_action :show, cache_path: "http://test.host/custom/show"
36
+ caches_action :edit, cache_path: ->(c) { c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
32
37
  caches_action :custom_cache_path, cache_path: CachePath.new
38
+ caches_action :symbol_cache_path, cache_path: :cache_path_protected_method
33
39
  caches_action :with_layout
34
- caches_action :with_format_and_http_param, cache_path: Proc.new { |c| { key: 'value' } }
40
+ caches_action :with_format_and_http_param, cache_path: ->(c) { { key: "value" } }
41
+ caches_action :with_symbol_format, cache_path: "http://test.host/action_caching_test/with_symbol_format"
42
+ caches_action :not_url_cache_path, cache_path: ->(c) { "#{c.params[:action]}_key" }
43
+ caches_action :not_url_cache_path_no_args, cache_path: -> { "#{params[:action]}_key" }
35
44
  caches_action :layout_false, layout: false
36
- caches_action :with_layout_proc_param, layout: Proc.new { |c| c.params[:layout] }
45
+ caches_action :with_layout_proc_param, layout: ->(c) { c.params[:layout] != "false" }
46
+ caches_action :with_layout_proc_param_no_args, layout: -> { params[:layout] != "false" }
37
47
  caches_action :record_not_found, :four_oh_four, :simple_runtime_error
38
48
  caches_action :streaming
39
49
  caches_action :invalid
50
+ caches_action :accept
40
51
 
41
- layout 'talk_from_action'
52
+ layout "talk_from_action"
42
53
 
43
54
  def index
44
55
  @cache_this = MockTime.now.to_f.to_s
45
- render text: @cache_this
56
+ render plain: @cache_this
46
57
  end
47
58
 
48
59
  def redirected
49
- redirect_to action: 'index'
60
+ redirect_to action: "index"
50
61
  end
51
62
 
52
63
  def forbidden
53
- render text: 'Forbidden'
54
- response.status = '403 Forbidden'
64
+ render plain: "Forbidden"
65
+ response.status = "403 Forbidden"
55
66
  end
56
67
 
57
68
  def with_layout
58
69
  @cache_this = MockTime.now.to_f.to_s
59
- @title = nil
60
- render text: @cache_this, layout: true
70
+ render html: @cache_this, layout: true
61
71
  end
62
72
 
63
73
  def with_format_and_http_param
64
74
  @cache_this = MockTime.now.to_f.to_s
65
- render text: @cache_this
75
+ render plain: @cache_this
76
+ end
77
+
78
+ def with_symbol_format
79
+ @cache_this = MockTime.now.to_f.to_s
80
+ render json: { timestamp: @cache_this }
81
+ end
82
+
83
+ def not_url_cache_path
84
+ render plain: "cache_this"
66
85
  end
86
+ alias_method :not_url_cache_path_no_args, :not_url_cache_path
67
87
 
68
88
  def record_not_found
69
- raise ActiveRecord::RecordNotFound, 'oops!'
89
+ raise ActiveRecord::RecordNotFound, "oops!"
70
90
  end
71
91
 
72
92
  def four_oh_four
73
- render text: "404'd!", status: 404
93
+ render plain: "404'd!", status: 404
74
94
  end
75
95
 
76
96
  def simple_runtime_error
77
- raise 'oops!'
97
+ raise "oops!"
78
98
  end
79
99
 
80
100
  alias_method :show, :index
81
101
  alias_method :edit, :index
82
102
  alias_method :destroy, :index
83
103
  alias_method :custom_cache_path, :index
104
+ alias_method :symbol_cache_path, :index
84
105
  alias_method :layout_false, :with_layout
85
106
  alias_method :with_layout_proc_param, :with_layout
107
+ alias_method :with_layout_proc_param_no_args, :with_layout
86
108
 
87
109
  def expire
88
- expire_action controller: 'action_caching_test', action: 'index'
89
- render nothing: true
110
+ expire_action controller: "action_caching_test", action: "index"
111
+ head :ok
90
112
  end
91
113
 
92
114
  def expire_xml
93
- expire_action controller: 'action_caching_test', action: 'index', format: 'xml'
94
- render nothing: true
115
+ expire_action controller: "action_caching_test", action: "index", format: "xml"
116
+ head :ok
95
117
  end
96
118
 
97
119
  def expire_with_url_string
98
- expire_action url_for(controller: 'action_caching_test', action: 'index')
99
- render nothing: true
120
+ expire_action url_for(controller: "action_caching_test", action: "index")
121
+ head :ok
100
122
  end
101
123
 
102
124
  def streaming
103
- render text: 'streaming', stream: true
125
+ render plain: "streaming", stream: true
104
126
  end
105
127
 
106
128
  def invalid
107
129
  @cache_this = MockTime.now.to_f.to_s
108
130
 
109
131
  respond_to do |format|
110
- format.json{ render json: @cache_this }
132
+ format.json { render json: @cache_this }
111
133
  end
112
134
  end
135
+
136
+ def accept
137
+ @cache_this = MockTime.now.to_f.to_s
138
+
139
+ respond_to do |format|
140
+ format.html { render html: @cache_this }
141
+ format.json { render json: @cache_this }
142
+ end
143
+ end
144
+
145
+ def expire_accept
146
+ if params.key?(:format)
147
+ expire_action action: "accept", format: params[:format]
148
+ elsif !request.format.html?
149
+ expire_action action: "accept", format: request.format.to_sym
150
+ else
151
+ expire_action action: "accept"
152
+ end
153
+
154
+ head :ok
155
+ end
156
+
157
+ protected
158
+ def cache_path_protected_method
159
+ ["controller", params[:id]].compact.join("-")
160
+ end
161
+
162
+ if ActionPack::VERSION::STRING < "4.1"
163
+ def render(options)
164
+ if options.key?(:plain)
165
+ super({ text: options.delete(:plain) }.merge(options))
166
+ response.content_type = "text/plain"
167
+ elsif options.key?(:html)
168
+ super({ text: options.delete(:html) }.merge(options))
169
+ response.content_type = "text/html"
170
+ else
171
+ super
172
+ end
173
+ end
174
+ end
113
175
  end
114
176
 
115
177
  class MockTime < Time
116
178
  # Let Time spicy to assure that Time.now != Time.now
117
179
  def to_f
118
- super+rand
180
+ super + rand
119
181
  end
120
182
  end
121
183
 
@@ -136,9 +198,9 @@ class ActionCachingMockController
136
198
  end
137
199
 
138
200
  def request
139
- Object.new.instance_eval(<<-EVAL)
140
- def path; '#{@mock_path}' end
141
- def format; 'all' end
201
+ Object.new.instance_eval <<-EVAL
202
+ def path; "#{@mock_path}" end
203
+ def format; "all" end
142
204
  def parameters; { format: nil }; end
143
205
  self
144
206
  EVAL
@@ -150,7 +212,10 @@ class ActionCacheTest < ActionController::TestCase
150
212
 
151
213
  def setup
152
214
  super
153
- @request.host = 'hostname.com'
215
+
216
+ @routes = ActionDispatch::Routing::RouteSet.new
217
+
218
+ @request.host = "hostname.com"
154
219
  FileUtils.mkdir_p(FILE_STORE_PATH)
155
220
  @path_class = ActionController::Caching::Actions::ActionCachePath
156
221
  @mock_controller = ActionCachingMockController.new
@@ -162,11 +227,15 @@ class ActionCacheTest < ActionController::TestCase
162
227
  end
163
228
 
164
229
  def test_simple_action_cache_with_http_head
230
+ draw do
231
+ get "/action_caching_test", to: "action_caching_test#index"
232
+ end
233
+
165
234
  head :index
166
235
  assert_response :success
167
236
  cached_time = content_to_cache
168
237
  assert_equal cached_time, @response.body
169
- assert fragment_exist?('hostname.com/action_caching_test')
238
+ assert fragment_exist?("hostname.com/action_caching_test")
170
239
 
171
240
  head :index
172
241
  assert_response :success
@@ -174,11 +243,15 @@ class ActionCacheTest < ActionController::TestCase
174
243
  end
175
244
 
176
245
  def test_simple_action_cache
246
+ draw do
247
+ get "/action_caching_test", to: "action_caching_test#index"
248
+ end
249
+
177
250
  get :index
178
251
  assert_response :success
179
252
  cached_time = content_to_cache
180
253
  assert_equal cached_time, @response.body
181
- assert fragment_exist?('hostname.com/action_caching_test')
254
+ assert fragment_exist?("hostname.com/action_caching_test")
182
255
 
183
256
  get :index
184
257
  assert_response :success
@@ -186,103 +259,155 @@ class ActionCacheTest < ActionController::TestCase
186
259
  end
187
260
 
188
261
  def test_simple_action_not_cached
262
+ draw do
263
+ get "/action_caching_test/destroy", to: "action_caching_test#destroy"
264
+ end
265
+
189
266
  get :destroy
190
267
  assert_response :success
191
268
  cached_time = content_to_cache
192
269
  assert_equal cached_time, @response.body
193
- assert !fragment_exist?('hostname.com/action_caching_test/destroy')
270
+ assert !fragment_exist?("hostname.com/action_caching_test/destroy")
194
271
 
195
272
  get :destroy
196
273
  assert_response :success
197
274
  assert_not_equal cached_time, @response.body
198
275
  end
199
276
 
200
- include RackTestUtils
201
-
202
277
  def test_action_cache_with_layout
278
+ draw do
279
+ get "/action_caching_test/with_layout", to: "action_caching_test#with_layout"
280
+ end
281
+
203
282
  get :with_layout
204
283
  assert_response :success
205
284
  cached_time = content_to_cache
206
285
  assert_not_equal cached_time, @response.body
207
- assert fragment_exist?('hostname.com/action_caching_test/with_layout')
286
+ assert fragment_exist?("hostname.com/action_caching_test/with_layout")
208
287
 
209
288
  get :with_layout
210
289
  assert_response :success
211
290
  assert_not_equal cached_time, @response.body
212
- body = body_to_string(read_fragment('hostname.com/action_caching_test/with_layout'))
213
- assert_equal @response.body, body
291
+ assert_equal @response.body, read_fragment("hostname.com/action_caching_test/with_layout")
214
292
  end
215
293
 
216
294
  def test_action_cache_with_layout_and_layout_cache_false
217
- get :layout_false
295
+ draw do
296
+ get "/action_caching_test/layout_false", to: "action_caching_test#layout_false"
297
+ end
298
+
299
+ get :layout_false, params: { title: "Request 1" }
218
300
  assert_response :success
219
301
  cached_time = content_to_cache
220
- assert_not_equal cached_time, @response.body
221
- assert fragment_exist?('hostname.com/action_caching_test/layout_false')
302
+ assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
303
+ assert_equal cached_time, read_fragment("hostname.com/action_caching_test/layout_false")
222
304
 
223
- get :layout_false
305
+ get :layout_false, params: { title: "Request 2" }
224
306
  assert_response :success
225
- assert_not_equal cached_time, @response.body
226
- body = body_to_string(read_fragment('hostname.com/action_caching_test/layout_false'))
227
- assert_equal cached_time, body
307
+ assert_equal "<title>Request 2</title>\n#{cached_time}", @response.body
308
+ assert_equal cached_time, read_fragment("hostname.com/action_caching_test/layout_false")
228
309
  end
229
310
 
230
311
  def test_action_cache_with_layout_and_layout_cache_false_via_proc
231
- get :with_layout_proc_param, layout: false
312
+ draw do
313
+ get "/action_caching_test/with_layout_proc_param", to: "action_caching_test#with_layout_proc_param"
314
+ end
315
+
316
+ get :with_layout_proc_param, params: { title: "Request 1", layout: "false" }
232
317
  assert_response :success
233
318
  cached_time = content_to_cache
234
- assert_not_equal cached_time, @response.body
235
- assert fragment_exist?('hostname.com/action_caching_test/with_layout_proc_param')
319
+ assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
320
+ assert_equal cached_time, read_fragment("hostname.com/action_caching_test/with_layout_proc_param")
236
321
 
237
- get :with_layout_proc_param, layout: false
322
+ get :with_layout_proc_param, params: { title: "Request 2", layout: "false" }
238
323
  assert_response :success
239
- assert_not_equal cached_time, @response.body
240
- body = body_to_string(read_fragment('hostname.com/action_caching_test/with_layout_proc_param'))
241
- assert_equal cached_time, body
324
+ assert_equal "<title>Request 2</title>\n#{cached_time}", @response.body
325
+ assert_equal cached_time, read_fragment("hostname.com/action_caching_test/with_layout_proc_param")
242
326
  end
243
327
 
244
328
  def test_action_cache_with_layout_and_layout_cache_true_via_proc
245
- get :with_layout_proc_param, layout: true
329
+ draw do
330
+ get "/action_caching_test/with_layout_proc_param", to: "action_caching_test#with_layout_proc_param"
331
+ end
332
+
333
+ get :with_layout_proc_param, params: { title: "Request 1", layout: "true" }
246
334
  assert_response :success
247
335
  cached_time = content_to_cache
248
- assert_not_equal cached_time, @response.body
249
- assert fragment_exist?('hostname.com/action_caching_test/with_layout_proc_param')
336
+ assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
337
+ assert_equal "<title>Request 1</title>\n#{cached_time}", read_fragment("hostname.com/action_caching_test/with_layout_proc_param")
250
338
 
251
- get :with_layout_proc_param, layout: true
339
+ get :with_layout_proc_param, params: { title: "Request 2", layout: "true" }
252
340
  assert_response :success
253
- assert_not_equal cached_time, @response.body
254
- body = body_to_string(read_fragment('hostname.com/action_caching_test/with_layout_proc_param'))
255
- assert_equal @response.body, body
341
+ assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
342
+ assert_equal "<title>Request 1</title>\n#{cached_time}", read_fragment("hostname.com/action_caching_test/with_layout_proc_param")
256
343
  end
257
344
 
258
345
  def test_action_cache_conditional_options
259
- @request.env['HTTP_ACCEPT'] = 'application/json'
346
+ draw do
347
+ get "/action_caching_test", to: "action_caching_test#index"
348
+ end
349
+
350
+ @request.accept = "application/json"
260
351
  get :index
261
352
  assert_response :success
262
- assert !fragment_exist?('hostname.com/action_caching_test')
353
+ assert !fragment_exist?("hostname.com/action_caching_test")
263
354
  end
264
355
 
265
356
  def test_action_cache_with_format_and_http_param
266
- get :with_format_and_http_param, format: 'json'
357
+ draw do
358
+ get "/action_caching_test/with_format_and_http_param", to: "action_caching_test#with_format_and_http_param"
359
+ end
360
+
361
+ get :with_format_and_http_param, format: "json"
362
+ assert_response :success
363
+ assert !fragment_exist?("hostname.com/action_caching_test/with_format_and_http_param.json?key=value.json")
364
+ assert fragment_exist?("hostname.com/action_caching_test/with_format_and_http_param.json?key=value")
365
+ end
366
+
367
+ def test_action_cache_with_symbol_format
368
+ draw do
369
+ get "/action_caching_test/with_symbol_format", to: "action_caching_test#with_symbol_format"
370
+ end
371
+
372
+ get :with_symbol_format
373
+ assert_response :success
374
+ assert !fragment_exist?("test.host/action_caching_test/with_symbol_format")
375
+ assert fragment_exist?("test.host/action_caching_test/with_symbol_format.json")
376
+ end
377
+
378
+ def test_action_cache_not_url_cache_path
379
+ draw do
380
+ get "/action_caching_test/not_url_cache_path", to: "action_caching_test#not_url_cache_path"
381
+ end
382
+
383
+ get :not_url_cache_path
267
384
  assert_response :success
268
- assert !fragment_exist?('hostname.com/action_caching_test/with_format_and_http_param.json?key=value.json')
269
- assert fragment_exist?('hostname.com/action_caching_test/with_format_and_http_param.json?key=value')
385
+ assert !fragment_exist?("test.host/action_caching_test/not_url_cache_path")
386
+ assert fragment_exist?("not_url_cache_path_key")
270
387
  end
271
388
 
272
389
  def test_action_cache_with_store_options
390
+ draw do
391
+ get "/action_caching_test", to: "action_caching_test#index"
392
+ end
393
+
273
394
  MockTime.expects(:now).returns(12345).once
274
- @controller.expects(:read_fragment).with('hostname.com/action_caching_test', expires_in: 1.hour).once
275
- @controller.expects(:write_fragment).with('hostname.com/action_caching_test', '12345.0', expires_in: 1.hour).once
395
+ @controller.expects(:read_fragment).with("hostname.com/action_caching_test", expires_in: 1.hour).once
396
+ @controller.expects(:write_fragment).with("hostname.com/action_caching_test", "12345.0", expires_in: 1.hour).once
276
397
  get :index
277
398
  assert_response :success
278
399
  end
279
400
 
280
401
  def test_action_cache_with_custom_cache_path
402
+ draw do
403
+ get "/action_caching_test/show", to: "action_caching_test#show"
404
+ end
405
+
281
406
  get :show
282
407
  assert_response :success
283
408
  cached_time = content_to_cache
284
409
  assert_equal cached_time, @response.body
285
- assert fragment_exist?('test.host/custom/show')
410
+ assert fragment_exist?("test.host/custom/show")
286
411
 
287
412
  get :show
288
413
  assert_response :success
@@ -290,26 +415,53 @@ class ActionCacheTest < ActionController::TestCase
290
415
  end
291
416
 
292
417
  def test_action_cache_with_custom_cache_path_in_block
418
+ draw do
419
+ get "/action_caching_test/edit(/:id)", to: "action_caching_test#edit"
420
+ end
421
+
293
422
  get :edit
294
423
  assert_response :success
295
- assert fragment_exist?('test.host/edit')
424
+ assert fragment_exist?("test.host/edit")
296
425
 
297
- get :edit, id: 1
426
+ get :edit, params: { id: 1 }
298
427
  assert_response :success
299
- assert fragment_exist?('test.host/1;edit')
428
+ assert fragment_exist?("test.host/1;edit")
300
429
  end
301
430
 
302
431
  def test_action_cache_with_custom_cache_path_with_custom_object
432
+ draw do
433
+ get "/action_caching_test/custom_cache_path(/:id)", to: "action_caching_test#custom_cache_path"
434
+ end
435
+
303
436
  get :custom_cache_path
304
437
  assert_response :success
305
- assert fragment_exist?('controller')
438
+ assert fragment_exist?("controller")
306
439
 
307
- get :custom_cache_path, id: 1
440
+ get :custom_cache_path, params: { id: 1 }
308
441
  assert_response :success
309
- assert fragment_exist?('controller-1')
442
+ assert fragment_exist?("controller-1")
443
+ end
444
+
445
+ def test_action_cache_with_symbol_cache_path
446
+ draw do
447
+ get "/action_caching_test/symbol_cache_path(/:id)", to: "action_caching_test#symbol_cache_path"
448
+ end
449
+
450
+ get :symbol_cache_path
451
+ assert_response :success
452
+ assert fragment_exist?("controller")
453
+
454
+ get :symbol_cache_path, params: { id: 1 }
455
+ assert_response :success
456
+ assert fragment_exist?("controller-1")
310
457
  end
311
458
 
312
459
  def test_cache_expiration
460
+ draw do
461
+ get "/action_caching_test", to: "action_caching_test#index"
462
+ get "/action_caching_test/expire", to: "action_caching_test#expire"
463
+ end
464
+
313
465
  get :index
314
466
  assert_response :success
315
467
  cached_time = content_to_cache
@@ -332,6 +484,11 @@ class ActionCacheTest < ActionController::TestCase
332
484
  end
333
485
 
334
486
  def test_cache_expiration_isnt_affected_by_request_format
487
+ draw do
488
+ get "/action_caching_test", to: "action_caching_test#index"
489
+ get "/action_caching_test/expire", to: "action_caching_test#expire"
490
+ end
491
+
335
492
  get :index
336
493
  cached_time = content_to_cache
337
494
 
@@ -345,6 +502,11 @@ class ActionCacheTest < ActionController::TestCase
345
502
  end
346
503
 
347
504
  def test_cache_expiration_with_url_string
505
+ draw do
506
+ get "/action_caching_test", to: "action_caching_test#index"
507
+ get "/action_caching_test/expire_with_url_string", to: "action_caching_test#expire_with_url_string"
508
+ end
509
+
348
510
  get :index
349
511
  cached_time = content_to_cache
350
512
 
@@ -358,29 +520,38 @@ class ActionCacheTest < ActionController::TestCase
358
520
  end
359
521
 
360
522
  def test_cache_is_scoped_by_subdomain
361
- @request.host = 'jamis.hostname.com'
523
+ draw do
524
+ get "/action_caching_test", to: "action_caching_test#index"
525
+ end
526
+
527
+ @request.host = "jamis.hostname.com"
362
528
  get :index
363
529
  assert_response :success
364
530
  jamis_cache = content_to_cache
365
531
 
366
- @request.host = 'david.hostname.com'
532
+ @request.host = "david.hostname.com"
367
533
  get :index
368
534
  assert_response :success
369
535
  david_cache = content_to_cache
370
536
  assert_not_equal jamis_cache, @response.body
371
537
 
372
- @request.host = 'jamis.hostname.com'
538
+ @request.host = "jamis.hostname.com"
373
539
  get :index
374
540
  assert_response :success
375
541
  assert_equal jamis_cache, @response.body
376
542
 
377
- @request.host = 'david.hostname.com'
543
+ @request.host = "david.hostname.com"
378
544
  get :index
379
545
  assert_response :success
380
546
  assert_equal david_cache, @response.body
381
547
  end
382
548
 
383
549
  def test_redirect_is_not_cached
550
+ draw do
551
+ get "/action_caching_test", to: "action_caching_test#index"
552
+ get "/action_caching_test/redirected", to: "action_caching_test#redirected"
553
+ end
554
+
384
555
  get :redirected
385
556
  assert_response :redirect
386
557
  get :redirected
@@ -388,6 +559,10 @@ class ActionCacheTest < ActionController::TestCase
388
559
  end
389
560
 
390
561
  def test_forbidden_is_not_cached
562
+ draw do
563
+ get "/action_caching_test/forbidden", to: "action_caching_test#forbidden"
564
+ end
565
+
391
566
  get :forbidden
392
567
  assert_response :forbidden
393
568
  get :forbidden
@@ -395,71 +570,90 @@ class ActionCacheTest < ActionController::TestCase
395
570
  end
396
571
 
397
572
  def test_xml_version_of_resource_is_treated_as_different_cache
398
- with_routing do |set|
399
- set.draw do
400
- get ':controller(/:action(.:format))'
401
- end
573
+ draw do
574
+ get "/action_caching_test/index", to: "action_caching_test#index"
575
+ get "/action_caching_test/expire_xml", to: "action_caching_test#expire_xml"
576
+ end
402
577
 
403
- get :index, format: 'xml'
404
- assert_response :success
405
- cached_time = content_to_cache
406
- assert_equal cached_time, @response.body
407
- assert fragment_exist?('hostname.com/action_caching_test/index.xml')
578
+ get :index, format: "xml"
579
+ assert_response :success
580
+ cached_time = content_to_cache
581
+ assert_equal cached_time, @response.body
582
+ assert fragment_exist?("hostname.com/action_caching_test/index.xml")
408
583
 
409
- get :index, format: 'xml'
410
- assert_response :success
411
- assert_equal cached_time, @response.body
412
- assert_equal 'application/xml', @response.content_type
584
+ get :index, format: "xml"
585
+ assert_response :success
586
+ assert_equal cached_time, @response.body
587
+ assert_equal "application/xml", @response.content_type
413
588
 
414
- get :expire_xml
415
- assert_response :success
589
+ get :expire_xml
590
+ assert_response :success
416
591
 
417
- get :index, format: 'xml'
418
- assert_response :success
419
- assert_not_equal cached_time, @response.body
420
- end
592
+ get :index, format: "xml"
593
+ assert_response :success
594
+ assert_not_equal cached_time, @response.body
421
595
  end
422
596
 
423
597
  def test_correct_content_type_is_returned_for_cache_hit
598
+ draw do
599
+ get "/action_caching_test/index/:id", to: "action_caching_test#index"
600
+ end
601
+
424
602
  # run it twice to cache it the first time
425
- get :index, id: 'content-type', format: 'xml'
426
- get :index, id: 'content-type', format: 'xml'
603
+ get :index, params: { id: "content-type" }, format: "xml"
604
+ get :index, params: { id: "content-type" }, format: "xml"
427
605
  assert_response :success
428
- assert_equal 'application/xml', @response.content_type
606
+ assert_equal "application/xml", @response.content_type
429
607
  end
430
608
 
431
609
  def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_key
610
+ draw do
611
+ get "/action_caching_test/show", to: "action_caching_test#show"
612
+ end
613
+
432
614
  # run it twice to cache it the first time
433
- get :show, format: 'xml'
434
- get :show, format: 'xml'
615
+ get :show, format: "xml"
616
+ get :show, format: "xml"
435
617
  assert_response :success
436
- assert_equal 'application/xml', @response.content_type
618
+ assert_equal "application/xml", @response.content_type
437
619
  end
438
620
 
439
621
  def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_key_from_proc
622
+ draw do
623
+ get "/action_caching_test/edit/:id", to: "action_caching_test#edit"
624
+ end
625
+
440
626
  # run it twice to cache it the first time
441
- get :edit, id: 1, format: 'xml'
442
- get :edit, id: 1, format: 'xml'
627
+ get :edit, params: { id: 1 }, format: "xml"
628
+ get :edit, params: { id: 1 }, format: "xml"
443
629
  assert_response :success
444
- assert_equal 'application/xml', @response.content_type
630
+ assert_equal "application/xml", @response.content_type
445
631
  end
446
632
 
447
633
  def test_empty_path_is_normalized
448
- @mock_controller.mock_url_for = 'http://example.org/'
449
- @mock_controller.mock_path = '/'
634
+ @mock_controller.mock_url_for = "http://example.org/"
635
+ @mock_controller.mock_path = "/"
450
636
 
451
- assert_equal 'example.org/index', @path_class.new(@mock_controller, {}).path
637
+ assert_equal "example.org/index", @path_class.new(@mock_controller, {}).path
452
638
  end
453
639
 
454
640
  def test_file_extensions
455
- get :index, id: 'kitten.jpg'
456
- get :index, id: 'kitten.jpg'
641
+ draw do
642
+ get "/action_caching_test/index/*id", to: "action_caching_test#index", format: false
643
+ end
644
+
645
+ get :index, params: { id: "kitten.jpg" }
646
+ get :index, params: { id: "kitten.jpg" }
457
647
 
458
648
  assert_response :success
459
649
  end
460
650
 
461
651
  if defined? ActiveRecord
462
652
  def test_record_not_found_returns_404_for_multiple_requests
653
+ draw do
654
+ get "/action_caching_test/record_not_found", to: "action_caching_test#record_not_found"
655
+ end
656
+
463
657
  get :record_not_found
464
658
  assert_response 404
465
659
  get :record_not_found
@@ -468,6 +662,10 @@ class ActionCacheTest < ActionController::TestCase
468
662
  end
469
663
 
470
664
  def test_four_oh_four_returns_404_for_multiple_requests
665
+ draw do
666
+ get "/action_caching_test/four_oh_four", to: "action_caching_test#four_oh_four"
667
+ end
668
+
471
669
  get :four_oh_four
472
670
  assert_response 404
473
671
  get :four_oh_four
@@ -475,11 +673,19 @@ class ActionCacheTest < ActionController::TestCase
475
673
  end
476
674
 
477
675
  def test_four_oh_four_renders_content
676
+ draw do
677
+ get "/action_caching_test/four_oh_four", to: "action_caching_test#four_oh_four"
678
+ end
679
+
478
680
  get :four_oh_four
479
681
  assert_equal "404'd!", @response.body
480
682
  end
481
683
 
482
684
  def test_simple_runtime_error_returns_500_for_multiple_requests
685
+ draw do
686
+ get "/action_caching_test/simple_runtime_error", to: "action_caching_test#simple_runtime_error"
687
+ end
688
+
483
689
  get :simple_runtime_error
484
690
  assert_response 500
485
691
  get :simple_runtime_error
@@ -487,35 +693,177 @@ class ActionCacheTest < ActionController::TestCase
487
693
  end
488
694
 
489
695
  def test_action_caching_plus_streaming
696
+ draw do
697
+ get "/action_caching_test/streaming", to: "action_caching_test#streaming"
698
+ end
699
+
490
700
  get :streaming
491
701
  assert_response :success
492
702
  assert_match(/streaming/, @response.body)
493
- assert fragment_exist?('hostname.com/action_caching_test/streaming')
703
+ assert fragment_exist?("hostname.com/action_caching_test/streaming")
494
704
  end
495
705
 
496
706
  def test_invalid_format_returns_not_acceptable
497
- get :invalid, format: 'json'
707
+ draw do
708
+ get "/action_caching_test/invalid", to: "action_caching_test#invalid"
709
+ end
710
+
711
+ get :invalid, format: "json"
498
712
  assert_response :success
499
713
  cached_time = content_to_cache
500
714
  assert_equal cached_time, @response.body
501
715
 
502
716
  assert fragment_exist?("hostname.com/action_caching_test/invalid.json")
503
717
 
504
- get :invalid, format: 'json'
718
+ get :invalid, format: "json"
505
719
  assert_response :success
506
720
  assert_equal cached_time, @response.body
507
721
 
508
- get :invalid, format: 'xml'
722
+ get :invalid, format: "xml"
509
723
  assert_response :not_acceptable
510
724
 
511
- get :invalid, format: '\xC3\x83'
725
+ get :invalid, format: "\xC3\x83"
512
726
  assert_response :not_acceptable
513
727
  end
514
728
 
729
+ def test_format_from_accept_header
730
+ draw do
731
+ get "/action_caching_test/accept", to: "action_caching_test#accept"
732
+ get "/action_caching_test/accept/expire", to: "action_caching_test#expire_accept"
733
+ end
734
+
735
+ # Cache the JSON format
736
+ get_json :accept
737
+ json_cached_time = content_to_cache
738
+ assert_cached json_cached_time, "application/json"
739
+
740
+ # Check that the JSON format is cached
741
+ get_json :accept
742
+ assert_cached json_cached_time, "application/json"
743
+
744
+ # Cache the HTML format
745
+ get_html :accept
746
+ html_cached_time = content_to_cache
747
+ assert_cached html_cached_time
748
+
749
+ # Check that it's not the JSON format
750
+ assert_not_equal json_cached_time, @response.body
751
+
752
+ # Check that the HTML format is cached
753
+ get_html :accept
754
+ assert_cached html_cached_time
755
+
756
+ # Check that the JSON format is still cached
757
+ get_json :accept
758
+ assert_cached json_cached_time, "application/json"
759
+
760
+ # Expire the JSON format
761
+ get_json :expire_accept
762
+ assert_response :success
763
+
764
+ # Check that the HTML format is still cached
765
+ get_html :accept
766
+ assert_cached html_cached_time
767
+
768
+ # Check the JSON format was expired
769
+ get_json :accept
770
+ new_json_cached_time = content_to_cache
771
+ assert_cached new_json_cached_time, "application/json"
772
+ assert_not_equal json_cached_time, @response.body
773
+
774
+ # Expire the HTML format
775
+ get_html :expire_accept
776
+ assert_response :success
777
+
778
+ # Check that the JSON format is still cached
779
+ get_json :accept
780
+ assert_cached new_json_cached_time, "application/json"
781
+
782
+ # Check the HTML format was expired
783
+ get_html :accept
784
+ new_html_cached_time = content_to_cache
785
+ assert_cached new_html_cached_time
786
+ assert_not_equal html_cached_time, @response.body
787
+ end
788
+
789
+ def test_explicit_html_format_is_used_for_fragment_path
790
+ draw do
791
+ get "/action_caching_test/accept", to: "action_caching_test#accept"
792
+ get "/action_caching_test/accept/expire", to: "action_caching_test#expire_accept"
793
+ end
794
+
795
+ get :accept, format: "html"
796
+ cached_time = content_to_cache
797
+ assert_cached cached_time
798
+
799
+ assert fragment_exist?("hostname.com/action_caching_test/accept.html")
800
+
801
+ get :accept, format: "html"
802
+ cached_time = content_to_cache
803
+ assert_cached cached_time
804
+
805
+ get :expire_accept, format: "html"
806
+ assert_response :success
807
+
808
+ assert !fragment_exist?("hostname.com/action_caching_test/accept.html")
809
+
810
+ get :accept, format: "html"
811
+ assert_not_cached cached_time
812
+ end
813
+
814
+ def test_lambda_arity_with_cache_path
815
+ draw do
816
+ get "/action_caching_test/not_url_cache_path_no_args", to: "action_caching_test#not_url_cache_path_no_args"
817
+ end
818
+
819
+ get :not_url_cache_path_no_args
820
+ assert_response :success
821
+ assert !fragment_exist?("test.host/action_caching_test/not_url_cache_path_no_args")
822
+ assert fragment_exist?("not_url_cache_path_no_args_key")
823
+ end
824
+
825
+ def test_lambda_arity_with_layout
826
+ draw do
827
+ get "/action_caching_test/with_layout_proc_param_no_args", to: "action_caching_test#with_layout_proc_param_no_args"
828
+ end
829
+
830
+ get :with_layout_proc_param_no_args, params: { title: "Request 1", layout: "false" }
831
+ assert_response :success
832
+ cached_time = content_to_cache
833
+ assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
834
+ assert_equal cached_time, read_fragment("hostname.com/action_caching_test/with_layout_proc_param_no_args")
835
+
836
+ get :with_layout_proc_param_no_args, params: { title: "Request 2", layout: "false" }
837
+ assert_response :success
838
+ assert_equal "<title>Request 2</title>\n#{cached_time}", @response.body
839
+ assert_equal cached_time, read_fragment("hostname.com/action_caching_test/with_layout_proc_param_no_args")
840
+ end
841
+
515
842
  private
843
+ def get_html(*args)
844
+ @request.accept = "text/html"
845
+ get(*args)
846
+ end
847
+
848
+ def get_json(*args)
849
+ @request.accept = "application/json"
850
+ get(*args)
851
+ end
852
+
853
+ def assert_cached(cache_time, content_type = "text/html")
854
+ assert_response :success
855
+ assert_equal cache_time, @response.body
856
+ assert_equal content_type, @response.content_type
857
+ end
858
+
859
+ def assert_not_cached(cache_time, content_type = "text/html")
860
+ assert_response :success
861
+ assert_not_equal cache_time, @response.body
862
+ assert_equal content_type, @response.content_type
863
+ end
516
864
 
517
865
  def content_to_cache
518
- assigns(:cache_this)
866
+ @controller.instance_variable_get(:@cache_this)
519
867
  end
520
868
 
521
869
  def fragment_exist?(path)
@@ -525,4 +873,21 @@ class ActionCacheTest < ActionController::TestCase
525
873
  def read_fragment(path)
526
874
  @controller.read_fragment(path)
527
875
  end
876
+
877
+ def draw(&block)
878
+ @routes = ActionDispatch::Routing::RouteSet.new
879
+ @routes.draw(&block)
880
+ @controller.extend(@routes.url_helpers)
881
+ end
882
+
883
+ if ActionPack::VERSION::STRING < "5.0"
884
+ def get(action, options = {})
885
+ format = options.slice(:format)
886
+ params = options[:params] || {}
887
+ session = options[:session] || {}
888
+ flash = options[:flash] || {}
889
+
890
+ super(action, params.merge(format), session, flash)
891
+ end
892
+ end
528
893
  end