exceptions_test.rb 2.6 KB
Newer Older
1
# encoding: utf-8
2
require 'isolation/abstract_unit'
3
require 'rack/test'
4 5

module ApplicationTests
6
  class MiddlewareExceptionsTest < Test::Unit::TestCase
7
    include ActiveSupport::Testing::Isolation
8
    include Rack::Test::Methods
9 10 11 12 13 14

    def setup
      build_app
      boot_rails
    end

15 16 17 18
    def teardown
      teardown_app
    end

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    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

48
    test "unspecified route when action_dispatch.show_exceptions is not set raises an exception" do
49
      app.config.action_dispatch.show_exceptions = false
50 51 52 53 54 55

      assert_raise(ActionController::RoutingError) do
        get '/foo'
      end
    end

56
    test "unspecified route when action_dispatch.show_exceptions is set shows 404" do
57
      app.config.action_dispatch.show_exceptions = true
58 59 60

      assert_nothing_raised(ActionController::RoutingError) do
        get '/foo'
61 62 63 64 65 66 67 68 69 70 71
        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
72 73
      end
    end
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

    test "displays diagnostics message when exception raised in template that contains UTF-8" do
      app.config.action_dispatch.show_exceptions = true

      controller :foo, <<-RUBY
        class FooController < ActionController::Base
          def index
          end
        end
      RUBY

      app_file 'app/views/foo/index.html.erb', <<-ERB
        <% raise 'boooom' %>

      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)
    end
99 100
  end
end