Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP::Request#route_uri_pattern that returns URI pattern of matched route. #47129

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Add HTTP::Request#route_uri_pattern that returns URI pattern of matched route.

*Joel Hawksley*, *Kate Higa*

* Add `ActionDispatch::AssumeSSL` middleware that can be turned on via `config.assume_ssl`.
It makes the application believe that all requests are arring over SSL. This is useful
when proxying through a load balancer that terminates SSL, the forwarded request will appear
Expand Down
12 changes: 12 additions & 0 deletions actionpack/lib/action_dispatch/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ def request_method
@request_method ||= check_method(super)
end

# Returns the URI pattern of the matched route for the request,
# using the same format as `bin/rails routes`:
#
# request.route_uri_pattern # => "/:controller(/:action(/:id))(.:format)"
def route_uri_pattern
get_header("action_dispatch.route_uri_pattern")
end

def route_uri_pattern=(pattern) # :nodoc:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joelhawksley I've opened #47306 but I was thinking we might want to add a test for this if possible? WDYT?

set_header("action_dispatch.route_uri_pattern", pattern)
end

def routes # :nodoc:
get_header("action_dispatch.routes")
end
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch/journey/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def serve(req)
}

req.path_parameters = tmp_params
req.route_uri_pattern = route.path.spec.to_s

_, headers, _ = response = route.app.serve(req)

Expand Down
11 changes: 11 additions & 0 deletions actionpack/test/controller/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@ def test_default_setup
assert_equal "/stuff", controller.url_for(controller: "/stuff", only_path: true)
end

def test_route_uri_pattern
rs.draw { ActionDispatch.deprecator.silence { get "/:controller(/:action(/:id))" } }

get URI("http://test.host/admin/user/list/10")

assert_equal(
"/:controller(/:action(/:id))(.:format)",
controller.request.route_uri_pattern
)
end

def test_route_with_colon_first
rs.draw do
ActionDispatch.deprecator.silence do
Expand Down