From 2e55095f6fc1ec2799bbdfd1143d487d2de1d7a3 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 17 Feb 2007 18:16:44 +0000 Subject: 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] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6158 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/test/controller/render_test.rb | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index bf9042ec7a..fc40d0ae7f 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -69,6 +69,10 @@ class TestController < ActionController::Base render "test/hello" end + def heading + head :ok + end + def greeting # let's just rely on the template end @@ -286,8 +290,50 @@ class RenderTest < Test::Unit::TestCase assert_equal "Goodbye, Local David", @response.body end + def test_render_200_should_set_etag + get :render_hello_world_from_variable + assert_equal etag_for("hello david"), @response.headers['Etag'] + end + + def test_render_against_etag_request_should_304_when_match + @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello david") + get :render_hello_world_from_variable + assert_equal "304 Not Modified", @response.headers['Status'] + assert @response.body.empty? + end + + def test_render_against_etag_request_should_200_when_no_match + @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello somewhere else") + get :render_hello_world_from_variable + assert_equal "200 OK", @response.headers['Status'] + assert !@response.body.empty? + end + + def test_render_with_etag + get :render_hello_world_from_variable + expected_etag = "\"#{MD5.new("hello david").to_s}\"" + assert_equal expected_etag, @response.headers['Etag'] + + @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag + get :render_hello_world_from_variable + assert_equal "304 Not Modified", @response.headers['Status'] + + @request.headers["HTTP_IF_NONE_MATCH"] = "\"diftag\"" + get :render_hello_world_from_variable + assert_equal "200 OK", @response.headers['Status'] + end + + def render_with_404_shouldnt_have_etag + get :render_custom_code + assert_nil @response.headers['Etag'] + end + protected def assert_deprecated_render(&block) assert_deprecated(/render/, &block) end + + def etag_for(text) + "\"#{MD5.new(text).to_s}\"" + end end -- cgit v1.2.3