1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

stop applying default headers in ActionDispatch::Response

I'm making this change so that I can construct response objects that
*don't* have the default headers applied.  For example, I would like to
construct a response object from the return value of a controller.

If you need to construct a response object with the default headers,
then please use the alternate constructor:
`ActionDispatch::Response.create`
This commit is contained in:
Aaron Patterson 2015-09-23 14:39:45 -07:00
parent 28cb10b15e
commit e16afe61ab
7 changed files with 25 additions and 16 deletions

View file

@ -1,3 +1,8 @@
* ActionDispatch::Response#new no longer applies default headers. If you want
default headers applied to the response object, then call
`ActionDispatch::Response.create`. This change only impacts people who are
directly constructing an `ActionDispatch::Response` object.
* Accessing mime types via constants like `Mime::HTML` is deprecated. Please * Accessing mime types via constants like `Mime::HTML` is deprecated. Please
change code like this: change code like this:

View file

@ -135,7 +135,7 @@ module ActionController
end end
def self.make_response!(request) def self.make_response!(request)
ActionDispatch::Response.new.tap do |res| ActionDispatch::Response.create.tap do |res|
res.request = request res.request = request
end end
end end

View file

@ -236,6 +236,10 @@ module ActionController
end end
end end
def initialize(status = 200, header = {}, body = [])
super(status, Header.new(self, header), body)
end
private private
def before_committed def before_committed
@ -257,10 +261,6 @@ module ActionController
buf buf
end end
def merge_default_headers(original, default)
Header.new self, super
end
def handle_conditional_get! def handle_conditional_get!
super unless committed? super unless committed?
end end

View file

@ -585,7 +585,7 @@ module ActionController
end end
def build_response(klass) def build_response(klass)
klass.new klass.create
end end
included do included do

View file

@ -103,13 +103,21 @@ module ActionDispatch # :nodoc:
end end
end end
def self.create(status = 200, header = {}, body = [], default_headers: self.default_headers)
header = merge_default_headers(header, default_headers)
new status, header, body
end
def self.merge_default_headers(original, default)
default.respond_to?(:merge) ? default.merge(original) : original
end
# The underlying body, as a streamable object. # The underlying body, as a streamable object.
attr_reader :stream attr_reader :stream
def initialize(status = 200, header = {}, body = [], default_headers: self.class.default_headers) def initialize(status = 200, header = {}, body = [])
super() super()
header = merge_default_headers(header, default_headers)
@header = header @header = header
self.body, self.status = body, status self.body, self.status = body, status
@ -345,10 +353,6 @@ module ActionDispatch # :nodoc:
def before_sending def before_sending
end end
def merge_default_headers(original, default)
default.respond_to?(:merge) ? default.merge(original) : original
end
def build_buffer(response, body) def build_buffer(response, body)
Buffer.new response, body Buffer.new response, body
end end

View file

@ -7,7 +7,7 @@ module ActionDispatch
# See Response for more information on controller response objects. # See Response for more information on controller response objects.
class TestResponse < Response class TestResponse < Response
def self.from_response(response) def self.from_response(response)
new response.status, response.headers, response.body, default_headers: nil new response.status, response.headers, response.body
end end
# Was the response successful? # Was the response successful?

View file

@ -4,7 +4,7 @@ require 'rack/content_length'
class ResponseTest < ActiveSupport::TestCase class ResponseTest < ActiveSupport::TestCase
def setup def setup
@response = ActionDispatch::Response.new @response = ActionDispatch::Response.create
end end
def test_can_wait_until_commit def test_can_wait_until_commit
@ -217,7 +217,7 @@ class ResponseTest < ActiveSupport::TestCase
'X-Content-Type-Options' => 'nosniff', 'X-Content-Type-Options' => 'nosniff',
'X-XSS-Protection' => '1;' 'X-XSS-Protection' => '1;'
} }
resp = ActionDispatch::Response.new.tap { |response| resp = ActionDispatch::Response.create.tap { |response|
response.body = 'Hello' response.body = 'Hello'
} }
resp.to_a resp.to_a
@ -236,7 +236,7 @@ class ResponseTest < ActiveSupport::TestCase
ActionDispatch::Response.default_headers = { ActionDispatch::Response.default_headers = {
'X-XX-XXXX' => 'Here is my phone number' 'X-XX-XXXX' => 'Here is my phone number'
} }
resp = ActionDispatch::Response.new.tap { |response| resp = ActionDispatch::Response.create.tap { |response|
response.body = 'Hello' response.body = 'Hello'
} }
resp.to_a resp.to_a