aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/show_exceptions_test.rb
blob: 2cd2114db67bf879579d9b7e7f946a14ec90090a (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
# frozen_string_literal: true

require "abstract_unit"

module ShowExceptions
  class ShowExceptionsController < ActionController::Base
    use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
    use ActionDispatch::DebugExceptions

    before_action only: :another_boom do
      request.env["action_dispatch.show_detailed_exceptions"] = true
    end

    def boom
      raise "boom!"
    end

    def another_boom
      raise "boom!"
    end

    def show_detailed_exceptions?
      request.local?
    end
  end

  class ShowExceptionsTest < ActionDispatch::IntegrationTest
    test "show error page from a remote ip" do
      @app = ShowExceptionsController.action(:boom)
      self.remote_addr = "208.77.188.166"
      get "/"
      assert_equal "500 error fixture\n", body
    end

    test "show diagnostics from a local ip if show_detailed_exceptions? is set to request.local?" do
      @app = ShowExceptionsController.action(:boom)
      ["127.0.0.1", "127.0.0.127", "127.12.1.1", "::1", "0:0:0:0:0:0:0:1", "0:0:0:0:0:0:0:1%0"].each do |ip_address|
        self.remote_addr = ip_address
        get "/"
        assert_match(/boom/, body)
      end
    end

    test "show diagnostics from a remote ip when env is already set" do
      @app = ShowExceptionsController.action(:another_boom)
      self.remote_addr = "208.77.188.166"
      get "/"
      assert_match(/boom/, body)
    end
  end

  class ShowExceptionsOverriddenController < ShowExceptionsController
    private

      def show_detailed_exceptions?
        params["detailed"] == "1"
      end
  end

  class ShowExceptionsOverriddenTest < ActionDispatch::IntegrationTest
    test "show error page" do
      @app = ShowExceptionsOverriddenController.action(:boom)
      get "/", params: { "detailed" => "0" }
      assert_equal "500 error fixture\n", body
    end

    test "show diagnostics message" do
      @app = ShowExceptionsOverriddenController.action(:boom)
      get "/", params: { "detailed" => "1" }
      assert_match(/boom/, body)
    end
  end

  class ShowExceptionsFormatsTest < ActionDispatch::IntegrationTest
    def test_render_json_exception
      @app = ShowExceptionsOverriddenController.action(:boom)
      get "/", headers: { "HTTP_ACCEPT" => "application/json" }
      assert_response :internal_server_error
      assert_equal "application/json", response.media_type
      assert_equal({ status: 500, error: "Internal Server Error" }.to_json, response.body)
    end

    def test_render_xml_exception
      @app = ShowExceptionsOverriddenController.action(:boom)
      get "/", headers: { "HTTP_ACCEPT" => "application/xml" }
      assert_response :internal_server_error
      assert_equal "application/xml", response.media_type
      assert_equal({ status: 500, error: "Internal Server Error" }.to_xml, response.body)
    end

    def test_render_fallback_exception
      @app = ShowExceptionsOverriddenController.action(:boom)
      get "/", headers: { "HTTP_ACCEPT" => "text/csv" }
      assert_response :internal_server_error
      assert_equal "text/html", response.media_type
    end
  end

  class ShowFailsafeExceptionsTest < ActionDispatch::IntegrationTest
    def test_render_failsafe_exception
      @app = ShowExceptionsOverriddenController.action(:boom)
      middleware = @app
      @exceptions_app = middleware.instance_variable_get(:@exceptions_app)
      middleware.instance_variable_set(:@exceptions_app, nil)
      $stderr = StringIO.new

      get "/", headers: { "HTTP_ACCEPT" => "text/json" }
      assert_response :internal_server_error
      assert_equal "text/plain", response.media_type
    ensure
      middleware.instance_variable_set(:@exceptions_app, @exceptions_app)
      $stderr = STDERR
    end
  end
end