Changeset 6158
- Timestamp:
- 02/17/07 18:16:44 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/base.rb (modified) (3 diffs)
- trunk/actionpack/lib/action_controller/request.rb (modified) (1 diff)
- trunk/actionpack/test/controller/render_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6157 r6158 1 1 *SVN* 2 3 * Added that rendering will automatically insert the etag header on 200 OK responses. The etag is calculated using MD5 of the response body. If a request comes in that has a matching etag, the response will be changed to a 304 Not Modified and the response body will be set to an empty string. [DHH] 2 4 3 5 * Added X-Runtime to all responses with the request run time [DHH] trunk/actionpack/lib/action_controller/base.rb
r6125 r6158 8 8 require 'drb' 9 9 require 'set' 10 require 'md5' 10 11 11 12 module ActionController #:nodoc: … … 649 650 # <tt>render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = {})</tt>. 650 651 # 652 # == Automatic etagging 653 # 654 # Rendering will automatically insert the etag header on 200 OK responses. The etag is calculated using MD5 of the 655 # response body. If a request comes in that has a matching etag, the response will be changed to a 304 Not Modified 656 # and the response body will be set to an empty string. 657 # 651 658 # === Rendering a template 652 659 # … … 871 878 response.body = text 872 879 end 880 881 if response.headers['Status'] == "200 OK" && response.body.size > 0 882 response.headers['Etag'] = "\"#{MD5.new(text).to_s}\"" 883 884 if request.headers['HTTP_IF_NONE_MATCH'] == response.headers['Etag'] 885 response.headers['Status'] = "304 Not Modified" 886 response.body = '' 887 end 888 end 889 890 response.body 873 891 end 874 892 trunk/actionpack/lib/action_controller/request.rb
r6155 r6158 50 50 def head? 51 51 @env['REQUEST_METHOD'].downcase.to_sym == :head 52 end 53 54 def headers 55 @env 52 56 end 53 57 trunk/actionpack/test/controller/render_test.rb
r6120 r6158 68 68 @name = "David" 69 69 render "test/hello" 70 end 71 72 def heading 73 head :ok 70 74 end 71 75 … … 287 291 end 288 292 293 def test_render_200_should_set_etag 294 get :render_hello_world_from_variable 295 assert_equal etag_for("hello david"), @response.headers['Etag'] 296 end 297 298 def test_render_against_etag_request_should_304_when_match 299 @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello david") 300 get :render_hello_world_from_variable 301 assert_equal "304 Not Modified", @response.headers['Status'] 302 assert @response.body.empty? 303 end 304 305 def test_render_against_etag_request_should_200_when_no_match 306 @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello somewhere else") 307 get :render_hello_world_from_variable 308 assert_equal "200 OK", @response.headers['Status'] 309 assert !@response.body.empty? 310 end 311 312 def test_render_with_etag 313 get :render_hello_world_from_variable 314 expected_etag = "\"#{MD5.new("hello david").to_s}\"" 315 assert_equal expected_etag, @response.headers['Etag'] 316 317 @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag 318 get :render_hello_world_from_variable 319 assert_equal "304 Not Modified", @response.headers['Status'] 320 321 @request.headers["HTTP_IF_NONE_MATCH"] = "\"diftag\"" 322 get :render_hello_world_from_variable 323 assert_equal "200 OK", @response.headers['Status'] 324 end 325 326 def render_with_404_shouldnt_have_etag 327 get :render_custom_code 328 assert_nil @response.headers['Etag'] 329 end 330 289 331 protected 290 332 def assert_deprecated_render(&block) 291 333 assert_deprecated(/render/, &block) 292 334 end 335 336 def etag_for(text) 337 "\"#{MD5.new(text).to_s}\"" 338 end 293 339 end