From b7529ed1cc7cfd8df5fd1b069e2881d39d3d984c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 7 Aug 2008 23:43:12 -0700 Subject: Simplifying usage of ETags and Last-Modified and conditional GET requests --- actionpack/test/controller/render_test.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 76832f5713..fd042794fa 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -15,9 +15,14 @@ class TestController < ActionController::Base end def conditional_hello - etag! [:foo, 123] - last_modified! Time.now.utc.beginning_of_day - render :action => 'hello_world' unless performed? + response.last_modified = Time.now.utc.beginning_of_day + response.etag = [:foo, 123] + + if request.fresh?(response) + head :not_modified + else + render :action => 'hello_world' + end end def render_hello_world -- cgit v1.2.3 From ba2d61dd8160237b2141ae24cf20db9b5301eb9d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 8 Aug 2008 02:31:12 -0700 Subject: Update tests for request memoization --- actionpack/test/controller/caching_test.rb | 2 +- actionpack/test/controller/cgi_test.rb | 2 +- actionpack/test/controller/content_type_test.rb | 8 +- actionpack/test/controller/mime_responds_test.rb | 62 ++++++++-------- actionpack/test/controller/rack_test.rb | 23 +++--- actionpack/test/controller/render_test.rb | 26 +++---- actionpack/test/controller/request_test.rb | 95 +++++++++++++----------- 7 files changed, 114 insertions(+), 104 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 47a0fcf99d..b6cdd116e5 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -109,7 +109,7 @@ class PageCachingTest < Test::Unit::TestCase uses_mocha("should_cache_ok_at_custom_path") do def test_should_cache_ok_at_custom_path - @request.expects(:path).returns("/index.html") + @request.stubs(:path).returns("/index.html") get :ok assert_response :ok assert File.exist?("#{FILE_STORE_PATH}/index.html") diff --git a/actionpack/test/controller/cgi_test.rb b/actionpack/test/controller/cgi_test.rb index 8ca70f8595..813171857a 100644 --- a/actionpack/test/controller/cgi_test.rb +++ b/actionpack/test/controller/cgi_test.rb @@ -75,7 +75,7 @@ class CgiRequestTest < BaseCgiTest assert_equal "rubyonrails.org:8080", @request.host_with_port @request_hash['HTTP_X_FORWARDED_HOST'] = "www.firsthost.org, www.secondhost.org" - assert_equal "www.secondhost.org", @request.host + assert_equal "www.secondhost.org", @request.host(true) end def test_http_host_with_default_port_overrides_server_port diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb index d457d13aef..e1bc46bb56 100644 --- a/actionpack/test/controller/content_type_test.rb +++ b/actionpack/test/controller/content_type_test.rb @@ -128,23 +128,23 @@ class AcceptBasedContentTypeTest < ActionController::TestCase def test_render_default_content_types_for_respond_to - @request.env["HTTP_ACCEPT"] = Mime::HTML.to_s + @request.accept = Mime::HTML.to_s get :render_default_content_types_for_respond_to assert_equal Mime::HTML, @response.content_type - @request.env["HTTP_ACCEPT"] = Mime::JS.to_s + @request.accept = Mime::JS.to_s get :render_default_content_types_for_respond_to assert_equal Mime::JS, @response.content_type end def test_render_default_content_types_for_respond_to_with_template - @request.env["HTTP_ACCEPT"] = Mime::XML.to_s + @request.accept = Mime::XML.to_s get :render_default_content_types_for_respond_to assert_equal Mime::XML, @response.content_type end def test_render_default_content_types_for_respond_to_with_overwrite - @request.env["HTTP_ACCEPT"] = Mime::RSS.to_s + @request.accept = Mime::RSS.to_s get :render_default_content_types_for_respond_to assert_equal Mime::XML, @response.content_type end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 1701431858..0d508eb8df 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -177,7 +177,7 @@ class MimeControllerTest < Test::Unit::TestCase end def test_html - @request.env["HTTP_ACCEPT"] = "text/html" + @request.accept = "text/html" get :js_or_html assert_equal 'HTML', @response.body @@ -189,7 +189,7 @@ class MimeControllerTest < Test::Unit::TestCase end def test_all - @request.env["HTTP_ACCEPT"] = "*/*" + @request.accept = "*/*" get :js_or_html assert_equal 'HTML', @response.body # js is not part of all @@ -201,13 +201,13 @@ class MimeControllerTest < Test::Unit::TestCase end def test_xml - @request.env["HTTP_ACCEPT"] = "application/xml" + @request.accept = "application/xml" get :html_xml_or_rss assert_equal 'XML', @response.body end def test_js_or_html - @request.env["HTTP_ACCEPT"] = "text/javascript, text/html" + @request.accept = "text/javascript, text/html" get :js_or_html assert_equal 'JS', @response.body @@ -232,7 +232,7 @@ class MimeControllerTest < Test::Unit::TestCase 'JSON' => %w(application/json text/x-json) }.each do |body, content_types| content_types.each do |content_type| - @request.env['HTTP_ACCEPT'] = content_type + @request.accept = content_type get :json_or_yaml assert_equal body, @response.body end @@ -240,7 +240,7 @@ class MimeControllerTest < Test::Unit::TestCase end def test_js_or_anything - @request.env["HTTP_ACCEPT"] = "text/javascript, */*" + @request.accept = "text/javascript, */*" get :js_or_html assert_equal 'JS', @response.body @@ -252,34 +252,34 @@ class MimeControllerTest < Test::Unit::TestCase end def test_using_defaults - @request.env["HTTP_ACCEPT"] = "*/*" + @request.accept = "*/*" get :using_defaults assert_equal "text/html", @response.content_type assert_equal 'Hello world!', @response.body - @request.env["HTTP_ACCEPT"] = "text/javascript" + @request.accept = "text/javascript" get :using_defaults assert_equal "text/javascript", @response.content_type assert_equal '$("body").visualEffect("highlight");', @response.body - @request.env["HTTP_ACCEPT"] = "application/xml" + @request.accept = "application/xml" get :using_defaults assert_equal "application/xml", @response.content_type assert_equal "

Hello world!

