From 0fe99e87c0af568df2f557637fff2983c928b8f2 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Mon, 13 Sep 2010 15:53:14 -0700 Subject: Add tests for Rack::Cache --- railties/test/application/middleware/cache_test.rb | 148 +++++++++++++++++++++ railties/test/railties/engine_test.rb | 17 ++- 2 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 railties/test/application/middleware/cache_test.rb (limited to 'railties/test') diff --git a/railties/test/application/middleware/cache_test.rb b/railties/test/application/middleware/cache_test.rb new file mode 100644 index 0000000000..655f9bcd55 --- /dev/null +++ b/railties/test/application/middleware/cache_test.rb @@ -0,0 +1,148 @@ +require 'isolation/abstract_unit' + +module ApplicationTests + class RoutingTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + require 'rack/test' + extend Rack::Test::Methods + end + + def app(env = "production") + old_env = ENV["RAILS_ENV"] + + @app ||= begin + ENV["RAILS_ENV"] = env + require "#{app_path}/config/environment" + Rails.application + end + ensure + ENV["RAILS_ENV"] = old_env + end + + def simple_controller + controller :foo, <<-RUBY + class ExpiresController < ApplicationController + def expires_header + expires_in 10, :public => !params[:private] + render :text => ActiveSupport::SecureRandom.hex(16) + end + + def expires_etag + render_conditionally(:etag => "1") + end + + def expires_last_modified + $last_modified ||= Time.now.utc + render_conditionally(:last_modified => $last_modified) + end + private + def render_conditionally(headers) + if stale?(headers.merge(:public => !params[:private])) + render :text => ActiveSupport::SecureRandom.hex(16) + end + end + end + RUBY + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + match ':controller(/:action)' + end + RUBY + end + + def test_cache_works_with_expires + simple_controller + + get "/expires/expires_header" + assert_equal "miss, store", last_response.headers["X-Rack-Cache"] + assert_equal "max-age=10, public", last_response.headers["Cache-Control"] + + 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 + + get "/expires/expires_header", :private => true + assert_equal "miss", last_response.headers["X-Rack-Cache"] + assert_equal "private, max-age=10", last_response.headers["Cache-Control"] + + body = last_response.body + + get "/expires/expires_header", :private => true + 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 + + get "/expires/expires_etag" + assert_equal "miss, store", last_response.headers["X-Rack-Cache"] + assert_equal "public", last_response.headers["Cache-Control"] + + 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 + + get "/expires/expires_etag", :private => true + 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"] + + get "/expires/expires_etag", {:private => true}, "If-None-Match" => etag + 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 + + get "/expires/expires_last_modified" + assert_equal "miss, store", last_response.headers["X-Rack-Cache"] + assert_equal "public", last_response.headers["Cache-Control"] + + 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 + + get "/expires/expires_last_modified", :private => true + 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"] + + get "/expires/expires_last_modified", {:private => true}, "If-Modified-Since" => last + assert_equal "miss", last_response.headers["X-Rack-Cache"] + assert_not_equal body, last_response.body + end + end +end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 6fc166e054..6b90b32005 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -328,15 +328,23 @@ module RailtiesTest env = Rack::MockRequest.env_for("/app.html") response = Rails.application.call(env) - assert_equal response[2].path, File.join(app_path, "public/app.html") + assert_equal rack_body(response[2]), rack_body(File.open(File.join(app_path, "public/app.html"))) env = Rack::MockRequest.env_for("/bukkits/bukkits.html") response = Rails.application.call(env) - assert_equal response[2].path, File.join(@plugin.path, "public/bukkits.html") + assert_equal rack_body(response[2]), rack_body(File.open(File.join(@plugin.path, "public/bukkits.html"))) env = Rack::MockRequest.env_for("/bukkits/file_from_app.html") response = Rails.application.call(env) - assert_equal response[2].path, File.join(app_path, "public/bukkits/file_from_app.html") + assert_equal rack_body(response[2]), rack_body(File.open(File.join(app_path, "public/bukkits/file_from_app.html"))) + end + + def rack_body(obj) + buffer = "" + obj.each do |part| + buffer << part + end + buffer end test "shared engine should include application's helpers and own helpers" do @@ -570,7 +578,8 @@ module RailtiesTest env = Rack::MockRequest.env_for("/bukkits/posts/new") response = AppTemplate::Application.call(env) - assert response[2].body =~ /name="post\[title\]"/ + p rack_body(response[2]) + assert rack_body(response[2]) =~ /name="post\[title\]"/ end test "creating symlinks" do -- cgit v1.2.3