From 9766997f4ce26fe0d97d7b9eebf885ddb517c80c Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Wed, 30 Mar 2011 20:53:42 +0200 Subject: when using respond_with with an invalid resource and custom options, the default response status and error messages should be returned MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/test/controller/mime_responds_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 5debf96232..eead857927 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -953,6 +953,23 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal 201, @response.status end + def test_using_resource_with_status_and_location_with_invalid_resource + errors = { :name => :invalid } + Customer.any_instance.stubs(:errors).returns(errors) + + @request.accept = "text/xml" + + post :using_resource_with_status_and_location + assert_equal errors.to_xml, @response.body + assert_equal 422, @response.status + assert_equal nil, @response.location + + put :using_resource_with_status_and_location + assert_equal errors.to_xml, @response.body + assert_equal 422, @response.status + assert_equal nil, @response.location + end + def test_using_resource_with_responder get :using_resource_with_responder assert_equal "Resource name is david", @response.body -- cgit v1.2.3 From 48404a751d7cab1556c390a5915c90947d56b46e Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Thu, 31 Mar 2011 18:18:11 +0200 Subject: only try to display an api template in responders if the request is a get or there are no errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/test/controller/mime_responds_test.rb | 21 +++++++++++++++++++++ .../using_invalid_resource_with_template.xml.erb | 1 + 2 files changed, 22 insertions(+) create mode 100644 actionpack/test/fixtures/respond_with/using_invalid_resource_with_template.xml.erb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index eead857927..4fde08b3f5 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -558,6 +558,10 @@ class RespondWithController < ActionController::Base respond_with(resource, :location => "http://test.host/", :status => :created) end + def using_invalid_resource_with_template + respond_with(resource) + end + def using_resource_with_responder responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" } respond_with(resource, :responder => responder) @@ -970,6 +974,23 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal nil, @response.location end + def test_using_invalid_resource_with_template + errors = { :name => :invalid } + Customer.any_instance.stubs(:errors).returns(errors) + + @request.accept = "text/xml" + + post :using_invalid_resource_with_template + assert_equal errors.to_xml, @response.body + assert_equal 422, @response.status + assert_equal nil, @response.location + + put :using_invalid_resource_with_template + assert_equal errors.to_xml, @response.body + assert_equal 422, @response.status + assert_equal nil, @response.location + end + def test_using_resource_with_responder get :using_resource_with_responder assert_equal "Resource name is david", @response.body diff --git a/actionpack/test/fixtures/respond_with/using_invalid_resource_with_template.xml.erb b/actionpack/test/fixtures/respond_with/using_invalid_resource_with_template.xml.erb new file mode 100644 index 0000000000..bf5869ed22 --- /dev/null +++ b/actionpack/test/fixtures/respond_with/using_invalid_resource_with_template.xml.erb @@ -0,0 +1 @@ +I should not be displayed \ No newline at end of file -- cgit v1.2.3 From b45302d7676a5e38d82662f9068ee6d832ff2e3c Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Thu, 31 Mar 2011 18:25:29 +0200 Subject: pass respond_with options to controller render when using a template for api navigation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/test/controller/mime_responds_test.rb | 19 +++++++++++++++++++ .../respond_with/using_options_with_template.xml.erb | 1 + 2 files changed, 20 insertions(+) create mode 100644 actionpack/test/fixtures/respond_with/using_options_with_template.xml.erb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 4fde08b3f5..41f80d0784 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -562,6 +562,11 @@ class RespondWithController < ActionController::Base respond_with(resource) end + def using_options_with_template + @customer = resource + respond_with(@customer, :status => 123, :location => "http://test.host/") + end + def using_resource_with_responder responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" } respond_with(resource, :responder => responder) @@ -991,6 +996,20 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal nil, @response.location end + def test_using_options_with_template + @request.accept = "text/xml" + + post :using_options_with_template + assert_equal "david", @response.body + assert_equal 123, @response.status + assert_equal "http://test.host/", @response.location + + put :using_options_with_template + assert_equal "david", @response.body + assert_equal 123, @response.status + assert_equal "http://test.host/", @response.location + end + def test_using_resource_with_responder get :using_resource_with_responder assert_equal "Resource name is david", @response.body diff --git a/actionpack/test/fixtures/respond_with/using_options_with_template.xml.erb b/actionpack/test/fixtures/respond_with/using_options_with_template.xml.erb new file mode 100644 index 0000000000..b313017913 --- /dev/null +++ b/actionpack/test/fixtures/respond_with/using_options_with_template.xml.erb @@ -0,0 +1 @@ +<%= @customer.name %> \ No newline at end of file -- cgit v1.2.3 From cc58fe79ac6f4d5fd54a39ff6e7f087c6a04fee8 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 2 Apr 2011 23:45:07 -0300 Subject: Implicit actions named not_implemented can be rendered --- .../test/controller/new_base/render_implicit_action_test.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/new_base/render_implicit_action_test.rb b/actionpack/test/controller/new_base/render_implicit_action_test.rb index 667a9021be..3bb3016fdb 100644 --- a/actionpack/test/controller/new_base/render_implicit_action_test.rb +++ b/actionpack/test/controller/new_base/render_implicit_action_test.rb @@ -3,8 +3,9 @@ require 'abstract_unit' module RenderImplicitAction class SimpleController < ::ApplicationController self.view_paths = [ActionView::FixtureResolver.new( - "render_implicit_action/simple/hello_world.html.erb" => "Hello world!", - "render_implicit_action/simple/hyphen-ated.html.erb" => "Hello hyphen-ated!" + "render_implicit_action/simple/hello_world.html.erb" => "Hello world!", + "render_implicit_action/simple/hyphen-ated.html.erb" => "Hello hyphen-ated!", + "render_implicit_action/simple/not_implemented.html.erb" => "Not Implemented" )] def hello_world() end @@ -25,9 +26,17 @@ module RenderImplicitAction assert_status 200 end + test "render an action called not_implemented" do + get "/render_implicit_action/simple/not_implemented" + + assert_body "Not Implemented" + assert_status 200 + end + test "action_method? returns true for implicit actions" do assert SimpleController.new.action_method?(:hello_world) assert SimpleController.new.action_method?(:"hyphen-ated") + assert SimpleController.new.action_method?(:not_implemented) end end end -- cgit v1.2.3 From d7a5638dfbe68d0a92958c0e81f44054ddd7d291 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 4 Apr 2011 21:52:37 -0300 Subject: raise if someone tries to modify the flash when it was already streamed back to the client or converted to HTTP headers --- actionpack/test/controller/flash_test.rb | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index 3569a2f213..a9c4f34275 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -214,11 +214,20 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33' class TestController < ActionController::Base + def dont_set_flash + head :ok + end + def set_flash flash["that"] = "hello" head :ok end + def set_flash_now + flash.now["that"] = "hello" + head :ok + end + def use_flash render :inline => "flash: #{flash["that"]}" end @@ -245,6 +254,47 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest end end + def test_setting_flash_raises_after_stream_back_to_client + with_test_route_set do + env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new } + get '/set_flash', nil, env + assert_raise(ActionDispatch::ClosedError) { + @request.flash['alert'] = 'alert' + } + end + end + + def test_setting_flash_raises_after_stream_back_to_client_even_with_an_empty_flash + with_test_route_set do + env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new } + get '/dont_set_flash', nil, env + assert_raise(ActionDispatch::ClosedError) { + @request.flash['alert'] = 'alert' + } + end + end + + def test_setting_flash_now_raises_after_stream_back_to_client + with_test_route_set do + env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new } + get '/set_flash_now', nil, env + assert_raise(ActionDispatch::ClosedError) { + @request.flash.now['alert'] = 'alert' + } + end + end + + def test_setting_flash_now_raises_after_stream_back_to_client_even_with_an_empty_flash + with_test_route_set do + env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new } + get '/dont_set_flash', nil, env + assert_raise(ActionDispatch::ClosedError) { + @request.flash.now['alert'] = 'alert' + } + end + end + + private # Overwrite get to send SessionSecret in env hash -- cgit v1.2.3 From 2e757bc298cef715e5c56945161bbd84f2610729 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 5 Apr 2011 13:59:35 -0700 Subject: do not return html safe strings from auto_link --- actionpack/test/template/text_helper_test.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index d0d4286393..a4fcff5167 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -315,14 +315,20 @@ class TextHelperTest < ActionView::TestCase end end - def test_auto_link_should_be_html_safe + def test_auto_link_should_not_be_html_safe email_raw = 'santiago@wyeworks.com' link_raw = 'http://www.rubyonrails.org' - assert auto_link(nil).html_safe? - assert auto_link('').html_safe? - assert auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe? - assert auto_link("hello #{email_raw}").html_safe? + assert !auto_link(nil).html_safe?, 'should not be html safe' + assert !auto_link('').html_safe?, 'should not be html safe' + assert !auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe?, 'should not be html safe' + assert !auto_link("hello #{email_raw}").html_safe?, 'should not be html safe' + end + + def test_auto_link_email_address + email_raw = 'aaron@tenderlovemaking.com' + email_result = %{#{email_raw}} + assert !auto_link_email_addresses(email_result).html_safe?, 'should not be html safe' end def test_auto_link -- cgit v1.2.3 From 0c5aded0922f80bd1a31c7d2a3974469a18160a8 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 12:05:58 -0300 Subject: raise if someone tries to modify the cookies when it was already streamed back to the client or converted to HTTP headers --- actionpack/test/dispatch/cookies_test.rb | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index 39159fd629..0d374e1d8b 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -495,3 +495,54 @@ class CookiesTest < ActionController::TestCase end end end + +class CookiesIntegrationTest < ActionDispatch::IntegrationTest + class TestController < ActionController::Base + def dont_set_cookies + head :ok + end + + def set_cookies + cookies["that"] = "hello" + head :ok + end + end + + def test_setting_cookies_raises_after_stream_back_to_client + with_test_route_set do + env = {} + get '/set_cookies', nil, env + assert_raise(ActionDispatch::ClosedError) { + request.cookie_jar['alert'] = 'alert' + cookies['alert'] = 'alert' + } + end + end + + def test_setting_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash + with_test_route_set do + env = {} + get '/dont_set_cookies', nil, {} + assert_raise(ActionDispatch::ClosedError) { + request.cookie_jar['alert'] = 'alert' + } + end + end + + private + + def with_test_route_set + with_routing do |set| + set.draw do + match ':action', :to => CookiesIntegrationTest::TestController + end + + @app = self.class.build_app(set) do |middleware| + middleware.use ActionDispatch::Cookies + middleware.delete "ActionDispatch::ShowExceptions" + end + + yield + end + end +end -- cgit v1.2.3 From 0e4748cd415660eb91e63d50aa15cdd027c612dd Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 16:37:55 -0300 Subject: Make process reuse the env var passed as argument --- actionpack/test/controller/integration_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index f0d62b0b13..01dc2f2091 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -521,4 +521,12 @@ class ApplicationIntegrationTest < ActionDispatch::IntegrationTest get '/foo' assert_raise(NameError) { missing_path } end + + test "process reuse the env we pass as argument" do + env = { :SERVER_NAME => 'server', 'action_dispatch.custom' => 'custom' } + get '/foo', nil, env + assert_equal :get, env[:method] + assert_equal 'server', env[:SERVER_NAME] + assert_equal 'custom', env['action_dispatch.custom'] + end end -- cgit v1.2.3 From 0d455673620a1646b1149e1e4a185143d46bb7b8 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 19:12:32 -0300 Subject: Add tests to verify that signed and permanent cookies raises if someone tries to modify the cookies when it was already streamed back to the client or converted to HTTP headers --- actionpack/test/dispatch/cookies_test.rb | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index 0d374e1d8b..2a32614ca1 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -497,6 +497,9 @@ class CookiesTest < ActionController::TestCase end class CookiesIntegrationTest < ActionDispatch::IntegrationTest + SessionKey = '_myapp_session' + SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33' + class TestController < ActionController::Base def dont_set_cookies head :ok @@ -529,8 +532,56 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest end end + def test_setting_permanent_cookies_raises_after_stream_back_to_client + with_test_route_set do + env = {} + get '/set_cookies', nil, env + assert_raise(ActionDispatch::ClosedError) { + request.cookie_jar.permanent['alert'] = 'alert' + cookies['alert'] = 'alert' + } + end + end + + def test_setting_permanent_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash + with_test_route_set do + env = {} + get '/dont_set_cookies', nil, {} + assert_raise(ActionDispatch::ClosedError) { + request.cookie_jar.permanent['alert'] = 'alert' + } + end + end + + def test_setting_signed_cookies_raises_after_stream_back_to_client + with_test_route_set do + env = {} + get '/set_cookies', nil, env + assert_raise(ActionDispatch::ClosedError) { + request.cookie_jar.signed['alert'] = 'alert' + cookies['alert'] = 'alert' + } + end + end + + def test_setting_signed_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash + with_test_route_set do + env = {} + get '/dont_set_cookies', nil, {} + assert_raise(ActionDispatch::ClosedError) { + request.cookie_jar.signed['alert'] = 'alert' + } + end + end + private + # Overwrite get to send SessionSecret in env hash + def get(path, parameters = nil, env = {}) + env["action_dispatch.secret_token"] ||= SessionSecret + super + end + def with_test_route_set with_routing do |set| set.draw do -- cgit v1.2.3 From ed042436295f0f55dc57c582d0b94628e6376e97 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 19:15:33 -0300 Subject: Delete useless env variable --- actionpack/test/dispatch/cookies_test.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index 2a32614ca1..2eb477e472 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -513,8 +513,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_cookies_raises_after_stream_back_to_client with_test_route_set do - env = {} - get '/set_cookies', nil, env + get '/set_cookies', nil, {} assert_raise(ActionDispatch::ClosedError) { request.cookie_jar['alert'] = 'alert' cookies['alert'] = 'alert' @@ -524,7 +523,6 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash with_test_route_set do - env = {} get '/dont_set_cookies', nil, {} assert_raise(ActionDispatch::ClosedError) { request.cookie_jar['alert'] = 'alert' @@ -534,8 +532,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_permanent_cookies_raises_after_stream_back_to_client with_test_route_set do - env = {} - get '/set_cookies', nil, env + get '/set_cookies', nil, {} assert_raise(ActionDispatch::ClosedError) { request.cookie_jar.permanent['alert'] = 'alert' cookies['alert'] = 'alert' @@ -545,7 +542,6 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_permanent_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash with_test_route_set do - env = {} get '/dont_set_cookies', nil, {} assert_raise(ActionDispatch::ClosedError) { request.cookie_jar.permanent['alert'] = 'alert' @@ -555,8 +551,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_signed_cookies_raises_after_stream_back_to_client with_test_route_set do - env = {} - get '/set_cookies', nil, env + get '/set_cookies', nil, {} assert_raise(ActionDispatch::ClosedError) { request.cookie_jar.signed['alert'] = 'alert' cookies['alert'] = 'alert' @@ -566,7 +561,6 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_signed_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash with_test_route_set do - env = {} get '/dont_set_cookies', nil, {} assert_raise(ActionDispatch::ClosedError) { request.cookie_jar.signed['alert'] = 'alert' -- cgit v1.2.3 From 9f765f4e0990742519b91d759a6b4374d940ab98 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 19:17:39 -0300 Subject: Delete useless arguments --- actionpack/test/dispatch/cookies_test.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index 2eb477e472..1d06a755b8 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -513,7 +513,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_cookies_raises_after_stream_back_to_client with_test_route_set do - get '/set_cookies', nil, {} + get '/set_cookies' assert_raise(ActionDispatch::ClosedError) { request.cookie_jar['alert'] = 'alert' cookies['alert'] = 'alert' @@ -523,7 +523,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash with_test_route_set do - get '/dont_set_cookies', nil, {} + get '/dont_set_cookies' assert_raise(ActionDispatch::ClosedError) { request.cookie_jar['alert'] = 'alert' } @@ -532,7 +532,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_permanent_cookies_raises_after_stream_back_to_client with_test_route_set do - get '/set_cookies', nil, {} + get '/set_cookies' assert_raise(ActionDispatch::ClosedError) { request.cookie_jar.permanent['alert'] = 'alert' cookies['alert'] = 'alert' @@ -542,7 +542,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_permanent_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash with_test_route_set do - get '/dont_set_cookies', nil, {} + get '/dont_set_cookies' assert_raise(ActionDispatch::ClosedError) { request.cookie_jar.permanent['alert'] = 'alert' } @@ -551,7 +551,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_signed_cookies_raises_after_stream_back_to_client with_test_route_set do - get '/set_cookies', nil, {} + get '/set_cookies' assert_raise(ActionDispatch::ClosedError) { request.cookie_jar.signed['alert'] = 'alert' cookies['alert'] = 'alert' @@ -561,7 +561,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest def test_setting_signed_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash with_test_route_set do - get '/dont_set_cookies', nil, {} + get '/dont_set_cookies' assert_raise(ActionDispatch::ClosedError) { request.cookie_jar.signed['alert'] = 'alert' } -- cgit v1.2.3 From 29592a7f09dda2e7e1e0a915d9230fe6a9b5c0af Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 20:53:48 -0300 Subject: Use freeze instead of close! --- actionpack/test/dispatch/cookies_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index 1d06a755b8..cfa521b2d9 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -502,10 +502,16 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest class TestController < ActionController::Base def dont_set_cookies + # initialize lazy loaded objects + cookies.permanent + cookies.signed head :ok end def set_cookies + # initialize lazy loaded objects + cookies.permanent + cookies.signed cookies["that"] = "hello" head :ok end -- cgit v1.2.3 From 76c2ea7882a83159408bdf1f7c363f442a65c4f1 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 6 Apr 2011 17:26:55 -0700 Subject: favor composition over inheritance, have FlashHash delegate to a Hash --- actionpack/test/controller/flash_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index a9c4f34275..9c89f1334d 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -174,13 +174,13 @@ class FlashTest < ActionController::TestCase assert_equal(:foo_indeed, flash.discard(:foo)) # valid key passed assert_nil flash.discard(:unknown) # non existant key passed - assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard()) # nothing passed - assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard(nil)) # nothing passed + assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard().to_hash) # nothing passed + assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard(nil).to_hash) # nothing passed assert_equal(:foo_indeed, flash.keep(:foo)) # valid key passed assert_nil flash.keep(:unknown) # non existant key passed - assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep()) # nothing passed - assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep(nil)) # nothing passed + assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep().to_hash) # nothing passed + assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep(nil).to_hash) # nothing passed end def test_redirect_to_with_alert -- cgit v1.2.3 From 32f876786aa1eb718f122b116203b01ee0260113 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 6 Apr 2011 18:01:03 -0700 Subject: getting the flash hash under test --- actionpack/test/controller/flash_hash_test.rb | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 actionpack/test/controller/flash_hash_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/flash_hash_test.rb b/actionpack/test/controller/flash_hash_test.rb new file mode 100644 index 0000000000..28c338c286 --- /dev/null +++ b/actionpack/test/controller/flash_hash_test.rb @@ -0,0 +1,100 @@ +require 'abstract_unit' + +module ActionDispatch + class FlashHashTest < ActiveSupport::TestCase + def setup + @hash = Flash::FlashHash.new + end + + def test_set_get + @hash[:foo] = 'zomg' + assert_equal 'zomg', @hash[:foo] + end + + def test_keys + assert_equal [], @hash.keys + + @hash['foo'] = 'zomg' + assert_equal ['foo'], @hash.keys + + @hash['bar'] = 'zomg' + assert_equal ['foo', 'bar'].sort, @hash.keys.sort + end + + def test_update + @hash['foo'] = 'bar' + @hash.update('foo' => 'baz', 'hello' => 'world') + + assert_equal 'baz', @hash['foo'] + assert_equal 'world', @hash['hello'] + end + + def test_delete + @hash['foo'] = 'bar' + @hash.delete 'foo' + + assert !@hash.key?('foo') + assert_nil @hash['foo'] + end + + def test_to_hash + @hash['foo'] = 'bar' + assert_equal({'foo' => 'bar'}, @hash.to_hash) + + @hash.to_hash['zomg'] = 'aaron' + assert !@hash.key?('zomg') + assert_equal({'foo' => 'bar'}, @hash.to_hash) + end + + def test_empty? + assert @hash.empty? + @hash['zomg'] = 'bears' + assert !@hash.empty? + @hash.clear + assert @hash.empty? + end + + def test_each + @hash['hello'] = 'world' + @hash['foo'] = 'bar' + + things = [] + @hash.each do |k,v| + things << [k,v] + end + + assert_equal([%w{ hello world }, %w{ foo bar }].sort, things.sort) + end + + def test_replace + @hash['hello'] = 'world' + @hash.replace('omg' => 'aaron') + assert_equal({'omg' => 'aaron'}, @hash.to_hash) + end + + def test_discard_no_args + @hash['hello'] = 'world' + @hash.discard + @hash.sweep + assert_equal({}, @hash.to_hash) + end + + def test_discard_one_arg + @hash['hello'] = 'world' + @hash['omg'] = 'world' + @hash.discard 'hello' + @hash.sweep + assert_equal({'omg' => 'world'}, @hash.to_hash) + end + + def test_discard_many_args + @hash['hello'] = 'world' + @hash['<3'] = 'world' + @hash['omg'] = 'world' + + @hash.discard ['hello', 'omg'] + @hash.sweep + assert_equal({'<3' => 'world'}, @hash.to_hash) + end + end +end -- cgit v1.2.3 From 1e90229e044b82846078d370a9539bd2457793b9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 6 Apr 2011 18:13:16 -0700 Subject: many args does not make sense with the current implementation because of how `use` works --- actionpack/test/controller/flash_hash_test.rb | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/flash_hash_test.rb b/actionpack/test/controller/flash_hash_test.rb index 28c338c286..9b69a2648e 100644 --- a/actionpack/test/controller/flash_hash_test.rb +++ b/actionpack/test/controller/flash_hash_test.rb @@ -86,15 +86,5 @@ module ActionDispatch @hash.sweep assert_equal({'omg' => 'world'}, @hash.to_hash) end - - def test_discard_many_args - @hash['hello'] = 'world' - @hash['<3'] = 'world' - @hash['omg'] = 'world' - - @hash.discard ['hello', 'omg'] - @hash.sweep - assert_equal({'<3' => 'world'}, @hash.to_hash) - end end end -- cgit v1.2.3 From 17205435f8b8c01cc0ef72d8b92faf771bae1b40 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 7 Apr 2011 09:20:35 -0300 Subject: cookies here --- actionpack/test/dispatch/cookies_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index cfa521b2d9..cd07230a57 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -527,7 +527,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest end end - def test_setting_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash + def test_setting_cookies_raises_after_stream_back_to_client_even_without_cookies with_test_route_set do get '/dont_set_cookies' assert_raise(ActionDispatch::ClosedError) { @@ -546,7 +546,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest end end - def test_setting_permanent_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash + def test_setting_permanent_cookies_raises_after_stream_back_to_client_even_without_cookies with_test_route_set do get '/dont_set_cookies' assert_raise(ActionDispatch::ClosedError) { @@ -565,7 +565,7 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest end end - def test_setting_signed_cookies_raises_after_stream_back_to_client_even_with_an_empty_flash + def test_setting_signed_cookies_raises_after_stream_back_to_client_even_without_cookies with_test_route_set do get '/dont_set_cookies' assert_raise(ActionDispatch::ClosedError) { -- cgit v1.2.3 From 03d561ad77085f17ba816ebec619a3d359b2164e Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 7 Apr 2011 09:26:04 -0300 Subject: Revert "Use freeze instead of close!" This reverts commit 29592a7f09dda2e7e1e0a915d9230fe6a9b5c0af. --- actionpack/test/dispatch/cookies_test.rb | 6 ------ 1 file changed, 6 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index cd07230a57..ebc16694db 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -502,16 +502,10 @@ class CookiesIntegrationTest < ActionDispatch::IntegrationTest class TestController < ActionController::Base def dont_set_cookies - # initialize lazy loaded objects - cookies.permanent - cookies.signed head :ok end def set_cookies - # initialize lazy loaded objects - cookies.permanent - cookies.signed cookies["that"] = "hello" head :ok end -- cgit v1.2.3 From 2cdc1f0cd5b89722e8c22bb4b26b83bd4619b28a Mon Sep 17 00:00:00 2001 From: James Robinson Date: Fri, 8 Apr 2011 02:18:33 +0200 Subject: Make csrf_meta_tags use the tag helper Improved formatting of csrf_helper and improved test coverage --- actionpack/test/controller/request_forgery_protection_test.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb index d520b5e512..31f4bf3a76 100644 --- a/actionpack/test/controller/request_forgery_protection_test.rb +++ b/actionpack/test/controller/request_forgery_protection_test.rb @@ -172,13 +172,11 @@ end class RequestForgeryProtectionControllerTest < ActionController::TestCase include RequestForgeryProtectionTests - test 'should emit a csrf-token meta tag' do + test 'should emit a csrf-param meta tag and a csrf-token meta tag' do ActiveSupport::SecureRandom.stubs(:base64).returns(@token + '<=?') get :meta - assert_equal <<-METAS.strip_heredoc.chomp, @response.body - - - METAS + assert_select 'meta[name=?][content=?]', 'csrf-param', 'authenticity_token' + assert_select 'meta[name=?][content=?]', 'csrf-token', 'cf50faa3fe97702ca1ae<=?' end end -- cgit v1.2.3 From a9f3c9da01d721963d05949604ead909aaabbf36 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 11 Apr 2011 00:52:42 +0800 Subject: Using Object#in? and Object#either? in various places There're a lot of places in Rails source code which make a lot of sense to switching to Object#in? or Object#either? instead of using [].include?. --- actionpack/test/controller/mime_responds_test.rb | 3 ++- actionpack/test/controller/resources_test.rb | 7 ++++--- actionpack/test/dispatch/routing_test.rb | 3 ++- actionpack/test/template/erb_util_test.rb | 3 ++- actionpack/test/template/form_helper_test.rb | 3 ++- actionpack/test/template/form_options_helper_test.rb | 3 ++- actionpack/test/template/form_tag_helper_test.rb | 3 ++- 7 files changed, 16 insertions(+), 9 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 41f80d0784..6c5dc09011 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -1,6 +1,7 @@ require 'abstract_unit' require 'controller/fake_models' require 'active_support/core_ext/hash/conversions' +require 'active_support/core_ext/object/inclusion' class StarStarMimeController < ActionController::Base layout nil @@ -158,7 +159,7 @@ class RespondToController < ActionController::Base protected def set_layout - if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name) + if action_name.either?("all_types_with_layout", "iphone_with_html_response_type") "respond_to/layouts/standard" elsif action_name == "iphone_with_html_response_type_without_layout" "respond_to/layouts/missing" diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb index acb4617a60..6ea492cf8b 100644 --- a/actionpack/test/controller/resources_test.rb +++ b/actionpack/test/controller/resources_test.rb @@ -1,6 +1,7 @@ require 'abstract_unit' require 'active_support/core_ext/object/try' require 'active_support/core_ext/object/with_options' +require 'active_support/core_ext/object/inclusion' class ResourcesController < ActionController::Base def index() render :nothing => true end @@ -1292,7 +1293,7 @@ class ResourcesTest < ActionController::TestCase def assert_resource_methods(expected, resource, action_method, method) assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}" expected.each do |action| - assert resource.send("#{action_method}_methods")[method].include?(action), + assert action.in?(resource.send("#{action_method}_methods")[method]) "#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}" end end @@ -1329,9 +1330,9 @@ class ResourcesTest < ActionController::TestCase options = options.merge(:action => action.to_s) path_options = { :path => path, :method => method } - if Array(allowed).include?(action) + if action.in?(Array(allowed)) assert_recognizes options, path_options - elsif Array(not_allowed).include?(action) + elsif action.in?(Array(not_allowed)) assert_not_recognizes options, path_options end end diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 5e5758a60e..51355b3ac7 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -1,6 +1,7 @@ require 'erb' require 'abstract_unit' require 'controller/fake_controllers' +require 'active_support/core_ext/object/inclusion' class TestRoutingMapper < ActionDispatch::IntegrationTest SprocketsApp = lambda { |env| @@ -495,7 +496,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest resources :todos, :id => /\d+/ end - scope '/countries/:country', :constraints => lambda { |params, req| %[all France].include?(params[:country]) } do + scope '/countries/:country', :constraints => lambda { |params, req| params[:country].either?("all", "france") } do match '/', :to => 'countries#index' match '/cities', :to => 'countries#cities' end diff --git a/actionpack/test/template/erb_util_test.rb b/actionpack/test/template/erb_util_test.rb index d1891094e8..30f6d1a213 100644 --- a/actionpack/test/template/erb_util_test.rb +++ b/actionpack/test/template/erb_util_test.rb @@ -1,4 +1,5 @@ require 'abstract_unit' +require 'active_support/core_ext/object/inclusion' class ErbUtilTest < Test::Unit::TestCase include ERB::Util @@ -29,7 +30,7 @@ class ErbUtilTest < Test::Unit::TestCase def test_rest_in_ascii (0..127).to_a.map {|int| int.chr }.each do |chr| - next if %w(& " < >).include?(chr) + next if chr.in?('&"<>') assert_equal chr, html_escape(chr) end end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index ff183d097d..fa0709179b 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require 'controller/fake_models' +require 'active_support/core_ext/object/inclusion' class FormHelperTest < ActionView::TestCase tests ActionView::Helpers::FormHelper @@ -1743,7 +1744,7 @@ class FormHelperTest < ActionView::TestCase def snowman(method = nil) txt = %{
} txt << %{} - if (method && !['get','post'].include?(method.to_s)) + if method && !method.to_s.either?('get', 'post') txt << %{} end txt << %{
} diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 93ff7ba0fd..6112bb7b20 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require 'tzinfo' +require 'active_support/core_ext/object/inclusion' class Map < Hash def category @@ -82,7 +83,7 @@ class FormOptionsHelperTest < ActionView::TestCase def test_collection_options_with_proc_for_disabled assert_dom_equal( "\n\n", - options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => lambda{|p| %w(Babe Cabe).include? p.author_name }) + options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => lambda{|p| p.author_name.either?("Babe", "Cabe") }) ) end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index f8671f2980..2d6e71d39a 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -1,4 +1,5 @@ require 'abstract_unit' +require 'active_support/core_ext/object/inclusion' class FormTagHelperTest < ActionView::TestCase tests ActionView::Helpers::FormTagHelper @@ -13,7 +14,7 @@ class FormTagHelperTest < ActionView::TestCase txt = %{
} txt << %{} - if (method && !['get','post'].include?(method.to_s)) + if method && !method.to_s.either?('get','post') txt << %{} end txt << %{
} -- cgit v1.2.3 From d6edaeeaf8b6c0f0b741c0827ab6e091bdd4e197 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 11 Apr 2011 12:35:20 +0700 Subject: Fix failing test case on master It turned out that I overlook at some replacements .. --- actionpack/test/dispatch/routing_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 51355b3ac7..8d23d63ebb 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -496,7 +496,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest resources :todos, :id => /\d+/ end - scope '/countries/:country', :constraints => lambda { |params, req| params[:country].either?("all", "france") } do + scope '/countries/:country', :constraints => lambda { |params, req| params[:country].either?("all", "France") } do match '/', :to => 'countries#index' match '/cities', :to => 'countries#cities' end -- cgit v1.2.3 From d1575ae1b9658c91145d6a46ec2a144a5a089207 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 12 Apr 2011 00:23:07 +0200 Subject: Change Object#either? to Object#among? -- thanks to @jamesarosen for the suggestion! --- actionpack/test/controller/mime_responds_test.rb | 2 +- actionpack/test/dispatch/routing_test.rb | 2 +- actionpack/test/template/form_helper_test.rb | 2 +- actionpack/test/template/form_options_helper_test.rb | 2 +- actionpack/test/template/form_tag_helper_test.rb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 6c5dc09011..be5866e5aa 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -159,7 +159,7 @@ class RespondToController < ActionController::Base protected def set_layout - if action_name.either?("all_types_with_layout", "iphone_with_html_response_type") + if action_name.among?("all_types_with_layout", "iphone_with_html_response_type") "respond_to/layouts/standard" elsif action_name == "iphone_with_html_response_type_without_layout" "respond_to/layouts/missing" diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 8d23d63ebb..cf7a5aaa3b 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -496,7 +496,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest resources :todos, :id => /\d+/ end - scope '/countries/:country', :constraints => lambda { |params, req| params[:country].either?("all", "France") } do + scope '/countries/:country', :constraints => lambda { |params, req| params[:country].among?("all", "France") } do match '/', :to => 'countries#index' match '/cities', :to => 'countries#cities' end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index fa0709179b..435ce81238 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1744,7 +1744,7 @@ class FormHelperTest < ActionView::TestCase def snowman(method = nil) txt = %{
} txt << %{} - if method && !method.to_s.either?('get', 'post') + if method && !method.to_s.among?('get', 'post') txt << %{} end txt << %{
} diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 6112bb7b20..76236aee41 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -83,7 +83,7 @@ class FormOptionsHelperTest < ActionView::TestCase def test_collection_options_with_proc_for_disabled assert_dom_equal( "\n\n", - options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => lambda{|p| p.author_name.either?("Babe", "Cabe") }) + options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => lambda{|p| p.author_name.among?("Babe", "Cabe") }) ) end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 2d6e71d39a..aa2114ff79 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -14,7 +14,7 @@ class FormTagHelperTest < ActionView::TestCase txt = %{
} txt << %{} - if method && !method.to_s.either?('get','post') + if method && !method.to_s.among?('get','post') txt << %{} end txt << %{
} -- cgit v1.2.3