\n", @response.body end def test_using_defaults_with_type_list - @request.env["HTTP_ACCEPT"] = "*/*" + @request.accept = "*/*" get :using_defaults_with_type_list assert_equal "text/html", @response.content_type assert_equal 'Hello world!', @response.body - @request.env["HTTP_ACCEPT"] = "text/javascript" + @request.accept = "text/javascript" get :using_defaults_with_type_list assert_equal "text/javascript", @response.content_type assert_equal '$("body").visualEffect("highlight");', @response.body - @request.env["HTTP_ACCEPT"] = "application/xml" + @request.accept = "application/xml" get :using_defaults_with_type_list assert_equal "application/xml", @response.content_type assert_equal "

Hello world!

\n", @response.body @@ -298,55 +298,55 @@ class MimeControllerTest < Test::Unit::TestCase end def test_synonyms - @request.env["HTTP_ACCEPT"] = "application/javascript" + @request.accept = "application/javascript" get :js_or_html assert_equal 'JS', @response.body - @request.env["HTTP_ACCEPT"] = "application/x-xml" + @request.accept = "application/x-xml" get :html_xml_or_rss assert_equal "XML", @response.body end def test_custom_types - @request.env["HTTP_ACCEPT"] = "application/crazy-xml" + @request.accept = "application/crazy-xml" get :custom_type_handling assert_equal "application/crazy-xml", @response.content_type assert_equal 'Crazy XML', @response.body - @request.env["HTTP_ACCEPT"] = "text/html" + @request.accept = "text/html" get :custom_type_handling assert_equal "text/html", @response.content_type assert_equal 'HTML', @response.body end def test_xhtml_alias - @request.env["HTTP_ACCEPT"] = "application/xhtml+xml,application/xml" + @request.accept = "application/xhtml+xml,application/xml" get :html_or_xml assert_equal 'HTML', @response.body end def test_firefox_simulation - @request.env["HTTP_ACCEPT"] = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + @request.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" get :html_or_xml assert_equal 'HTML', @response.body end def test_handle_any - @request.env["HTTP_ACCEPT"] = "*/*" + @request.accept = "*/*" get :handle_any assert_equal 'HTML', @response.body - @request.env["HTTP_ACCEPT"] = "text/javascript" + @request.accept = "text/javascript" get :handle_any assert_equal 'Either JS or XML', @response.body - @request.env["HTTP_ACCEPT"] = "text/xml" + @request.accept = "text/xml" get :handle_any assert_equal 'Either JS or XML', @response.body end def test_handle_any_any - @request.env["HTTP_ACCEPT"] = "*/*" + @request.accept = "*/*" get :handle_any_any assert_equal 'HTML', @response.body end @@ -357,31 +357,31 @@ class MimeControllerTest < Test::Unit::TestCase end def test_handle_any_any_explicit_html - @request.env["HTTP_ACCEPT"] = "text/html" + @request.accept = "text/html" get :handle_any_any assert_equal 'HTML', @response.body end def test_handle_any_any_javascript - @request.env["HTTP_ACCEPT"] = "text/javascript" + @request.accept = "text/javascript" get :handle_any_any assert_equal 'Whatever you ask for, I got it', @response.body end def test_handle_any_any_xml - @request.env["HTTP_ACCEPT"] = "text/xml" + @request.accept = "text/xml" get :handle_any_any assert_equal 'Whatever you ask for, I got it', @response.body end def test_rjs_type_skips_layout - @request.env["HTTP_ACCEPT"] = "text/javascript" + @request.accept = "text/javascript" get :all_types_with_layout assert_equal 'RJS for all_types_with_layout', @response.body end def test_html_type_with_layout - @request.env["HTTP_ACCEPT"] = "text/html" + @request.accept = "text/html" get :all_types_with_layout assert_equal '
HTML for all_types_with_layout
', @response.body end @@ -460,7 +460,7 @@ class MimeControllerTest < Test::Unit::TestCase end def test_format_with_custom_response_type_and_request_headers - @request.env["HTTP_ACCEPT"] = "text/iphone" + @request.accept = "text/iphone" get :iphone_with_html_response_type assert_equal '
Hello iPhone future from iPhone!
', @response.body assert_equal "text/html", @response.content_type @@ -470,7 +470,7 @@ class MimeControllerTest < Test::Unit::TestCase get :iphone_with_html_response_type_without_layout assert_equal '
Hello future from Firefox!
', @response.body - @request.env["HTTP_ACCEPT"] = "text/iphone" + @request.accept = "text/iphone" assert_raises(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout } end end @@ -522,7 +522,7 @@ class MimeControllerLayoutsTest < Test::Unit::TestCase get :index assert_equal '
Hello Firefox
', @response.body - @request.env["HTTP_ACCEPT"] = "text/iphone" + @request.accept = "text/iphone" get :index assert_equal 'Hello iPhone', @response.body end @@ -533,7 +533,7 @@ class MimeControllerLayoutsTest < Test::Unit::TestCase get :index assert_equal 'Super Firefox', @response.body - @request.env["HTTP_ACCEPT"] = "text/iphone" + @request.accept = "text/iphone" get :index assert_equal '
Super iPhone
', @response.body end diff --git a/actionpack/test/controller/rack_test.rb b/actionpack/test/controller/rack_test.rb index ab8bbc3bf9..d1650de1fc 100644 --- a/actionpack/test/controller/rack_test.rb +++ b/actionpack/test/controller/rack_test.rb @@ -64,58 +64,61 @@ end class RackRequestTest < BaseRackTest def test_proxy_request - assert_equal 'glu.ttono.us', @request.host_with_port + assert_equal 'glu.ttono.us', @request.host_with_port(true) end def test_http_host @env.delete "HTTP_X_FORWARDED_HOST" @env['HTTP_HOST'] = "rubyonrails.org:8080" - assert_equal "rubyonrails.org:8080", @request.host_with_port + assert_equal "rubyonrails.org", @request.host(true) + assert_equal "rubyonrails.org:8080", @request.host_with_port(true) @env['HTTP_X_FORWARDED_HOST'] = "www.firsthost.org, www.secondhost.org" - assert_equal "www.secondhost.org", @request.host + assert_equal "www.secondhost.org", @request.host(true) end def test_http_host_with_default_port_overrides_server_port @env.delete "HTTP_X_FORWARDED_HOST" @env['HTTP_HOST'] = "rubyonrails.org" - assert_equal "rubyonrails.org", @request.host_with_port + assert_equal "rubyonrails.org", @request.host_with_port(true) end def test_host_with_port_defaults_to_server_name_if_no_host_headers @env.delete "HTTP_X_FORWARDED_HOST" @env.delete "HTTP_HOST" - assert_equal "glu.ttono.us:8007", @request.host_with_port + assert_equal "glu.ttono.us:8007", @request.host_with_port(true) end def test_host_with_port_falls_back_to_server_addr_if_necessary @env.delete "HTTP_X_FORWARDED_HOST" @env.delete "HTTP_HOST" @env.delete "SERVER_NAME" - assert_equal "207.7.108.53:8007", @request.host_with_port + assert_equal "207.7.108.53", @request.host(true) + assert_equal 8007, @request.port(true) + assert_equal "207.7.108.53:8007", @request.host_with_port(true) end def test_host_with_port_if_http_standard_port_is_specified @env['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:80" - assert_equal "glu.ttono.us", @request.host_with_port + assert_equal "glu.ttono.us", @request.host_with_port(true) end def test_host_with_port_if_https_standard_port_is_specified @env['HTTP_X_FORWARDED_PROTO'] = "https" @env['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:443" - assert_equal "glu.ttono.us", @request.host_with_port + assert_equal "glu.ttono.us", @request.host_with_port(true) end def test_host_if_ipv6_reference @env.delete "HTTP_X_FORWARDED_HOST" @env['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]" - assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host + assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host(true) end def test_host_if_ipv6_reference_with_port @env.delete "HTTP_X_FORWARDED_HOST" @env['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]:8008" - assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host + assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host(true) end def test_cgi_environment_variables diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index fd042794fa..1b9b12acc6 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -433,7 +433,7 @@ class RenderTest < Test::Unit::TestCase end def test_should_render_formatted_html_erb_template_with_faulty_accepts_header - @request.env["HTTP_ACCEPT"] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*" + @request.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*" get :formatted_xml_erb assert_equal 'passed formatted html erb', @response.body end @@ -495,16 +495,16 @@ class EtagRenderTest < Test::Unit::TestCase end def test_render_against_etag_request_should_304_when_match - @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello david") + @request.if_none_match = etag_for("hello david") get :render_hello_world_from_variable - assert_equal "304 Not Modified", @response.headers['Status'] + assert_equal "304 Not Modified", @response.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") + @request.if_none_match = etag_for("hello somewhere else") get :render_hello_world_from_variable - assert_equal "200 OK", @response.headers['Status'] + assert_equal "200 OK", @response.status assert !@response.body.empty? end @@ -513,13 +513,13 @@ class EtagRenderTest < Test::Unit::TestCase expected_etag = etag_for('hello david') assert_equal expected_etag, @response.headers['ETag'] - @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag + @request.if_none_match = expected_etag get :render_hello_world_from_variable - assert_equal "304 Not Modified", @response.headers['Status'] + assert_equal "304 Not Modified", @response.status - @request.headers["HTTP_IF_NONE_MATCH"] = "\"diftag\"" + @request.if_none_match = "\"diftag\"" get :render_hello_world_from_variable - assert_equal "200 OK", @response.headers['Status'] + assert_equal "200 OK", @response.status end def render_with_404_shouldnt_have_etag @@ -562,17 +562,17 @@ class LastModifiedRenderTest < Test::Unit::TestCase end def test_request_not_modified - @request.headers["HTTP_IF_MODIFIED_SINCE"] = @last_modified + @request.if_modified_since = @last_modified get :conditional_hello - assert_equal "304 Not Modified", @response.headers['Status'] + assert_equal "304 Not Modified", @response.status assert @response.body.blank?, @response.body assert_equal @last_modified, @response.headers['Last-Modified'] end def test_request_modified - @request.headers["HTTP_IF_MODIFIED_SINCE"] = 'Thu, 16 Jul 2008 00:00:00 GMT' + @request.if_modified_since = 'Thu, 16 Jul 2008 00:00:00 GMT' get :conditional_hello - assert_equal "200 OK", @response.headers['Status'] + assert_equal "200 OK", @response.status assert !@response.body.blank? assert_equal @last_modified, @response.headers['Last-Modified'] end diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb index 7db5264840..226c1ac018 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -15,57 +15,57 @@ class RequestTest < Test::Unit::TestCase assert_equal '0.0.0.0', @request.remote_ip @request.remote_addr = '1.2.3.4' - assert_equal '1.2.3.4', @request.remote_ip + assert_equal '1.2.3.4', @request.remote_ip(true) @request.env['HTTP_CLIENT_IP'] = '2.3.4.5' - assert_equal '1.2.3.4', @request.remote_ip + assert_equal '1.2.3.4', @request.remote_ip(true) @request.remote_addr = '192.168.0.1' - assert_equal '2.3.4.5', @request.remote_ip + assert_equal '2.3.4.5', @request.remote_ip(true) @request.env.delete 'HTTP_CLIENT_IP' @request.remote_addr = '1.2.3.4' @request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6' - assert_equal '1.2.3.4', @request.remote_ip + assert_equal '1.2.3.4', @request.remote_ip(true) @request.remote_addr = '127.0.0.1' @request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6' - assert_equal '3.4.5.6', @request.remote_ip + assert_equal '3.4.5.6', @request.remote_ip(true) @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,3.4.5.6' - assert_equal '3.4.5.6', @request.remote_ip + assert_equal '3.4.5.6', @request.remote_ip(true) @request.env['HTTP_X_FORWARDED_FOR'] = '172.16.0.1,3.4.5.6' - assert_equal '3.4.5.6', @request.remote_ip + assert_equal '3.4.5.6', @request.remote_ip(true) @request.env['HTTP_X_FORWARDED_FOR'] = '192.168.0.1,3.4.5.6' - assert_equal '3.4.5.6', @request.remote_ip + assert_equal '3.4.5.6', @request.remote_ip(true) @request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1,3.4.5.6' - assert_equal '3.4.5.6', @request.remote_ip + assert_equal '3.4.5.6', @request.remote_ip(true) @request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1, 10.0.0.1, 3.4.5.6' - assert_equal '3.4.5.6', @request.remote_ip + assert_equal '3.4.5.6', @request.remote_ip(true) @request.env['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,3.4.5.6' - assert_equal '3.4.5.6', @request.remote_ip + assert_equal '3.4.5.6', @request.remote_ip(true) @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,192.168.0.1' - assert_equal 'unknown', @request.remote_ip + assert_equal 'unknown', @request.remote_ip(true) @request.env['HTTP_X_FORWARDED_FOR'] = '9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4' - assert_equal '3.4.5.6', @request.remote_ip + assert_equal '3.4.5.6', @request.remote_ip(true) @request.env['HTTP_CLIENT_IP'] = '8.8.8.8' e = assert_raises(ActionController::ActionControllerError) { - @request.remote_ip + @request.remote_ip(true) } assert_match /IP spoofing attack/, e.message assert_match /HTTP_X_FORWARDED_FOR="9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4"/, e.message assert_match /HTTP_CLIENT_IP="8.8.8.8"/, e.message @request.env['HTTP_X_FORWARDED_FOR'] = '8.8.8.8, 9.9.9.9' - assert_equal '8.8.8.8', @request.remote_ip + assert_equal '8.8.8.8', @request.remote_ip(true) @request.env.delete 'HTTP_CLIENT_IP' @request.env.delete 'HTTP_X_FORWARDED_FOR' @@ -168,58 +168,58 @@ class RequestTest < Test::Unit::TestCase ActionController::Base.relative_url_root = nil # The following tests are for when REQUEST_URI is not supplied (as in IIS) - @request.set_REQUEST_URI nil @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1" @request.env['SCRIPT_NAME'] = nil #"/path/dispatch.rb" + @request.set_REQUEST_URI nil assert_equal "/path/of/some/uri?mapped=1", @request.request_uri assert_equal "/path/of/some/uri", @request.path ActionController::Base.relative_url_root = '/path' - @request.set_REQUEST_URI nil @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1" @request.env['SCRIPT_NAME'] = "/path/dispatch.rb" - assert_equal "/path/of/some/uri?mapped=1", @request.request_uri - assert_equal "/of/some/uri", @request.path + @request.set_REQUEST_URI nil + assert_equal "/path/of/some/uri?mapped=1", @request.request_uri(true) + assert_equal "/of/some/uri", @request.path(true) ActionController::Base.relative_url_root = nil - @request.set_REQUEST_URI nil @request.env['PATH_INFO'] = "/path/of/some/uri" @request.env['SCRIPT_NAME'] = nil + @request.set_REQUEST_URI nil assert_equal "/path/of/some/uri", @request.request_uri assert_equal "/path/of/some/uri", @request.path - @request.set_REQUEST_URI nil @request.env['PATH_INFO'] = "/" + @request.set_REQUEST_URI nil assert_equal "/", @request.request_uri assert_equal "/", @request.path - @request.set_REQUEST_URI nil @request.env['PATH_INFO'] = "/?m=b" + @request.set_REQUEST_URI nil assert_equal "/?m=b", @request.request_uri assert_equal "/", @request.path - @request.set_REQUEST_URI nil @request.env['PATH_INFO'] = "/" @request.env['SCRIPT_NAME'] = "/dispatch.cgi" + @request.set_REQUEST_URI nil assert_equal "/", @request.request_uri assert_equal "/", @request.path ActionController::Base.relative_url_root = '/hieraki' - @request.set_REQUEST_URI nil @request.env['PATH_INFO'] = "/hieraki/" @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi" + @request.set_REQUEST_URI nil assert_equal "/hieraki/", @request.request_uri assert_equal "/", @request.path ActionController::Base.relative_url_root = nil @request.set_REQUEST_URI '/hieraki/dispatch.cgi' ActionController::Base.relative_url_root = '/hieraki' - assert_equal "/dispatch.cgi", @request.path + assert_equal "/dispatch.cgi", @request.path(true) ActionController::Base.relative_url_root = nil @request.set_REQUEST_URI '/hieraki/dispatch.cgi' ActionController::Base.relative_url_root = '/foo' - assert_equal "/hieraki/dispatch.cgi", @request.path + assert_equal "/hieraki/dispatch.cgi", @request.path(true) ActionController::Base.relative_url_root = nil # This test ensures that Rails uses REQUEST_URI over PATH_INFO @@ -227,8 +227,8 @@ class RequestTest < Test::Unit::TestCase @request.env['REQUEST_URI'] = "/some/path" @request.env['PATH_INFO'] = "/another/path" @request.env['SCRIPT_NAME'] = "/dispatch.cgi" - assert_equal "/some/path", @request.request_uri - assert_equal "/some/path", @request.path + assert_equal "/some/path", @request.request_uri(true) + assert_equal "/some/path", @request.path(true) end def test_host_with_default_port @@ -244,13 +244,13 @@ class RequestTest < Test::Unit::TestCase end def test_server_software - assert_equal nil, @request.server_software + assert_equal nil, @request.server_software(true) @request.env['SERVER_SOFTWARE'] = 'Apache3.422' - assert_equal 'apache', @request.server_software + assert_equal 'apache', @request.server_software(true) @request.env['SERVER_SOFTWARE'] = 'lighttpd(1.1.4)' - assert_equal 'lighttpd', @request.server_software + assert_equal 'lighttpd', @request.server_software(true) end def test_xml_http_request @@ -280,44 +280,44 @@ class RequestTest < Test::Unit::TestCase def test_symbolized_request_methods [:get, :post, :put, :delete].each do |method| - set_request_method_to method + self.request_method = method assert_equal method, @request.method end end def test_invalid_http_method_raises_exception - set_request_method_to :random_method assert_raises(ActionController::UnknownHttpMethod) do - @request.method + self.request_method = :random_method end end def test_allow_method_hacking_on_post - set_request_method_to :post + self.request_method = :post [:get, :head, :options, :put, :post, :delete].each do |method| - @request.instance_eval { @parameters = { :_method => method } ; @request_method = nil } + @request.instance_eval { @parameters = { :_method => method.to_s } ; @request_method = nil } + @request.request_method(true) assert_equal(method == :head ? :get : method, @request.method) end end def test_invalid_method_hacking_on_post_raises_exception - set_request_method_to :post + self.request_method = :post @request.instance_eval { @parameters = { :_method => :random_method } ; @request_method = nil } assert_raises(ActionController::UnknownHttpMethod) do - @request.method + @request.request_method(true) end end def test_restrict_method_hacking @request.instance_eval { @parameters = { :_method => 'put' } } [:get, :put, :delete].each do |method| - set_request_method_to method + self.request_method = method assert_equal method, @request.method end end - def test_head_masquarading_as_get - set_request_method_to :head + def test_head_masquerading_as_get + self.request_method = :head assert_equal :get, @request.method assert @request.get? assert @request.head? @@ -339,9 +339,16 @@ class RequestTest < Test::Unit::TestCase end def test_nil_format - @request.instance_eval { @parameters = { :format => nil } } + ActionController::Base.use_accept_header, old = + false, ActionController::Base.use_accept_header + + @request.instance_eval { @parameters = {} } @request.env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" + assert @request.xhr? assert_equal Mime::JS, @request.format + + ensure + ActionController::Base.use_accept_header = old end def test_content_type @@ -384,9 +391,9 @@ class RequestTest < Test::Unit::TestCase end protected - def set_request_method_to(method) + def request_method=(method) @request.env['REQUEST_METHOD'] = method.to_s.upcase - @request.instance_eval { @request_method = nil } + @request.request_method(true) end end -- cgit v1.2.3 From 1b127fcdea28d6c6ab2f2c6370125c8817ba99d6 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 12 Aug 2008 20:18:03 -0700 Subject: Set asset-cached file ctime and mtime to the max mtime of the combined files. Allows for consistent ETag generation without having a shared filesystem. --- actionpack/test/template/asset_tag_helper_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 8410e82c3c..7e40a55dc5 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -425,7 +425,8 @@ class AssetTagHelperTest < ActionView::TestCase stylesheet_link_tag(:all, :cache => true) ) - assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css')) + expected = Dir["#{ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR}/*.css"].map { |p| File.mtime(p) }.max + assert_equal expected, File.mtime(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css')) assert_dom_equal( %(), -- cgit v1.2.3 From 3b9324e62f770f1a0a457f7ad5fe6a3287ecae1f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 13 Aug 2008 19:15:35 -0500 Subject: Fix rendering partials at the top level [#795 state:resolved] --- actionpack/test/fixtures/_top_level_partial.html.erb | 1 + actionpack/test/fixtures/_top_level_partial_only.erb | 1 + actionpack/test/template/render_test.rb | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 actionpack/test/fixtures/_top_level_partial.html.erb create mode 100644 actionpack/test/fixtures/_top_level_partial_only.erb (limited to 'actionpack/test') diff --git a/actionpack/test/fixtures/_top_level_partial.html.erb b/actionpack/test/fixtures/_top_level_partial.html.erb new file mode 100644 index 0000000000..0b1c2e46e0 --- /dev/null +++ b/actionpack/test/fixtures/_top_level_partial.html.erb @@ -0,0 +1 @@ +top level partial html \ No newline at end of file diff --git a/actionpack/test/fixtures/_top_level_partial_only.erb b/actionpack/test/fixtures/_top_level_partial_only.erb new file mode 100644 index 0000000000..44f25b61d0 --- /dev/null +++ b/actionpack/test/fixtures/_top_level_partial_only.erb @@ -0,0 +1 @@ +top level partial \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index c37bac95f1..31cfdce531 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -19,6 +19,10 @@ class ViewRenderTest < Test::Unit::TestCase assert_equal "Hello world!", @view.render("test/hello_world") end + def test_render_file_at_top_level + assert_equal 'Elastica', @view.render('/shared') + end + def test_render_file_with_full_path template_path = File.join(File.dirname(__FILE__), '../fixtures/test/hello_world.erb') assert_equal "Hello world!", @view.render(:file => template_path) @@ -47,6 +51,20 @@ class ViewRenderTest < Test::Unit::TestCase assert_equal "only partial", @view.render(:partial => "test/partial_only") end + def test_render_partial_with_format + assert_equal 'partial html', @view.render(:partial => 'test/partial') + end + + def test_render_partial_at_top_level + # file fixtures/_top_level_partial_only.erb (not fixtures/test) + assert_equal 'top level partial', @view.render(:partial => '/top_level_partial_only') + end + + def test_render_partial_with_format_at_top_level + # file fixtures/_top_level_partial.html.erb (not fixtures/test, with format extension) + assert_equal 'top level partial html', @view.render(:partial => '/top_level_partial') + end + def test_render_partial_with_locals assert_equal "5", @view.render(:partial => "test/counter", :locals => { :counter_counter => 5 }) end -- cgit v1.2.3 From 894f9ccc5354c29e102f316f388d1dc0b98ef080 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 17 Aug 2008 19:04:01 -0500 Subject: Use RackRequest as a mock instead of StubCGI into RequestTest --- actionpack/test/controller/request_test.rb | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb index 226c1ac018..045dab4141 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -397,7 +397,6 @@ class RequestTest < Test::Unit::TestCase end end - class UrlEncodedRequestParameterParsingTest < Test::Unit::TestCase def setup @query_string = "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1" @@ -509,7 +508,6 @@ class UrlEncodedRequestParameterParsingTest < Test::Unit::TestCase ) end - def test_request_hash_parsing query = { "note[viewers][viewer][][type]" => ["User", "Group"], @@ -521,7 +519,6 @@ class UrlEncodedRequestParameterParsingTest < Test::Unit::TestCase assert_equal(expected, ActionController::AbstractRequest.parse_request_parameters(query)) end - def test_parse_params input = { "customers[boston][first][name]" => [ "David" ], @@ -704,7 +701,6 @@ class UrlEncodedRequestParameterParsingTest < Test::Unit::TestCase end end - class MultipartRequestParameterParsingTest < Test::Unit::TestCase FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart' @@ -852,20 +848,20 @@ class XmlParamsParsingTest < Test::Unit::TestCase private def parse_body(body) - env = { 'CONTENT_TYPE' => 'application/xml', + env = { 'rack.input' => StringIO.new(body), + 'CONTENT_TYPE' => 'application/xml', 'CONTENT_LENGTH' => body.size.to_s } - cgi = ActionController::Integration::Session::StubCGI.new(env, body) - ActionController::CgiRequest.new(cgi).request_parameters + ActionController::RackRequest.new(env).request_parameters end end class LegacyXmlParamsParsingTest < XmlParamsParsingTest private def parse_body(body) - env = { 'HTTP_X_POST_DATA_FORMAT' => 'xml', - 'CONTENT_LENGTH' => body.size.to_s } - cgi = ActionController::Integration::Session::StubCGI.new(env, body) - ActionController::CgiRequest.new(cgi).request_parameters + env = { 'rack.input' => StringIO.new(body), + 'HTTP_X_POST_DATA_FORMAT' => 'xml', + 'CONTENT_LENGTH' => body.size.to_s } + ActionController::RackRequest.new(env).request_parameters end end @@ -884,9 +880,9 @@ class JsonParamsParsingTest < Test::Unit::TestCase private def parse_body(body,content_type) - env = { 'CONTENT_TYPE' => content_type, + env = { 'rack.input' => StringIO.new(body), + 'CONTENT_TYPE' => content_type, 'CONTENT_LENGTH' => body.size.to_s } - cgi = ActionController::Integration::Session::StubCGI.new(env, body) - ActionController::CgiRequest.new(cgi).request_parameters + ActionController::RackRequest.new(env).request_parameters end end -- cgit v1.2.3 From b8e930aa016e26471046d3f7d7ca1c10103791e7 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 17 Aug 2008 19:09:38 -0500 Subject: Merge RackProcess#normalize_headers logic into AbstractResponse#prepare! --- actionpack/test/controller/rack_test.rb | 35 ++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/rack_test.rb b/actionpack/test/controller/rack_test.rb index d1650de1fc..30a1144aad 100644 --- a/actionpack/test/controller/rack_test.rb +++ b/actionpack/test/controller/rack_test.rb @@ -236,10 +236,17 @@ class RackResponseTest < BaseRackTest def test_simple_output @response.body = "Hello, World!" + @response.prepare! status, headers, body = @response.out(@output) assert_equal "200 OK", status - assert_equal({"Content-Type" => "text/html", "Cache-Control" => "no-cache", "Set-Cookie" => []}, headers) + assert_equal({ + "Content-Type" => "text/html", + "Cache-Control" => "private, max-age=0, must-revalidate", + "ETag" => '"65a8e27d8879283831b664bd8b7f0ad4"', + "Set-Cookie" => [], + "Content-Length" => "13" + }, headers) parts = [] body.each { |part| parts << part } @@ -250,6 +257,7 @@ class RackResponseTest < BaseRackTest @response.body = Proc.new do |response, output| 5.times { |n| output.write(n) } end + @response.prepare! status, headers, body = @response.out(@output) assert_equal "200 OK", status @@ -265,13 +273,16 @@ class RackResponseTest < BaseRackTest @request.cgi.send :instance_variable_set, '@output_cookies', [cookie] @response.body = "Hello, World!" + @response.prepare! status, headers, body = @response.out(@output) assert_equal "200 OK", status assert_equal({ "Content-Type" => "text/html", - "Cache-Control" => "no-cache", - "Set-Cookie" => ["name=Josh; path="] + "Cache-Control" => "private, max-age=0, must-revalidate", + "ETag" => '"65a8e27d8879283831b664bd8b7f0ad4"', + "Set-Cookie" => ["name=Josh; path="], + "Content-Length" => "13" }, headers) parts = [] @@ -285,18 +296,18 @@ class RackResponseHeadersTest < BaseRackTest super @response = ActionController::RackResponse.new(@request) @output = StringIO.new('') - @response.headers['Status'] = 200 + @response.headers['Status'] = "200 OK" end def test_content_type [204, 304].each do |c| - @response.headers['Status'] = c - assert !response_headers.has_key?("Content-Type") + @response.headers['Status'] = c.to_s + assert !response_headers.has_key?("Content-Type"), "#{c} should not have Content-Type header" end [200, 302, 404, 500].each do |c| - @response.headers['Status'] = c - assert response_headers.has_key?("Content-Type") + @response.headers['Status'] = c.to_s + assert response_headers.has_key?("Content-Type"), "#{c} did not have Content-Type header" end end @@ -305,8 +316,8 @@ class RackResponseHeadersTest < BaseRackTest end private - - def response_headers - @response.out(@output)[1] - end + def response_headers + @response.prepare! + @response.out(@output)[1] + end end -- cgit v1.2.3 From dbb0abfb7e9eb9a63b721a38625e3eff66ced49d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 17 Aug 2008 19:18:18 -0500 Subject: More integration test coverage --- actionpack/test/controller/integration_test.rb | 32 +++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 475e13897b..a709343a94 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -265,6 +265,10 @@ class IntegrationProcessTest < ActionController::IntegrationTest cookies["cookie_3"] = "chocolate" render :text => "Gone", :status => 410 end + + def redirect + redirect_to :action => "get" + end end def test_get @@ -274,6 +278,9 @@ class IntegrationProcessTest < ActionController::IntegrationTest assert_equal "OK", status_message assert_equal "200 OK", response.headers["Status"] assert_equal ["200 OK"], headers["status"] + assert_response 200 + assert_response :success + assert_response :ok assert_equal [], response.headers["cookie"] assert_equal [], headers["cookie"] assert_equal({}, cookies) @@ -290,6 +297,9 @@ class IntegrationProcessTest < ActionController::IntegrationTest assert_equal "Created", status_message assert_equal "201 Created", response.headers["Status"] assert_equal ["201 Created"], headers["status"] + assert_response 201 + assert_response :success + assert_response :created assert_equal [], response.headers["cookie"] assert_equal [], headers["cookie"] assert_equal({}, cookies) @@ -308,6 +318,8 @@ class IntegrationProcessTest < ActionController::IntegrationTest assert_equal "Gone", status_message assert_equal "410 Gone", response.headers["Status"] assert_equal ["410 Gone"], headers["status"] + assert_response 410 + assert_response :gone assert_equal nil, response.headers["Set-Cookie"] assert_equal ["cookie_1=; path=/", "cookie_3=chocolate; path=/"], headers['set-cookie'] assert_equal [[], ["chocolate"]], response.headers["cookie"] @@ -317,14 +329,28 @@ class IntegrationProcessTest < ActionController::IntegrationTest end end + def test_redirect + with_test_route_set do + get '/redirect' + assert_equal 302, status + assert_equal "Found", status_message + assert_equal "302 Found", response.headers["Status"] + assert_equal ["302 Found"], headers["status"] + assert_response 302 + assert_response :redirect + assert_response :found + assert_equal "You are being redirected.", response.body + assert_kind_of HTML::Document, html_document + assert_equal 1, request_count + end + end + private def with_test_route_set with_routing do |set| set.draw do |map| map.with_options :controller => "IntegrationProcessTest::Integration" do |c| - c.connect '/get', :action => "get" - c.connect '/post', :action => "post" - c.connect '/cookie_monster', :action => "cookie_monster" + c.connect "/:action" end end yield -- cgit v1.2.3 From 38c7d73e73d569211c4dfadf96fc295a925b7c9c Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Sun, 17 Aug 2008 19:29:24 -0500 Subject: pass yielded arguments to block for ActionView::Base#render with :layout [#847 state:resolved] Signed-off-by: Joshua Peek --- actionpack/test/controller/layout_test.rb | 19 +++++++++---------- actionpack/test/controller/new_render_test.rb | 9 +++++++++ .../test/_layout_for_block_with_args.html.erb | 3 +++ .../test/using_layout_around_block_with_args.html.erb | 1 + 4 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 actionpack/test/fixtures/test/_layout_for_block_with_args.html.erb create mode 100644 actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index 72c01a9102..71f110f241 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -41,19 +41,19 @@ class LayoutAutoDiscoveryTest < Test::Unit::TestCase @request.host = "www.nextangle.com" end - + def test_application_layout_is_default_when_no_controller_match @controller = ProductController.new get :hello assert_equal 'layout_test.rhtml hello.rhtml', @response.body end - + def test_controller_name_layout_name_match @controller = ItemController.new get :hello assert_equal 'item.rhtml hello.rhtml', @response.body end - + def test_third_party_template_library_auto_discovers_layout ThirdPartyTemplateLibraryController.view_paths.reload! @controller = ThirdPartyTemplateLibraryController.new @@ -63,14 +63,14 @@ class LayoutAutoDiscoveryTest < Test::Unit::TestCase assert_response :success assert_equal 'Mab', @response.body end - + def test_namespaced_controllers_auto_detect_layouts @controller = ControllerNameSpace::NestedController.new get :hello assert_equal 'layouts/controller_name_space/nested', @controller.active_layout assert_equal 'controller_name_space/nested.rhtml hello.rhtml', @response.body end - + def test_namespaced_controllers_auto_detect_layouts @controller = MultipleExtensions.new get :hello @@ -115,7 +115,7 @@ class ExemptFromLayoutTest < Test::Unit::TestCase def test_rhtml_exempt_from_layout_status_should_prevent_layout_render ActionController::Base.exempt_from_layout :rhtml - + assert @controller.send!(:template_exempt_from_layout?, 'test.rhtml') assert @controller.send!(:template_exempt_from_layout?, 'hello.rhtml') @@ -156,19 +156,19 @@ class LayoutSetInResponseTest < Test::Unit::TestCase get :hello assert_equal 'layouts/layout_test', @response.layout end - + def test_layout_set_when_set_in_controller @controller = HasOwnLayoutController.new get :hello assert_equal 'layouts/item', @response.layout end - + def test_layout_set_when_using_render @controller = SetsLayoutInRenderController.new get :hello assert_equal 'layouts/third_party_template_library', @response.layout end - + def test_layout_is_not_set_when_none_rendered @controller = RendersNoLayoutController.new get :hello @@ -249,4 +249,3 @@ class LayoutSymlinkedIsRenderedTest < Test::Unit::TestCase assert_equal "layouts/symlinked/symlinked_layout", @response.layout end end - \ No newline at end of file diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index be99350cd2..82919b7777 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -435,6 +435,10 @@ class NewRenderTestController < ActionController::Base render :action => "using_layout_around_block" end + def render_using_layout_around_block_with_args + render :action => "using_layout_around_block_with_args" + end + def render_using_layout_around_block_in_main_layout_and_within_content_for_layout render :action => "using_layout_around_block" end @@ -969,4 +973,9 @@ EOS get :render_using_layout_around_block_in_main_layout_and_within_content_for_layout assert_equal "Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n", @response.body end + + def test_using_layout_around_block_with_args + get :render_using_layout_around_block_with_args + assert_equal "Before\narg1arg2\nAfter", @response.body + end end diff --git a/actionpack/test/fixtures/test/_layout_for_block_with_args.html.erb b/actionpack/test/fixtures/test/_layout_for_block_with_args.html.erb new file mode 100644 index 0000000000..307533208d --- /dev/null +++ b/actionpack/test/fixtures/test/_layout_for_block_with_args.html.erb @@ -0,0 +1,3 @@ +Before +<%= yield 'arg1', 'arg2' %> +After \ No newline at end of file diff --git a/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb b/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb new file mode 100644 index 0000000000..71b1f30ad0 --- /dev/null +++ b/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb @@ -0,0 +1 @@ +<% render(:layout => "layout_for_block_with_args") do |*args| %><%= args.join %><% end %> \ No newline at end of file -- cgit v1.2.3 From 71c4ff07ab4313c1f4781d59ad2f4528f5875665 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 19 Aug 2008 18:53:46 -0500 Subject: Delegate xhr helper method to integration session --- actionpack/test/controller/integration_test.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index a709343a94..b878962df3 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -253,7 +253,10 @@ class IntegrationProcessTest < ActionController::IntegrationTest session :off def get - render :text => "OK", :status => 200 + respond_to do |format| + format.html { render :text => "OK", :status => 200 } + format.js { render :text => "JS OK", :status => 200 } + end end def post @@ -345,6 +348,20 @@ class IntegrationProcessTest < ActionController::IntegrationTest end end + def test_xml_http_request_get + with_test_route_set do + xhr :get, '/get' + assert_equal 200, status + assert_equal "OK", status_message + assert_equal "200 OK", response.headers["Status"] + assert_equal ["200 OK"], headers["status"] + assert_response 200 + assert_response :success + assert_response :ok + assert_equal "JS OK", response.body + end + end + private def with_test_route_set with_routing do |set| -- cgit v1.2.3 From a8ece12fe2ac7838407954453e0d31af6186a5db Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 19 Aug 2008 19:09:04 -0500 Subject: Return nil instead of a space when passing an empty collection or nil to 'render :partial' [#791 state:resolved] Signed-off-by: Joshua Peek --- actionpack/test/controller/render_test.rb | 2 +- actionpack/test/template/render_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 1b9b12acc6..3008f5ca03 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -329,7 +329,7 @@ class RenderTest < Test::Unit::TestCase def test_render_text_with_nil get :render_text_with_nil assert_response 200 - assert_equal '', @response.body + assert_equal ' ', @response.body end def test_render_text_with_false diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 31cfdce531..25bbc263dd 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -87,6 +87,14 @@ class ViewRenderTest < Test::Unit::TestCase @view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ]) end + def test_render_partial_with_empty_collection_should_return_nil + assert_nil @view.render(:partial => "test/customer", :collection => []) + end + + def test_render_partial_with_nil_collection_should_return_nil + assert_nil @view.render(:partial => "test/customer", :collection => nil) + end + # TODO: The reason for this test is unclear, improve documentation def test_render_partial_and_fallback_to_layout assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" }) -- cgit v1.2.3 From 6f530de9443a4e2ab3d193624ff6ecd3c2b06de2 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 19 Aug 2008 20:15:51 -0500 Subject: Test coverage for integration testing with parameters --- actionpack/test/controller/integration_test.rb | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index b878962df3..a5373912f5 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -259,6 +259,10 @@ class IntegrationProcessTest < ActionController::IntegrationTest end end + def get_with_params + render :text => "foo: #{params[:foo]}", :status => 200 + end + def post render :text => "Created", :status => 201 end @@ -362,6 +366,34 @@ class IntegrationProcessTest < ActionController::IntegrationTest end end + def test_get_with_query_string + with_test_route_set do + get '/get_with_params?foo=bar' + assert_equal '/get_with_params?foo=bar', request.env["REQUEST_URI"] + assert_equal '/get_with_params?foo=bar', request.request_uri + assert_equal nil, request.env["QUERY_STRING"] + assert_equal 'foo=bar', request.query_string + assert_equal 'bar', request.parameters['foo'] + + assert_equal 200, status + assert_equal "foo: bar", response.body + end + end + + def test_get_with_parameters + with_test_route_set do + get '/get_with_params', :foo => "bar" + assert_equal '/get_with_params', request.env["REQUEST_URI"] + assert_equal '/get_with_params', request.request_uri + assert_equal 'foo=bar', request.env["QUERY_STRING"] + assert_equal 'foo=bar', request.query_string + assert_equal 'bar', request.parameters['foo'] + + assert_equal 200, status + assert_equal "foo: bar", response.body + end + end + private def with_test_route_set with_routing do |set| -- cgit v1.2.3 From a74dbe6c528a46f11e709386c282ead8049840ba Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 20 Aug 2008 13:22:36 -0500 Subject: Improve test coverage for integration tests cookie header --- actionpack/test/controller/integration_test.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index a5373912f5..47ff2dbbd3 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -329,7 +329,10 @@ class IntegrationProcessTest < ActionController::IntegrationTest assert_response :gone assert_equal nil, response.headers["Set-Cookie"] assert_equal ["cookie_1=; path=/", "cookie_3=chocolate; path=/"], headers['set-cookie'] - assert_equal [[], ["chocolate"]], response.headers["cookie"] + assert_equal [ + CGI::Cookie::new("name" => "cookie_1", "value" => ""), + CGI::Cookie::new("name" => "cookie_3", "value" => "chocolate") + ], response.headers["cookie"] assert_equal [], headers["cookie"] assert_equal({"cookie_1"=>"", "cookie_2"=>"oatmeal", "cookie_3"=>"chocolate"}, cookies) assert_equal "Gone", response.body -- cgit v1.2.3 From 47cd8b81cc19345aa68323755c78ab03f8176590 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 20 Aug 2008 13:37:18 -0500 Subject: Switched integration test runner to use Rack processor instead of CGI --- actionpack/test/controller/integration_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 47ff2dbbd3..c986941140 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -327,7 +327,7 @@ class IntegrationProcessTest < ActionController::IntegrationTest assert_equal ["410 Gone"], headers["status"] assert_response 410 assert_response :gone - assert_equal nil, response.headers["Set-Cookie"] + assert_equal ["cookie_1=; path=/", "cookie_3=chocolate; path=/"], response.headers["Set-Cookie"] assert_equal ["cookie_1=; path=/", "cookie_3=chocolate; path=/"], headers['set-cookie'] assert_equal [ CGI::Cookie::new("name" => "cookie_1", "value" => ""), -- cgit v1.2.3 From 7e4ea5f4a2fd25e06820689688e3db5a4851f8e0 Mon Sep 17 00:00:00 2001 From: Darragh Curran Date: Wed, 25 Jun 2008 11:15:26 +0100 Subject: Allow overriding id for feed and entry with atom_feed_builder. [#485 state:resolved] Signed-off-by: Pratik Naik --- actionpack/test/template/atom_feed_helper_test.rb | 37 ++++++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/atom_feed_helper_test.rb b/actionpack/test/template/atom_feed_helper_test.rb index 9f7e5b4c6c..ef31ab2c76 100644 --- a/actionpack/test/template/atom_feed_helper_test.rb +++ b/actionpack/test/template/atom_feed_helper_test.rb @@ -67,6 +67,24 @@ class ScrollsController < ActionController::Base entry.content(scroll.body, :type => 'html') entry.tag!('app:edited', Time.now) + entry.author do |author| + author.name("DHH") + end + end + end + end + EOT + FEEDS["feed_with_overridden_ids"] = <<-EOT + atom_feed({:id => 'tag:test.rubyonrails.org,2008:test/'}) do |feed| + feed.title("My great blog!") + feed.updated((@scrolls.first.created_at)) + + for scroll in @scrolls + feed.entry(scroll, :id => "tag:test.rubyonrails.org,2008:"+scroll.id.to_s) do |entry| + entry.title(scroll.title) + entry.content(scroll.body, :type => 'html') + entry.tag!('app:edited', Time.now) + entry.author do |author| author.name("DHH") end @@ -79,7 +97,7 @@ class ScrollsController < ActionController::Base Scroll.new(1, "1", "Hello One", "Something COOL!", Time.utc(2007, 12, 12, 15), Time.utc(2007, 12, 12, 15)), Scroll.new(2, "2", "Hello Two", "Something Boring", Time.utc(2007, 12, 12, 15)), ] - + render :inline => FEEDS[params[:id]], :type => :builder end @@ -98,21 +116,21 @@ class AtomFeedTest < Test::Unit::TestCase @request.host = "www.nextangle.com" end - + def test_feed_should_use_default_language_if_none_is_given with_restful_routing(:scrolls) do get :index, :id => "defaults" assert_match %r{xml:lang="en-US"}, @response.body end end - + def test_feed_should_include_two_entries with_restful_routing(:scrolls) do get :index, :id => "defaults" assert_select "entry", 2 end end - + def test_entry_should_only_use_published_if_created_at_is_present with_restful_routing(:scrolls) do get :index, :id => "defaults" @@ -167,7 +185,16 @@ class AtomFeedTest < Test::Unit::TestCase end end - private + def test_feed_should_allow_overriding_ids + with_restful_routing(:scrolls) do + get :index, :id => "feed_with_overridden_ids" + assert_select "id", :text => "tag:test.rubyonrails.org,2008:test/" + assert_select "entry id", :text => "tag:test.rubyonrails.org,2008:1" + assert_select "entry id", :text => "tag:test.rubyonrails.org,2008:2" + end + end + +private def with_restful_routing(resources) with_routing do |set| set.draw do |map| -- cgit v1.2.3