2010-09-13 18:53:14 -04:00
|
|
|
require 'isolation/abstract_unit'
|
|
|
|
|
|
|
|
module ApplicationTests
|
2012-08-28 12:08:22 -04:00
|
|
|
class CacheTest < ActiveSupport::TestCase
|
2010-09-13 18:53:14 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
require 'rack/test'
|
|
|
|
extend Rack::Test::Methods
|
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2010-09-13 18:53:14 -04:00
|
|
|
def simple_controller
|
2010-09-15 15:58:49 -04:00
|
|
|
controller :expires, <<-RUBY
|
2010-09-13 18:53:14 -04:00
|
|
|
class ExpiresController < ApplicationController
|
|
|
|
def expires_header
|
2012-10-14 06:03:39 -04:00
|
|
|
expires_in 10, public: !params[:private]
|
|
|
|
render text: SecureRandom.hex(16)
|
2010-09-13 18:53:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def expires_etag
|
2012-10-14 06:03:39 -04:00
|
|
|
render_conditionally(etag: "1")
|
2010-09-13 18:53:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def expires_last_modified
|
|
|
|
$last_modified ||= Time.now.utc
|
2012-10-14 06:03:39 -04:00
|
|
|
render_conditionally(last_modified: $last_modified)
|
2010-09-13 18:53:14 -04:00
|
|
|
end
|
2011-10-03 14:17:12 -04:00
|
|
|
|
|
|
|
def keeps_if_modified_since
|
|
|
|
render :text => request.headers['If-Modified-Since']
|
|
|
|
end
|
2010-09-13 18:53:14 -04:00
|
|
|
private
|
|
|
|
def render_conditionally(headers)
|
2012-10-14 06:03:39 -04:00
|
|
|
if stale?(headers.merge(public: !params[:private]))
|
|
|
|
render text: SecureRandom.hex(16)
|
2010-09-13 18:53:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2013-06-09 22:47:07 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller(/:action)'
|
2010-09-13 18:53:14 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2011-10-03 14:17:12 -04:00
|
|
|
def test_cache_keeps_if_modified_since
|
|
|
|
simple_controller
|
|
|
|
expected = "Wed, 30 May 1984 19:43:31 GMT"
|
2011-12-12 14:40:29 -05:00
|
|
|
|
2011-10-03 14:17:12 -04:00
|
|
|
get "/expires/keeps_if_modified_since", {}, "HTTP_IF_MODIFIED_SINCE" => expected
|
2011-12-12 14:40:29 -05:00
|
|
|
|
2011-10-03 14:17:12 -04:00
|
|
|
assert_equal 200, last_response.status
|
|
|
|
assert_equal expected, last_response.body, "cache should have kept If-Modified-Since"
|
|
|
|
end
|
|
|
|
|
2010-09-15 15:58:49 -04:00
|
|
|
def test_cache_is_disabled_in_dev_mode
|
|
|
|
simple_controller
|
|
|
|
app("development")
|
|
|
|
|
|
|
|
get "/expires/expires_header"
|
|
|
|
assert_nil last_response.headers['X-Rack-Cache']
|
|
|
|
|
|
|
|
body = last_response.body
|
|
|
|
|
|
|
|
get "/expires/expires_header"
|
|
|
|
assert_nil last_response.headers['X-Rack-Cache']
|
|
|
|
assert_not_equal body, last_response.body
|
|
|
|
end
|
|
|
|
|
2010-09-13 18:53:14 -04:00
|
|
|
def test_cache_works_with_expires
|
|
|
|
simple_controller
|
|
|
|
|
2012-10-18 13:31:52 -04:00
|
|
|
add_to_config "config.action_dispatch.rack_cache = true"
|
|
|
|
|
2010-09-13 18:53:14 -04:00
|
|
|
get "/expires/expires_header"
|
2013-01-14 22:55:35 -05:00
|
|
|
assert_equal "miss, ignore, store", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_equal "max-age=10, public", last_response.headers["Cache-Control"]
|
2010-09-13 18:53:14 -04:00
|
|
|
|
|
|
|
body = last_response.body
|
|
|
|
|
|
|
|
get "/expires/expires_header"
|
|
|
|
|
|
|
|
assert_equal "fresh", last_response.headers["X-Rack-Cache"]
|
|
|
|
|
|
|
|
assert_equal body, last_response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_works_with_expires_private
|
|
|
|
simple_controller
|
|
|
|
|
2012-10-18 13:31:52 -04:00
|
|
|
add_to_config "config.action_dispatch.rack_cache = true"
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
get "/expires/expires_header", private: true
|
2010-09-13 18:53:14 -04:00
|
|
|
assert_equal "miss", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_equal "private, max-age=10", last_response.headers["Cache-Control"]
|
|
|
|
|
|
|
|
body = last_response.body
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
get "/expires/expires_header", private: true
|
2010-09-13 18:53:14 -04:00
|
|
|
assert_equal "miss", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_not_equal body, last_response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_works_with_etags
|
|
|
|
simple_controller
|
|
|
|
|
2012-10-18 13:31:52 -04:00
|
|
|
add_to_config "config.action_dispatch.rack_cache = true"
|
|
|
|
|
2010-09-13 18:53:14 -04:00
|
|
|
get "/expires/expires_etag"
|
2013-01-14 22:55:35 -05:00
|
|
|
assert_equal "miss, ignore, store", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_equal "public", last_response.headers["Cache-Control"]
|
2010-09-13 18:53:14 -04:00
|
|
|
|
|
|
|
body = last_response.body
|
|
|
|
etag = last_response.headers["ETag"]
|
|
|
|
|
|
|
|
get "/expires/expires_etag", {}, "If-None-Match" => etag
|
|
|
|
assert_equal "stale, valid, store", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_equal body, last_response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_works_with_etags_private
|
|
|
|
simple_controller
|
|
|
|
|
2012-10-18 13:31:52 -04:00
|
|
|
add_to_config "config.action_dispatch.rack_cache = true"
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
get "/expires/expires_etag", private: true
|
2010-09-13 18:53:14 -04:00
|
|
|
assert_equal "miss", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_equal "must-revalidate, private, max-age=0", last_response.headers["Cache-Control"]
|
|
|
|
|
|
|
|
body = last_response.body
|
|
|
|
etag = last_response.headers["ETag"]
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
get "/expires/expires_etag", {private: true}, "If-None-Match" => etag
|
2010-09-13 18:53:14 -04:00
|
|
|
assert_equal "miss", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_not_equal body, last_response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_works_with_last_modified
|
|
|
|
simple_controller
|
|
|
|
|
2012-10-18 13:31:52 -04:00
|
|
|
add_to_config "config.action_dispatch.rack_cache = true"
|
|
|
|
|
2010-09-13 18:53:14 -04:00
|
|
|
get "/expires/expires_last_modified"
|
2013-01-14 22:55:35 -05:00
|
|
|
assert_equal "miss, ignore, store", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_equal "public", last_response.headers["Cache-Control"]
|
2010-09-13 18:53:14 -04:00
|
|
|
|
|
|
|
body = last_response.body
|
|
|
|
last = last_response.headers["Last-Modified"]
|
|
|
|
|
|
|
|
get "/expires/expires_last_modified", {}, "If-Modified-Since" => last
|
|
|
|
assert_equal "stale, valid, store", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_equal body, last_response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_works_with_last_modified_private
|
|
|
|
simple_controller
|
|
|
|
|
2012-10-18 13:31:52 -04:00
|
|
|
add_to_config "config.action_dispatch.rack_cache = true"
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
get "/expires/expires_last_modified", private: true
|
2010-09-13 18:53:14 -04:00
|
|
|
assert_equal "miss", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_equal "must-revalidate, private, max-age=0", last_response.headers["Cache-Control"]
|
|
|
|
|
|
|
|
body = last_response.body
|
|
|
|
last = last_response.headers["Last-Modified"]
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
get "/expires/expires_last_modified", {private: true}, "If-Modified-Since" => last
|
2010-09-13 18:53:14 -04:00
|
|
|
assert_equal "miss", last_response.headers["X-Rack-Cache"]
|
|
|
|
assert_not_equal body, last_response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|