2010-01-04 20:34:42 -05:00
require 'isolation/abstract_unit'
2010-06-09 16:48:50 -04:00
require 'stringio'
2011-12-20 14:17:17 -05:00
require 'rack/test'
2010-01-04 20:34:42 -05:00
module ApplicationTests
2012-01-05 20:30:17 -05:00
class MiddlewareTest < ActiveSupport :: TestCase
2010-01-04 20:34:42 -05:00
include ActiveSupport :: Testing :: Isolation
def setup
build_app
boot_rails
FileUtils . rm_rf " #{ app_path } /config/environments "
end
2011-06-06 08:54:05 -04:00
def teardown
teardown_app
end
2010-04-26 03:04:04 -04:00
def app
@app || = Rails . application
end
2010-01-04 20:34:42 -05:00
test " default middleware stack " do
2011-07-25 23:18:56 -04:00
add_to_config " config.action_dispatch.x_sendfile_header = 'X-Sendfile' "
2010-01-04 20:34:42 -05:00
boot!
2010-09-15 15:58:49 -04:00
assert_equal [
2012-03-03 14:05:45 -05:00
" Rack::Sendfile " ,
2010-09-15 15:58:49 -04:00
" ActionDispatch::Static " ,
" Rack::Lock " ,
" ActiveSupport::Cache::Strategy::LocalCache " ,
" Rack::Runtime " ,
2011-05-07 04:19:01 -04:00
" Rack::MethodOverride " ,
2011-10-20 03:29:27 -04:00
" ActionDispatch::RequestId " ,
2011-05-07 04:19:01 -04:00
" Rails::Rack::Logger " , # must come after Rack::MethodOverride to properly log overridden methods
2010-09-15 15:58:49 -04:00
" ActionDispatch::ShowExceptions " ,
2011-12-01 14:46:18 -05:00
" ActionDispatch::DebugExceptions " ,
2010-09-15 15:58:49 -04:00
" ActionDispatch::RemoteIp " ,
2010-11-23 21:04:05 -05:00
" ActionDispatch::Reloader " ,
2010-09-15 15:58:49 -04:00
" ActionDispatch::Callbacks " ,
" ActiveRecord::ConnectionAdapters::ConnectionManagement " ,
" ActiveRecord::QueryCache " ,
" ActionDispatch::Cookies " ,
" ActionDispatch::Session::CookieStore " ,
" ActionDispatch::Flash " ,
" ActionDispatch::ParamsParser " ,
" ActionDispatch::Head " ,
2010-09-22 15:40:14 -04:00
" Rack::ConditionalGet " ,
" Rack::ETag " ,
2010-09-15 15:58:49 -04:00
" ActionDispatch::BestStandardsSupport "
] , middleware
end
2011-07-25 23:18:56 -04:00
test " Rack::Sendfile is not included by default " do
boot!
assert ! middleware . include? ( " Rack::Sendfile " ) , " Rack::Sendfile is not included in the default stack unless you set config.action_dispatch.x_sendfile_header "
end
2010-09-15 15:58:49 -04:00
test " Rack::Cache is present when action_controller.perform_caching is set " do
add_to_config " config.action_controller.perform_caching = true "
boot!
2011-05-23 04:27:46 -04:00
assert_equal " Rack::Cache " , middleware . first
2010-01-04 20:34:42 -05:00
end
2012-03-16 22:22:25 -04:00
test " ActionDispatch::SSL is present when force_ssl is set " do
2011-03-27 12:55:46 -04:00
add_to_config " config.force_ssl = true "
boot!
2012-03-16 22:22:25 -04:00
assert middleware . include? ( " ActionDispatch::SSL " )
2011-03-27 12:55:46 -04:00
end
2012-03-16 22:22:25 -04:00
test " ActionDispatch::SSL is configured with options when given " do
2011-09-26 22:22:52 -04:00
add_to_config " config.force_ssl = true "
add_to_config " config.ssl_options = { :host => 'example.com' } "
boot!
2011-12-20 14:17:17 -05:00
2011-09-26 22:22:52 -04:00
assert_equal AppTemplate :: Application . middleware . first . args , [ { :host = > 'example.com' } ]
end
2010-07-25 16:08:34 -04:00
test " removing Active Record omits its middleware " do
2010-01-04 20:34:42 -05:00
use_frameworks [ ]
boot!
assert ! middleware . include? ( " ActiveRecord::ConnectionAdapters::ConnectionManagement " )
assert ! middleware . include? ( " ActiveRecord::QueryCache " )
end
test " removes lock if allow concurrency is set " do
2010-02-01 05:40:27 -05:00
add_to_config " config.allow_concurrency = true "
2010-01-04 20:34:42 -05:00
boot!
assert ! middleware . include? ( " Rack::Lock " )
end
test " removes static asset server if serve_static_assets is disabled " do
add_to_config " config.serve_static_assets = false "
boot!
assert ! middleware . include? ( " ActionDispatch::Static " )
end
2010-06-07 17:17:23 -04:00
test " can delete a middleware from the stack " do
add_to_config " config.middleware.delete ActionDispatch::Static "
boot!
assert ! middleware . include? ( " ActionDispatch::Static " )
end
2011-12-01 14:46:18 -05:00
test " includes exceptions middlewares even if action_dispatch.show_exceptions is disabled " do
2010-04-02 14:54:10 -04:00
add_to_config " config.action_dispatch.show_exceptions = false "
boot!
2011-02-22 15:02:39 -05:00
assert middleware . include? ( " ActionDispatch::ShowExceptions " )
2011-12-01 14:46:18 -05:00
assert middleware . include? ( " ActionDispatch::DebugExceptions " )
2010-04-02 14:54:10 -04:00
end
2010-11-23 21:04:05 -05:00
test " removes ActionDispatch::Reloader if cache_classes is true " do
add_to_config " config.cache_classes = true "
boot!
assert ! middleware . include? ( " ActionDispatch::Reloader " )
end
2010-01-04 20:34:42 -05:00
test " use middleware " do
use_frameworks [ ]
add_to_config " config.middleware.use Rack::Config "
boot!
assert_equal " Rack::Config " , middleware . last
end
2010-01-04 20:38:56 -05:00
test " insert middleware after " do
2011-05-23 04:27:46 -04:00
add_to_config " config.middleware.insert_after ActionDispatch::Static, Rack::Config "
2010-01-04 20:38:56 -05:00
boot!
2010-09-15 15:58:49 -04:00
assert_equal " Rack::Config " , middleware . second
2010-01-04 20:38:56 -05:00
end
2012-01-17 11:53:09 -05:00
test " Rails.cache does not respond to middleware " do
2010-06-24 12:42:09 -04:00
add_to_config " config.cache_store = :memory_store "
boot!
2011-05-23 04:27:46 -04:00
assert_equal " Rack::Runtime " , middleware . third
2010-06-24 12:42:09 -04:00
end
2012-01-17 11:53:09 -05:00
test " Rails.cache does respond to middleware " do
2010-06-24 12:42:09 -04:00
boot!
2011-05-23 04:27:46 -04:00
assert_equal " Rack::Runtime " , middleware . fourth
2010-06-24 12:42:09 -04:00
end
2010-01-04 20:38:56 -05:00
test " insert middleware before " do
2011-05-23 04:27:46 -04:00
add_to_config " config.middleware.insert_before ActionDispatch::Static, Rack::Config "
2010-01-04 20:38:56 -05:00
boot!
2010-09-15 15:58:49 -04:00
assert_equal " Rack::Config " , middleware . first
2010-01-04 20:38:56 -05:00
end
2012-04-20 15:17:03 -04:00
test " can't change middleware after it's built " do
boot!
assert_raise RuntimeError do
app . config . middleware . use Rack :: Config
end
end
2010-09-22 15:40:14 -04:00
# ConditionalGet + Etag
test " conditional get + etag middlewares handle http caching based on body " do
make_basic_app
class :: OmgController < ActionController :: Base
def index
if params [ :nothing ]
render :text = > " "
else
render :text = > " OMG "
end
end
end
etag = " 5af83e3196bf99f440f31f2e1a6c9afe " . inspect
get " / "
assert_equal 200 , last_response . status
assert_equal " OMG " , last_response . body
assert_equal " text/html; charset=utf-8 " , last_response . headers [ " Content-Type " ]
assert_equal " max-age=0, private, must-revalidate " , last_response . headers [ " Cache-Control " ]
assert_equal etag , last_response . headers [ " Etag " ]
get " / " , { } , " HTTP_IF_NONE_MATCH " = > etag
assert_equal 304 , last_response . status
assert_equal " " , last_response . body
assert_equal nil , last_response . headers [ " Content-Type " ]
assert_equal " max-age=0, private, must-revalidate " , last_response . headers [ " Cache-Control " ]
assert_equal etag , last_response . headers [ " Etag " ]
get " /?nothing=true "
assert_equal 200 , last_response . status
assert_equal " " , last_response . body
assert_equal " text/html; charset=utf-8 " , last_response . headers [ " Content-Type " ]
assert_equal " no-cache " , last_response . headers [ " Cache-Control " ]
assert_equal nil , last_response . headers [ " Etag " ]
end
2011-12-20 14:17:17 -05:00
test " ORIGINAL_FULLPATH is passed to env " do
boot!
env = :: Rack :: MockRequest . env_for ( " /foo/?something " )
Rails . application . call ( env )
assert_equal " /foo/?something " , env [ " ORIGINAL_FULLPATH " ]
end
2010-01-04 20:34:42 -05:00
private
2010-04-26 03:04:04 -04:00
2010-01-04 20:34:42 -05:00
def boot!
require " #{ app_path } /config/environment "
end
def middleware
2010-05-29 16:29:14 -04:00
AppTemplate :: Application . middleware . map ( & :klass ) . map ( & :name )
2010-01-04 20:34:42 -05:00
end
end
2011-06-06 08:54:05 -04:00
end