Skip to content

Commit

Permalink
[RHICOMPL-1037] Use default headers also on API controllers
Browse files Browse the repository at this point in the history
Default headers were not being set for an API only controllers. This
issue has been fixed in Rails 6.0.

References:

rails/rails@f22bc41
rails/rails#32484
  • Loading branch information
vkrizan committed Oct 26, 2020
1 parent ecc7559 commit 57d085c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -3,6 +3,7 @@
# General controller to include all-encompassing behavior
class ApplicationController < ActionController::API
include ActionController::Helpers
include DefaultHeaders
include Pundit
include Authentication
include ExceptionNotifierCustomData
Expand Down
22 changes: 22 additions & 0 deletions app/controllers/concerns/default_headers.rb
@@ -0,0 +1,22 @@
# frozen_string_literal: true

# Original Author: Kevin Deisz
# https://github.com/rails/rails/commit/f22bc41a92e8f51d6f6da5b840f3364474d6aaba
# https://github.com/rails/rails/pull/32484

# The ActionController::DefaultHeaders is available from Rails 6.0
unless defined? ActionController::DefaultHeaders
# Allows configuring default headers that will be automatically merged into
# each response.
module DefaultHeaders
extend ActiveSupport::Concern

class_methods do
def make_response!(request)
ActionDispatch::Response.create.tap do |res|
res.request = request
end
end
end
end
end
25 changes: 25 additions & 0 deletions test/controllers/concerns/default_headers_test.rb
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'test_helper'

class DefaultHeadersTest < ActionDispatch::IntegrationTest
setup do
ApplicationController.any_instance.expects(:authenticate_user)
ApplicationController.any_instance.stubs(:index).returns('Response Body')
Rails.application.routes.draw do
root 'application#index'
end
end

teardown do
Rails.application.reload_routes!
end

test 'response includes default headers' do
get '/'
default_headers = Rails.application.config
.action_dispatch.default_headers
.keys.to_set
assert default_headers.subset?(response.header.keys.to_set)
end
end

0 comments on commit 57d085c

Please sign in to comment.