diff options
author | José Valim <jose.valim@gmail.com> | 2009-07-31 21:49:29 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-07-31 21:49:29 +0200 |
commit | 072c87b532a0bfb334787928248863a2561dc849 (patch) | |
tree | 1011ad044643449db9441874fa330d3486644749 /actionpack/test | |
parent | 62fd1d3716b4b5fd1d91cdcc77003efe80fc5a7e (diff) | |
parent | b2d24baf790fee4f932fa32a8ae94f0212d14ad9 (diff) | |
download | rails-072c87b532a0bfb334787928248863a2561dc849.tar.gz rails-072c87b532a0bfb334787928248863a2561dc849.tar.bz2 rails-072c87b532a0bfb334787928248863a2561dc849.zip |
Merge branch 'respond_with'
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/controller/mime_responds_test.rb | 117 |
1 files changed, 106 insertions, 11 deletions
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 117f4ea4f0..1d27e749ae 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -474,6 +474,14 @@ end class RespondResource undef_method :to_json + def self.model_name + @_model_name ||= ActiveModel::Name.new("resource") + end + + def to_param + 13 + end + def to_xml "XML" end @@ -481,6 +489,20 @@ class RespondResource def to_js "JS" end + + def errors + [] + end +end + +class ParentResource + def self.model_name + @_model_name ||= ActiveModel::Name.new("parent") + end + + def to_param + 11 + end end class RespondWithController < ActionController::Base @@ -498,6 +520,12 @@ class RespondWithController < ActionController::Base respond_to(:js, :xml) end + def default_overwritten + respond_to do |format| + format.html { render :text => "HTML" } + end + end + def using_resource respond_with(RespondResource.new) end @@ -508,10 +536,8 @@ class RespondWithController < ActionController::Base end end - def default_overwritten - respond_to do |format| - format.html { render :text => "HTML" } - end + def using_resource_with_parent + respond_with([ParentResource.new, RespondResource.new]) end protected @@ -520,6 +546,14 @@ protected self.content_type ||= Mime::JS self.response_body = js.respond_to?(:to_js) ? js.to_js : js end + + def resource_url(resource) + request.host + "/resource/#{resource.to_param}" + end + + def parent_resource_url(parent, resource) + request.host + "/parent/#{parent.to_param}/resource/#{resource.to_param}" + end end class InheritedRespondWithController < RespondWithController @@ -576,6 +610,12 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal "<p>Hello world!</p>\n", @response.body end + def test_default_overwritten + get :default_overwritten + assert_equal "text/html", @response.content_type + assert_equal "HTML", @response.body + end + def test_using_resource @request.accept = "text/html" get :using_resource @@ -593,6 +633,51 @@ class RespondWithControllerTest < ActionController::TestCase end end + def test_using_resource_for_post + @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 + + errors = { :name => :invalid } + RespondResource.any_instance.stubs(:errors).returns(errors) + post :using_resource + assert_equal "application/xml", @response.content_type + assert_equal 422, @response.status + assert_equal errors.to_xml, @response.body + assert_nil @response.location + end + + def test_using_resource_for_put + @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 + + errors = { :name => :invalid } + RespondResource.any_instance.stubs(:errors).returns(errors) + put :using_resource + assert_equal "application/xml", @response.content_type + assert_equal 422, @response.status + assert_equal errors.to_xml, @response.body + assert_nil @response.location + end + + def test_using_resource_for_delete + @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 + end + def test_using_resource_with_options @request.accept = "application/xml" get :using_resource_with_options @@ -607,10 +692,22 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal "JS", @response.body end - def test_default_overwritten - get :default_overwritten - assert_equal "text/html", @response.content_type - assert_equal "HTML", @response.body + def test_using_resource_with_parent + @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 + + errors = { :name => :invalid } + RespondResource.any_instance.stubs(:errors).returns(errors) + post :using_resource + assert_equal "application/xml", @response.content_type + assert_equal 422, @response.status + assert_equal errors.to_xml, @response.body + assert_nil @response.location end def test_clear_respond_to @@ -648,8 +745,6 @@ class RespondWithControllerTest < ActionController::TestCase end class AbstractPostController < ActionController::Base - respond_to :html, :iphone - self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/" end @@ -658,7 +753,7 @@ class PostController < AbstractPostController around_filter :with_iphone def index - respond_to # It will use formats declared above + respond_to(:html, :iphone) end protected |