diff --git a/lib/devise/failure_app.rb b/lib/devise/failure_app.rb index 70cf6d2f31..f09758a4ad 100644 --- a/lib/devise/failure_app.rb +++ b/lib/devise/failure_app.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "action_controller/metal" +require "action_controller/metal/default_headers" module Devise # Failure application that will be called every time :warden is thrown from @@ -8,6 +9,7 @@ module Devise # in page based on current scope and mapping. If no scope is given, it # redirects to the default_url. class FailureApp < ActionController::Metal + include ActionController::DefaultHeaders include ActionController::UrlFor include ActionController::Redirecting diff --git a/test/failure_app_test.rb b/test/failure_app_test.rb index c9e4a56ce1..97ca30813f 100644 --- a/test/failure_app_test.rb +++ b/test/failure_app_test.rb @@ -94,6 +94,14 @@ def call_failure(env_params = {}) end context 'When redirecting' do + test 'includes default response headers' do + swap ActionDispatch::Response, default_headers: { 'X-Custom-Header' => 'custom' } do + call_failure + + assert_equal 'custom', @response.second['X-Custom-Header'] + end + end + test 'returns to the default redirect location' do call_failure assert_equal 302, @response.first @@ -260,6 +268,14 @@ def call_failure(env_params = {}) end context 'For HTTP request' do + test 'explicit response headers override default response headers' do + swap ActionDispatch::Response, default_headers: { 'WWW-Authenticate' => 'default' } do + call_failure('formats' => Mime[:json]) + + assert_equal 'Basic realm="Application"', @response.second['WWW-Authenticate'] + end + end + test 'return 401 status' do call_failure('formats' => Mime[:json]) assert_equal 401, @response.first