Merge pull request #11379 from dacook/respond_to-not_found

Respond to all formats for "not found" error
This commit is contained in:
Konrad
2023-08-18 18:47:09 +02:00
committed by GitHub
4 changed files with 39 additions and 4 deletions

View File

@@ -9,7 +9,7 @@ class ErrorsController < ApplicationController
event.add_metadata(:request, request.env)
end
render status: :not_found
render status: :not_found, formats: :html
end
def internal_server_error

View File

@@ -0,0 +1,21 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Errors', type: :request do
include ExceptionHelper
shared_examples "returning a HTTP 404" do |path|
it path do
rails_respond_without_detailed_exceptions do
get path
end
expect(response).to have_http_status(:not_found)
end
end
it_behaves_like "returning a HTTP 404", "/nonexistent/path"
it_behaves_like "returning a HTTP 404", "/nonexistent/path.jpg"
it_behaves_like "returning a HTTP 404", "/nonexistent/path.xml"
end

View File

@@ -8,8 +8,6 @@ require 'spec_helper'
RSpec.describe 'A very large request', type: :request do
it 'should not overflow cookies' do
get '/admin', params: { foo: 'x' * ActionDispatch::Cookies::MAX_COOKIE_SIZE }
expect(response.status).to eq(302) # HTTP status 302 - Found
## Use the newer syntax if rspec gets upgraded
# expect(response).to have_http_status(:redirect)
expect(response).to have_http_status(:redirect)
end
end

View File

@@ -0,0 +1,16 @@
# frozen_string_literal: true
# https://medium.com/@edgar/rspec-how-to-have-rails-request-specs-handling-errors-like-production-a8c21ed0308a
module ExceptionHelper
def rails_respond_without_detailed_exceptions
env_config = Rails.application.env_config
original_show_exceptions = env_config['action_dispatch.show_exceptions']
original_show_detailed_exceptions = env_config['action_dispatch.show_detailed_exceptions']
env_config['action_dispatch.show_exceptions'] = true
env_config['action_dispatch.show_detailed_exceptions'] = false
yield
ensure
env_config['action_dispatch.show_exceptions'] = original_show_exceptions
env_config['action_dispatch.show_detailed_exceptions'] = original_show_detailed_exceptions
end
end