1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix a routing test. Reorganize middleware tests.

This commit is contained in:
José Valim 2010-10-02 17:42:36 +02:00
parent 4e93179ed3
commit 609849a0f1
7 changed files with 180 additions and 151 deletions

View file

@ -0,0 +1,26 @@
require 'isolation/abstract_unit'
module ApplicationTests
class BestPracticesTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
require 'rack/test'
extend Rack::Test::Methods
simple_controller
end
test "simple controller in production mode returns best standards" do
get '/foo'
assert_equal "IE=Edge,chrome=1", last_response.headers["X-UA-Compatible"]
end
test "simple controller in development mode leaves out Chrome" do
app("development")
get "/foo"
assert_equal "IE=Edge", last_response.headers["X-UA-Compatible"]
end
end
end

View file

@ -11,18 +11,6 @@ module ApplicationTests
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 :expires, <<-RUBY
class ExpiresController < ApplicationController

View file

@ -0,0 +1,63 @@
require 'isolation/abstract_unit'
module ApplicationTests
class RemoteIpTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf "#{app_path}/config/environments"
end
def app
@app ||= Rails.application
end
def remote_ip(env = {})
remote_ip = nil
env = Rack::MockRequest.env_for("/").merge(env).merge!(
'action_dispatch.show_exceptions' => false,
'action_dispatch.secret_token' => 'b3c631c314c0bbca50c1b2843150fe33'
)
endpoint = Proc.new do |e|
remote_ip = ActionDispatch::Request.new(e).remote_ip
[200, {}, ["Hello"]]
end
Rails.application.middleware.build(endpoint).call(env)
remote_ip
end
test "remote_ip works" do
make_basic_app
assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "1.1.1.1")
end
test "checks IP spoofing by default" do
make_basic_app
assert_raises(ActionDispatch::RemoteIp::IpSpoofAttackError) do
remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
end
end
test "can disable IP spoofing check" do
make_basic_app do |app|
app.config.action_dispatch.ip_spoofing_check = false
end
assert_nothing_raised(ActionDispatch::RemoteIp::IpSpoofAttackError) do
assert_equal "1.1.1.2", remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
end
end
test "the user can set trusted proxies" do
make_basic_app do |app|
app.config.action_dispatch.trusted_proxies = /^4\.2\.42\.42$/
end
assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "4.2.42.42,1.1.1.1")
end
end
end

View file

@ -0,0 +1,56 @@
require 'isolation/abstract_unit'
module ApplicationTests
class SendfileTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf "#{app_path}/config/environments"
end
def app
@app ||= Rails.application
end
define_method :simple_controller do
class ::OmgController < ActionController::Base
def index
send_file __FILE__
end
end
end
# x_sendfile_header middleware
test "config.action_dispatch.x_sendfile_header defaults to ''" do
make_basic_app
simple_controller
get "/"
assert_equal File.read(__FILE__), last_response.body
end
test "config.action_dispatch.x_sendfile_header can be set" do
make_basic_app do |app|
app.config.action_dispatch.x_sendfile_header = "X-Sendfile"
end
simple_controller
get "/"
assert_equal File.expand_path(__FILE__), last_response.headers["X-Sendfile"]
end
test "config.action_dispatch.x_sendfile_header is sent to Rack::Sendfile" do
make_basic_app do |app|
app.config.action_dispatch.x_sendfile_header = 'X-Lighttpd-Send-File'
end
simple_controller
get "/"
assert_equal File.expand_path(__FILE__), last_response.headers["X-Lighttpd-Send-File"]
end
end
end

View file

