File tree Expand file tree Collapse file tree 5 files changed +76
-4
lines changed Expand file tree Collapse file tree 5 files changed +76
-4
lines changed Original file line number Diff line number Diff line change 1
- * Prevent ` ActionDispatch::ServerTiming ` from overwriting existing values in ` Server-Timing ` .
1
+ * Log redirects from routes the same way as redirects from controllers.
2
+
3
+ * Dennis Paagman*
2
4
5
+ * Prevent ` ActionDispatch::ServerTiming ` from overwriting existing values in ` Server-Timing ` .
3
6
Previously, if another middleware down the chain set ` Server-Timing ` header,
4
7
it would overwritten by ` ActionDispatch::ServerTiming ` .
5
8
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module ActionDispatch
4
+ class LogSubscriber < ActiveSupport ::LogSubscriber
5
+ def redirect ( event )
6
+ payload = event . payload
7
+
8
+ info { "Redirected to #{ payload [ :location ] } " }
9
+
10
+ info do
11
+ status = payload [ :status ]
12
+
13
+ message = +"Completed #{ status } #{ Rack ::Utils ::HTTP_STATUS_CODES [ status ] } in #{ event . duration . round } ms"
14
+ message << "\n \n " if defined? ( Rails . env ) && Rails . env . development?
15
+
16
+ message
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ ActionDispatch ::LogSubscriber . attach_to :action_dispatch
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require "action_dispatch"
4
+ require "action_dispatch/log_subscriber"
4
5
require "active_support/messages/rotation_configuration"
5
6
6
7
module ActionDispatch
Original file line number Diff line number Diff line change @@ -18,10 +18,17 @@ def initialize(status, block)
18
18
def redirect? ; true ; end
19
19
20
20
def call ( env )
21
- serve Request . new env
21
+ ActiveSupport ::Notifications . instrument ( "redirect.action_dispatch" ) do |payload |
22
+ response = build_response ( Request . new ( env ) )
23
+
24
+ payload [ :status ] = @status
25
+ payload [ :location ] = response . headers [ "Location" ]
26
+
27
+ response . to_a
28
+ end
22
29
end
23
30
24
- def serve ( req )
31
+ def build_response ( req )
25
32
uri = URI . parse ( path ( req . path_parameters , req ) )
26
33
27
34
unless uri . host
@@ -46,7 +53,7 @@ def serve(req)
46
53
"Content-Length" => body . length . to_s
47
54
}
48
55
49
- [ status , headers , [ body ] ]
56
+ ActionDispatch :: Response . new ( status , headers , body )
50
57
end
51
58
52
59
def path ( params , request )
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require "abstract_unit"
4
+ require "active_support/log_subscriber/test_helper"
5
+ require "action_dispatch/log_subscriber"
6
+
7
+ class TestLogSubscriber < ActionDispatch ::IntegrationTest
8
+ include ActiveSupport ::LogSubscriber ::TestHelper
9
+
10
+ def setup
11
+ super
12
+ ActionDispatch ::LogSubscriber . attach_to :action_dispatch
13
+ end
14
+
15
+ def test_redirect_logging
16
+ draw do
17
+ get "redirect" , to : redirect ( "/login" )
18
+ end
19
+
20
+ get "/redirect"
21
+ wait
22
+ assert_equal 2 , logs . size
23
+ assert_equal "Redirected to http://www.example.com/login" , logs . first
24
+ assert_match ( /Completed 301/ , logs . last )
25
+ end
26
+
27
+ private
28
+ def draw ( &block )
29
+ self . class . stub_controllers do |routes |
30
+ routes . default_url_options = { host : "www.example.com" }
31
+ routes . draw ( &block )
32
+ @app = RoutedRackApp . new routes
33
+ end
34
+ end
35
+
36
+ def logs
37
+ @logs ||= @logger . logged ( :info )
38
+ end
39
+ end
You can’t perform that action at this time.
0 commit comments