aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/rack/logger_test.rb
blob: ea425d5fa577475398f2a48af48f90337b053561 (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
# frozen_string_literal: true

require "isolation/abstract_unit"
require "active_support/log_subscriber/test_helper"
require "rack/test"

module ApplicationTests
  module RackTests
    class LoggerTest < ActiveSupport::TestCase
      include ActiveSupport::Testing::Isolation
      include ActiveSupport::LogSubscriber::TestHelper
      include Rack::Test::Methods

      def setup
        build_app
        add_to_config <<-RUBY
          config.logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
        RUBY

        require "#{app_path}/config/environment"
        super
      end

      def teardown
        super
        teardown_app
      end

      def logs
        @logs ||= Rails.logger.logged(:info).join("\n")
      end

      test "logger logs proper HTTP GET verb and path" do
        get "/blah"
        wait
        assert_match 'Started GET "/blah"', logs
      end

      test "logger logs proper HTTP HEAD verb and path" do
        head "/blah"
        wait
        assert_match 'Started HEAD "/blah"', logs
      end

      test "logger logs HTTP verb override" do
        post "/", _method: "put"
        wait
        assert_match 'Started PUT "/"', logs
      end

      test "logger logs HEAD requests" do
        post "/", _method: "head"
        wait
        assert_match 'Started HEAD "/"', logs
      end

      test "logger logs correct remote IP address" do
        get "/", {}, { "REMOTE_ADDR" => "127.0.0.1", "HTTP_X_FORWARDED_FOR" => "1.2.3.4" }
        wait
        assert_match 'Started GET "/" for 1.2.3.4', logs
      end
    end
  end
end