diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index f7173fbca3..fc17827785 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -9,7 +9,7 @@ class ErrorsController < ApplicationController event.add_metadata(:request, request.env) end - render status: :not_found + render "not_found", status: :not_found, formats: :html end def internal_server_error diff --git a/spec/requests/errors_spec.rb b/spec/requests/errors_spec.rb new file mode 100644 index 0000000000..0d44cc5ada --- /dev/null +++ b/spec/requests/errors_spec.rb @@ -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 diff --git a/spec/support/request/exception_helper.rb b/spec/support/request/exception_helper.rb new file mode 100644 index 0000000000..645cde2813 --- /dev/null +++ b/spec/support/request/exception_helper.rb @@ -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