From f59984cc81bd1a64a53a2480a9b4e05fe7357d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 1 Aug 2009 15:29:39 +0200 Subject: Add nagivational behavior to respond_with. --- actionpack/test/controller/mime_responds_test.rb | 89 +++++++++++++++++++----- actionpack/test/controller/render_test.rb | 15 ++++ 2 files changed, 88 insertions(+), 16 deletions(-) (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 1d27e749ae..e9c8a3c10f 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -493,6 +493,10 @@ class RespondResource def errors [] end + + def destroyed? + false + end end class ParentResource @@ -508,7 +512,7 @@ end class RespondWithController < ActionController::Base respond_to :html, :json respond_to :xml, :except => :using_defaults - respond_to :js, :only => :using_defaults + respond_to :js, :only => [ :using_defaults, :using_resource ] def using_defaults respond_to do |format| @@ -547,12 +551,16 @@ protected self.response_body = js.respond_to?(:to_js) ? js.to_js : js end + def resources_url + request.host + "/resources" + end + def resource_url(resource) - request.host + "/resource/#{resource.to_param}" + request.host + "/resources/#{resource.to_param}" end def parent_resource_url(parent, resource) - request.host + "/parent/#{parent.to_param}/resource/#{resource.to_param}" + request.host + "/parents/#{parent.to_param}/resources/#{resource.to_param}" end end @@ -617,10 +625,10 @@ class RespondWithControllerTest < ActionController::TestCase end def test_using_resource - @request.accept = "text/html" + @request.accept = "text/javascript" get :using_resource - assert_equal "text/html", @response.content_type - assert_equal "Hello world!", @response.body + assert_equal "text/javascript", @response.content_type + assert_equal '$("body").visualEffect("highlight");', @response.body @request.accept = "application/xml" get :using_resource @@ -633,14 +641,30 @@ class RespondWithControllerTest < ActionController::TestCase end end - def test_using_resource_for_post + def test_using_resource_for_post_with_html + post :using_resource + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "www.example.com/resources/13", @response.location + assert @response.redirect? + + errors = { :name => :invalid } + RespondResource.any_instance.stubs(:errors).returns(errors) + post :using_resource + assert_equal "text/html", @response.content_type + assert_equal 200, @response.status + assert_equal "New world!\n", @response.body + assert_nil @response.location + end + + def test_using_resource_for_post_with_xml @request.accept = "application/xml" post :using_resource assert_equal "application/xml", @response.content_type assert_equal 201, @response.status assert_equal "XML", @response.body - assert_equal "www.example.com/resource/13", @response.location + assert_equal "www.example.com/resources/13", @response.location errors = { :name => :invalid } RespondResource.any_instance.stubs(:errors).returns(errors) @@ -651,14 +675,30 @@ class RespondWithControllerTest < ActionController::TestCase assert_nil @response.location end - def test_using_resource_for_put + def test_using_resource_for_put_with_html + put :using_resource + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "www.example.com/resources/13", @response.location + assert @response.redirect? + + errors = { :name => :invalid } + RespondResource.any_instance.stubs(:errors).returns(errors) + put :using_resource + assert_equal "text/html", @response.content_type + assert_equal 200, @response.status + assert_equal "Edit world!\n", @response.body + assert_nil @response.location + end + + def test_using_resource_for_put_with_xml @request.accept = "application/xml" put :using_resource assert_equal "application/xml", @response.content_type assert_equal 200, @response.status assert_equal " ", @response.body - assert_nil @response.location + assert_equal "www.example.com/resources/13", @response.location errors = { :name => :invalid } RespondResource.any_instance.stubs(:errors).returns(errors) @@ -666,16 +706,25 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal "application/xml", @response.content_type assert_equal 422, @response.status assert_equal errors.to_xml, @response.body - assert_nil @response.location + assert_nil @response.location end - def test_using_resource_for_delete + def test_using_resource_for_delete_with_html + RespondResource.any_instance.stubs(:destroyed?).returns(true) + delete :using_resource + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "www.example.com/resources", @response.location + end + + def test_using_resource_for_delete_with_xml + RespondResource.any_instance.stubs(:destroyed?).returns(true) @request.accept = "application/xml" delete :using_resource assert_equal "application/xml", @response.content_type assert_equal 200, @response.status assert_equal " ", @response.body - assert_nil @response.location + assert_equal "www.example.com/resources", @response.location end def test_using_resource_with_options @@ -692,14 +741,22 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal "JS", @response.body end - def test_using_resource_with_parent + def test_using_resource_with_parent_for_get + @request.accept = "application/xml" + get :using_resource_with_parent + assert_equal "application/xml", @response.content_type + assert_equal 200, @response.status + assert_equal "XML", @response.body + end + + def test_using_resource_with_parent_for_post @request.accept = "application/xml" post :using_resource_with_parent assert_equal "application/xml", @response.content_type assert_equal 201, @response.status assert_equal "XML", @response.body - assert_equal "www.example.com/parent/11/resource/13", @response.location + assert_equal "www.example.com/parents/11/resources/13", @response.location errors = { :name => :invalid } RespondResource.any_instance.stubs(:errors).returns(errors) @@ -739,7 +796,7 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal 406, @response.status @request.accept = "text/javascript" - get :using_resource + get :default_overwritten assert_equal 406, @response.status end end diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 947ffa9ea6..f2cd6dbb85 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -458,6 +458,10 @@ class TestController < ActionController::Base head :location => "/foo" end + def head_with_location_object + head :location => Customer.new("david") + end + def head_with_symbolic_status head :status => params[:status].intern end @@ -618,6 +622,10 @@ class TestController < ActionController::Base end private + def customer_url(customer) + request.host + "/customers/1" + end + def determine_layout case action_name when "hello_world", "layout_test", "rendering_without_layout", @@ -1084,6 +1092,13 @@ class RenderTest < ActionController::TestCase assert_response :ok end + def test_head_with_location_object + get :head_with_location_object + assert @response.body.blank? + assert_equal "www.nextangle.com/customers/1", @response.headers["Location"] + assert_response :ok + end + def test_head_with_custom_header get :head_with_custom_header assert @response.body.blank? -- cgit v1.2.3