2011-11-30 13:51:01 -05:00
|
|
|
# encoding: utf-8
|
2011-02-22 15:02:39 -05:00
|
|
|
require 'isolation/abstract_unit'
|
2011-11-30 13:51:01 -05:00
|
|
|
require 'rack/test'
|
2011-02-22 15:02:39 -05:00
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class MiddlewareExceptionsTest < ActiveSupport::TestCase
|
2011-02-22 15:02:39 -05:00
|
|
|
include ActiveSupport::Testing::Isolation
|
2011-11-30 13:51:01 -05:00
|
|
|
include Rack::Test::Methods
|
2011-02-22 15:02:39 -05:00
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2011-12-01 13:16:19 -05:00
|
|
|
test "show exceptions middleware filter backtrace before logging" do
|
|
|
|
my_middleware = Struct.new(:app) do
|
|
|
|
def call(env)
|
|
|
|
raise "Failure"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
app.config.middleware.use my_middleware
|
|
|
|
|
|
|
|
stringio = StringIO.new
|
|
|
|
Rails.logger = Logger.new(stringio)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert_no_match(/action_dispatch/, stringio.string)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "renders active record exceptions as 404" do
|
|
|
|
my_middleware = Struct.new(:app) do
|
|
|
|
def call(env)
|
|
|
|
raise ActiveRecord::RecordNotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
app.config.middleware.use my_middleware
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert_equal 404, last_response.status
|
|
|
|
end
|
|
|
|
|
2011-12-16 03:41:05 -05:00
|
|
|
test "uses custom exceptions app" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.exceptions_app = lambda do |env|
|
2011-12-16 03:45:14 -05:00
|
|
|
[404, { "Content-Type" => "text/plain" }, ["YOU FAILED BRO"]]
|
2011-12-16 03:41:05 -05:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app.config.action_dispatch.show_exceptions = true
|
|
|
|
|
|
|
|
get "/foo"
|
|
|
|
assert_equal 404, last_response.status
|
|
|
|
assert_equal "YOU FAILED BRO", last_response.body
|
|
|
|
end
|
|
|
|
|
2011-12-15 13:43:49 -05:00
|
|
|
test "unspecified route when action_dispatch.show_exceptions is not set raises an exception" do
|
2011-11-30 13:51:01 -05:00
|
|
|
app.config.action_dispatch.show_exceptions = false
|
2011-02-22 15:02:39 -05:00
|
|
|
|
|
|
|
assert_raise(ActionController::RoutingError) do
|
|
|
|
get '/foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-15 13:43:49 -05:00
|
|
|
test "unspecified route when action_dispatch.show_exceptions is set shows 404" do
|
2011-11-30 13:51:01 -05:00
|
|
|
app.config.action_dispatch.show_exceptions = true
|
2011-02-22 15:02:39 -05:00
|
|
|
|
|
|
|
assert_nothing_raised(ActionController::RoutingError) do
|
|
|
|
get '/foo'
|
2011-12-15 13:43:49 -05:00
|
|
|
assert_match "The page you were looking for doesn't exist.", last_response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unspecified route when action_dispatch.show_exceptions and consider_all_requests_local are set shows diagnostics" do
|
|
|
|
app.config.action_dispatch.show_exceptions = true
|
|
|
|
app.config.consider_all_requests_local = true
|
|
|
|
|
|
|
|
assert_nothing_raised(ActionController::RoutingError) do
|
|
|
|
get '/foo'
|
|
|
|
assert_match "No route matches", last_response.body
|
2011-02-22 15:02:39 -05:00
|
|
|
end
|
|
|
|
end
|
2011-11-30 13:51:01 -05:00
|
|
|
|
|
|
|
test "displays diagnostics message when exception raised in template that contains UTF-8" do
|
|
|
|
app.config.action_dispatch.show_exceptions = true
|
2011-12-16 04:38:17 -05:00
|
|
|
app.config.consider_all_requests_local = true
|
2011-11-30 13:51:01 -05:00
|
|
|
|
|
|
|
controller :foo, <<-RUBY
|
|
|
|
class FooController < ActionController::Base
|
|
|
|
def index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'app/views/foo/index.html.erb', <<-ERB
|
|
|
|
<% raise 'boooom' %>
|
2011-12-19 23:33:34 -05:00
|
|
|
✓測試テスト시험
|
2011-11-30 13:51:01 -05:00
|
|
|
ERB
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
|
|
|
AppTemplate::Application.routes.draw do
|
|
|
|
match ':controller(/:action)'
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
post '/foo', :utf8 => '✓'
|
|
|
|
assert_match(/boooom/, last_response.body)
|
2011-12-19 23:33:34 -05:00
|
|
|
assert_match(/測試テスト시험/, last_response.body)
|
2011-11-30 13:51:01 -05:00
|
|
|
end
|
2011-02-22 15:02:39 -05:00
|
|
|
end
|
|
|
|
end
|