aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/show_exceptions_test.rb
blob: c70bc5f95e93a6c8aa7e05faabcfa2f4f975f1d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require 'abstract_unit'

class ShowExceptionsTest < ActionDispatch::IntegrationTest

  class Boomer
    def call(env)
      req = ActionDispatch::Request.new(env)
      case req.path
      when "/not_found"
        raise AbstractController::ActionNotFound
      when "/bad_params"
        raise ActionDispatch::ParamsParser::ParseError.new("", StandardError.new)
      when "/method_not_allowed"
        raise ActionController::MethodNotAllowed, 'PUT'
      when "/unknown_http_method"
        raise ActionController::UnknownHttpMethod
      when "/not_found_original_exception"
        raise ActionView::Template::Error.new('template', AbstractController::ActionNotFound.new)
      else
        raise "puke!"
      end
    end
  end

  ProductionApp    = ActionDispatch::ShowExceptions.new(Boomer.new, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public"))
  ProductionApiApp = ActionDispatch::ShowExceptions.new(Boomer.new, ActionDispatch::ApiPublicExceptions.new("#{FIXTURE_LOAD_PATH}/public"))

  test "skip exceptions app if not showing exceptions" do
    @app = ProductionApp
    assert_raise RuntimeError do
      get "/", headers: { 'action_dispatch.show_exceptions' => false }
    end
  end

  test "rescue with error page" do
    @app = ProductionApp

    get "/", headers: { 'action_dispatch.show_exceptions' => true }
    assert_response 500
    assert_equal "500 error fixture\n", body

    get "/bad_params", headers: { 'action_dispatch.show_exceptions' => true }
    assert_response 400
    assert_equal "400 error fixture\n", body

    get "/not_found", headers: { 'action_dispatch.show_exceptions' => true }
    assert_response 404
    assert_equal "404 error fixture\n", body

    get "/method_not_allowed", headers: { 'action_dispatch.show_exceptions' => true }
    assert_response 405
    assert_equal "", body

    get "/unknown_http_method", headers: { 'action_dispatch.show_exceptions' => true }
    assert_response 405
    assert_equal "", body
  end

  test "rescue api apps with json response" do
    @app = ProductionApiApp

    get "/", headers: { 'HTTP_ACCEPT' => 'application/json', 'action_dispatch.show_exceptions' => true }
    assert_response 500
    assert_equal({ :status => '500', :error => 'puke!' }.to_json, body)

    get "/method_not_allowed", headers: { 'HTTP_ACCEPT' => 'application/json', 'action_dispatch.show_exceptions' => true }
    assert_response 405
    assert_equal({ :status => '405', :error => 'Only PUT requests are allowed.' }.to_json, body)

    get "/unknown_http_method", headers: { 'HTTP_ACCEPT' => 'application/json', 'action_dispatch.show_exceptions' => true }
    assert_response 405
    assert_equal({ :status => '405', :error => 'ActionController::UnknownHttpMethod' }.to_json, body)
  end

  test "rescue api apps unknown content-type requests with html response" do
    @app = ProductionApiApp

    get "/", headers: { 'HTTP_ACCEPT' => 'application/x-custom', 'action_dispatch.show_exceptions' => true }
    assert_response 500
    assert_equal "500 error fixture\n", body

    get "/bad_params", headers: { 'HTTP_ACCEPT' => 'application/x-custom', 'action_dispatch.show_exceptions' => true }
    assert_response 400
    assert_equal "400 error fixture\n", body

    get "/not_found", headers: { 'HTTP_ACCEPT' => 'application/x-custom', 'action_dispatch.show_exceptions' => true }
    assert_response 404
    assert_equal "404 error fixture\n", body

    get "/unknown_http_method", headers: { 'HTTP_ACCEPT' => 'application/x-custom', 'action_dispatch.show_exceptions' => true }
    assert_response 405
    assert_equal("", body)
  end

  test "localize rescue error page" do
    old_locale, I18n.locale = I18n.locale, :da

    begin
      @app = ProductionApp

      get "/", headers: { 'action_dispatch.show_exceptions' => true }
      assert_response 500
      assert_equal "500 localized error fixture\n", body

      get "/not_found", headers: { 'action_dispatch.show_exceptions' => true }
      assert_response 404
      assert_equal "404 error fixture\n", body
    ensure
      I18n.locale = old_locale
    end
  end

  test "sets the HTTP charset parameter" do
    @app = ProductionApp

    get "/", headers: { 'action_dispatch.show_exceptions' => true }
    assert_equal "text/html; charset=utf-8", response.headers["Content-Type"]
  end

  test "show registered original exception for wrapped exceptions" do
    @app = ProductionApp

    get "/not_found_original_exception", headers: { 'action_dispatch.show_exceptions' => true }
    assert_response 404
    assert_match(/404 error/, body)
  end

  test "calls custom exceptions app" do
    exceptions_app = lambda do |env|
      assert_kind_of AbstractController::ActionNotFound, env["action_dispatch.exception"]
      assert_equal "/404", env["PATH_INFO"]
      assert_equal "/not_found_original_exception", env["action_dispatch.original_path"]
      [404, { "Content-Type" => "text/plain" }, ["YOU FAILED BRO"]]
    end

    @app = ActionDispatch::ShowExceptions.new(Boomer.new, exceptions_app)
    get "/not_found_original_exception", headers: { 'action_dispatch.show_exceptions' => true }
    assert_response 404
    assert_equal "YOU FAILED BRO", body
  end

  test "returns an empty response if custom exceptions app returns X-Cascade pass" do
    exceptions_app = lambda do |env|
      [404, { "X-Cascade" => "pass" }, []]
    end

    @app = ActionDispatch::ShowExceptions.new(Boomer.new, exceptions_app)
    get "/method_not_allowed", headers: { 'action_dispatch.show_exceptions' => true }
    assert_response 405
    assert_equal "", body
  end
end