Skip to content

Change request method to a GET when passing failed requests to config.exceptions_app #40834

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

Merged
merged 1 commit into from
Dec 17, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

* Change the request method to a `GET` when passing failed requests down to `config.exceptions_app`.

*Alex Robbin*

* Add `redirect_back_or_to(fallback_location, **)` as a more aesthetically pleasing version of `redirect_back fallback_location:, **`.
The old method name is retained without explicit deprecation.

Expand Down
2 changes: 2 additions & 0 deletions actionpack/lib/action_dispatch/middleware/show_exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def render_exception(request, exception)
status = wrapper.status_code
request.set_header "action_dispatch.exception", wrapper.unwrapped_exception
request.set_header "action_dispatch.original_path", request.path_info
request.set_header "action_dispatch.original_request_method", request.raw_request_method
request.path_info = "/#{status}"
request.request_method = "GET"
response = @exceptions_app.call(request.env)
response[1]["X-Cascade"] == "pass" ? pass_response(status) : response
rescue Exception => failsafe_error
Expand Down
14 changes: 14 additions & 0 deletions railties/test/application/middleware/exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ def index
assert_equal 405, last_response.status
end

test "renders unknown http methods as 405 when routes are used as the custom exceptions app" do
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
end
RUBY

add_to_config "config.exceptions_app = self.routes"

app.config.action_dispatch.show_exceptions = true

request "/", { "REQUEST_METHOD" => "NOT_AN_HTTP_METHOD" }
assert_equal 405, last_response.status
end

test "uses custom exceptions app" do
add_to_config <<-RUBY
config.exceptions_app = lambda do |env|
Expand Down