aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/action_dispatch/http/cache.rb21
-rw-r--r--actionpack/lib/action_dispatch/http/response.rb36
-rw-r--r--actionpack/lib/action_dispatch/testing/integration.rb3
-rw-r--r--actionpack/test/controller/send_file_test.rb3
4 files changed, 42 insertions, 21 deletions
diff --git a/actionpack/lib/action_dispatch/http/cache.rb b/actionpack/lib/action_dispatch/http/cache.rb
index 428e62dc6b..d2404e63c5 100644
--- a/actionpack/lib/action_dispatch/http/cache.rb
+++ b/actionpack/lib/action_dispatch/http/cache.rb
@@ -37,8 +37,21 @@ module ActionDispatch
end
module Response
- def cache_control
- @cache_control ||= {}
+ attr_reader :cache_control
+
+ def initialize(*)
+ status, header, body = super
+
+ @cache_control = {}
+ @etag = self["ETag"]
+
+ if cache_control = self["Cache-Control"]
+ cache_control.split(/,\s*/).each do |segment|
+ first, last = segment.split("=")
+ last ||= true
+ @cache_control[first.to_sym] = last
+ end
+ end
end
def last_modified
@@ -65,7 +78,7 @@ module ActionDispatch
def etag=(etag)
key = ActiveSupport::Cache.expand_cache_key(etag)
- @etag = %("#{Digest::MD5.hexdigest(key)}")
+ @etag = self["ETag"] = %("#{Digest::MD5.hexdigest(key)}")
end
private
@@ -100,6 +113,8 @@ module ActionDispatch
def set_conditional_cache_control!
control = @cache_control
+ return if self["Cache-Control"].present?
+
if control.empty?
headers["Cache-Control"] = DEFAULT_CACHE_CONTROL
elsif @cache_control[:no_cache]
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb
index f299306ff4..1b8dd9abfd 100644
--- a/actionpack/lib/action_dispatch/http/response.rb
+++ b/actionpack/lib/action_dispatch/http/response.rb
@@ -32,31 +32,37 @@ module ActionDispatch # :nodoc:
# end
# end
class Response < Rack::Response
- include ActionDispatch::Http::Cache::Response
-
attr_accessor :request, :blank
attr_writer :header, :sending_file
alias_method :headers=, :header=
- def initialize
- @status = 200
- @header = {}
- @cache_control = {}
+ module Setup
+ def initialize(status = 200, header = {}, body = [])
+ @writer = lambda { |x| @body << x }
+ @block = nil
+ @length = 0
- @writer = lambda { |x| @body << x }
- @block = nil
- @length = 0
+ @status, @header, @body = status, header, body
- @body, @cookie = [], []
- @sending_file = false
+ @cookie = []
+ @sending_file = false
- @blank = false
- @etag = nil
+ @blank = false
+
+ if content_type = self["Content-Type"]
+ type, charset = content_type.split(/;\s*charset=/)
+ @content_type = Mime::Type.lookup(type)
+ @charset = charset || "UTF-8"
+ end
- yield self if block_given?
+ yield self if block_given?
+ end
end
+ include Setup
+ include ActionDispatch::Http::Cache::Response
+
def status=(status)
@status = Rack::Utils.status_code(status)
end
@@ -120,7 +126,7 @@ module ActionDispatch # :nodoc:
assign_default_content_type_and_charset!
handle_conditional_get!
self["Set-Cookie"] = @cookie.join("\n") unless @cookie.blank?
- self["ETag"] = @etag if @etag
+ self["ETag"] = @_etag if @_etag
super
end
diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb
index 2093bb3a0e..d179af47d9 100644
--- a/actionpack/lib/action_dispatch/testing/integration.rb
+++ b/actionpack/lib/action_dispatch/testing/integration.rb
@@ -273,7 +273,8 @@ module ActionDispatch
@request_count += 1
@request = ActionDispatch::Request.new(session.last_request.env)
- @response = ActionDispatch::TestResponse.from_response(@mock_session.last_response)
+ response = @mock_session.last_response
+ @response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
@html_document = nil
@controller = session.last_request.env['action_controller.instance']
diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb
index aebbd893a8..31177223e4 100644
--- a/actionpack/test/controller/send_file_test.rb
+++ b/actionpack/test/controller/send_file_test.rb
@@ -102,7 +102,7 @@ class SendFileTest < ActionController::TestCase
end
# Test that send_file_headers! is setting the correct HTTP headers.
- def test_send_file_headers!
+ def test_send_file_headers_bang
options = {
:length => 1,
:type => Mime::PNG,
@@ -125,7 +125,6 @@ class SendFileTest < ActionController::TestCase
assert_equal 'binary', h['Content-Transfer-Encoding']
# test overriding Cache-Control: no-cache header to fix IE open/save dialog
- @controller.headers = { 'Cache-Control' => 'no-cache' }
@controller.send(:send_file_headers!, options)
@controller.response.prepare!
assert_equal 'private', h['Cache-Control']