@ -129,81 +129,6 @@ module ApplicationTests
assert_equal "Rack::Config", middleware.first
end
# x_sendfile_header middleware
test "config.action_dispatch.x_sendfile_header defaults to ''" do
make_basic_app
class ::OmgController < ActionController::Base
def index
send_file __FILE__
end
end
get "/"
assert_equal File.read(__FILE__), last_response.body
end
test "config.action_dispatch.x_sendfile_header can be set" do
make_basic_app do |app|
app.config.action_dispatch.x_sendfile_header = "X-Sendfile"
end
class ::OmgController < ActionController::Base
def index
send_file __FILE__
end
end
get "/"
assert_equal File.expand_path(__FILE__), last_response.headers["X-Sendfile"]
end
test "config.action_dispatch.x_sendfile_header is sent to Rack::Sendfile" do
make_basic_app do |app|
app.config.action_dispatch.x_sendfile_header = 'X-Lighttpd-Send-File'
end
class ::OmgController < ActionController::Base
def index
send_file __FILE__
end
end
get "/"
assert_equal File.expand_path(__FILE__), last_response.headers["X-Lighttpd-Send-File"]
end
# remote_ip tests
test "remote_ip works" do
make_basic_app
assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "1.1.1.1")
end
test "checks IP spoofing by default" do
make_basic_app
assert_raises(ActionDispatch::RemoteIp::IpSpoofAttackError) do
remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
end
end
test "can disable IP spoofing check" do
make_basic_app do |app|
app.config.action_dispatch.ip_spoofing_check = false
end
assert_nothing_raised(ActionDispatch::RemoteIp::IpSpoofAttackError) do
assert_equal "1.1.1.2", remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
end
end
test "the user can set trusted proxies" do
make_basic_app do |app|
app.config.action_dispatch.trusted_proxies = /^4\.2\.42\.42$/
end
assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "4.2.42.42,1.1.1.1")
end
test "show exceptions middleware filter backtrace before logging" do
my_middleware = Struct.new(:app) do
def call(env)
@ -232,21 +157,5 @@ module ApplicationTests
def middleware
AppTemplate::Application.middleware.map(&:klass).map(&:name)
end
def remote_ip(env = {})
remote_ip = nil
env = Rack::MockRequest.env_for("/").merge(env).merge!(
'action_dispatch.show_exceptions' => false,
'action_dispatch.secret_token' => 'b3c631c314c0bbca50c1b2843150fe33'
)
endpoint = Proc.new do |e|
remote_ip = ActionDispatch::Request.new(e).remote_ip
[200, {}, ["Hello"]]
end
Rails.application.middleware.build(endpoint).call(env)
remote_ip
end
end
end

View file

@ -11,34 +11,6 @@ module ApplicationTests
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 FooController < ApplicationController
def index
render :text => "foo"
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do
match ':controller(/:action)'
end
RUBY
end
test "rails/info/properties in development" do
app("development")
get "/rails/info/properties"
@ -58,21 +30,6 @@ module ApplicationTests
assert_equal 'foo', last_response.body
end
test "simple controller in production mode returns best standards" do
simple_controller
get '/foo'
assert_equal "IE=Edge,chrome=1", last_response.headers["X-UA-Compatible"]
end
test "simple controller in development mode leaves out Chrome" do
simple_controller
app("development")
get "/foo"
assert_equal "IE=Edge", last_response.headers["X-UA-Compatible"]
end
test "simple controller with helper" do
controller :foo, <<-RUBY
class FooController < ApplicationController
@ -177,7 +134,7 @@ module ApplicationTests
assert_equal 'admin::foo', last_response.body
end
def test_reloads_appended_route_blocks
test "routes appending blocks" do
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do
match ':controller#:action'
@ -246,10 +203,13 @@ module ApplicationTests
test 'routes are loaded just after initialization' do
require "#{app_path}/config/application"
app_file 'config/routes.rb', <<-RUBY
InitializeRackApp = lambda { |env| [200, {}, ["InitializeRackApp"]] }
# Create the rack app just inside after initialize callback
ActiveSupport.on_load(:after_initialize) do
::InitializeRackApp = lambda { |env| [200, {}, ["InitializeRackApp"]] }
end
AppTemplate::Application.routes.draw do
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do |map|
match 'foo', :to => ::InitializeRackApp
end
RUBY
@ -258,7 +218,7 @@ module ApplicationTests
assert_equal "InitializeRackApp", last_response.body
end
test 'resource routing with irrigular inflection' do
test 'resource routing with irregular inflection' do
app_file 'config/initializers/inflection.rb', <<-RUBY
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'yazi', 'yazilar'

View file

@ -45,6 +45,17 @@ module TestHelpers
end
module Rack
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 extract_body(response)
"".tap do |body|
response[2].each {|chunk| body << chunk }
@ -124,6 +135,22 @@ module TestHelpers
extend ::Rack::Test::Methods
end
def simple_controller
controller :foo, <<-RUBY
class FooController < ApplicationController
def index
render :text => "foo"
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do
match ':controller(/:action)'
end
RUBY
end
class Bukkit
attr_reader :path