require 'rubygems' require_gem 'rails' require 'action_controller/test_process' require 'test/unit' require 'lib/http_caching' ActionController::Routing::Routes.draw do |map| map.connect ':controller/:action/:id' end # The base class for testing. class ApplicationController < ActionController::Base def rescue_action(e) raise e end; # This is some regular content. Well, time-wise it's regular. It's updated on # a regular basis. def index_without_cache @pony_palace_parade = "Excelsior!" render :text => 'Whee!' end # This is some really old content, and it's never updated. Hasn't been updated # since the Great Turnip/Gopher Fire of aught-three! If someone got a copy any # time in the past three years, don't send them a new one--it's 300TB! def index_with_cache if_cached_before 3.years.ago do @woo_hah = 'Thwarp!' render :text => 'Majestesticular.' end end # This is some really old content too, but in a different format. def index_with_cache_render render_if_cached_before 3.years.ago, :text => 'MYEWTAYNTZ!' end end class HttpCachingTest < Test::Unit::TestCase def setup @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @controller = ApplicationController.new end def test_baseline get :index_without_cache assert_response :success assert_not_nil assigns(:pony_palace_parade) end def test_unless_cached_before # "I'd like a copy if it's been updated in the past ms!" @request.env['HTTP_IF_MODIFIED_SINCE'] = Time.now.httpdate get :index_with_cache # "Sure thing, freakmeat! Oh wait--CACHE! Smell you later!" assert_response 304 assert_nil assigns(:woo_hah) # "I'm from the past! I don't know what a button is! Give me this file" @request.env['HTTP_IF_MODIFIED_SINCE'] = 4.years.ago.httpdate get :index_with_cache # "Sure thing, grand-dad! Here's an iron axe while we're at it. They're all # the rage with the kids, by which I mean they are fashionable!" assert_response :success assert_not_nil assigns(:woo_hah) # "I'm a mysterious stranger, trying to make a home for myself in this # crazy, mixed-up world of crappy browsers." @request.env['HTTP_IF_MODIFIED_SINCE'] = nil get :index_with_cache # "Sure thing, Bananahands--I mean, tall, dark and handsome!" assert_response :success assert_not_nil assigns(:woo_hah) end def test_render_unless_cached_before @request.env['HTTP_IF_MODIFIED_SINCE'] = Time.now.httpdate get :index_with_cache_render assert_response 304 @request.env['HTTP_IF_MODIFIED_SINCE'] = 4.years.ago.httpdate get :index_with_cache_render assert_response :success @request.env['HTTP_IF_MODIFIED_SINCE'] = nil get :index_with_cache_render assert_response :success end end