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