aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-08-08 19:14:35 -0300
committerEmilio Tagua <miloops@gmail.com>2009-08-08 19:14:35 -0300
commit952014315926d370f2a0b681cb765948bf2e6883 (patch)
tree3abfe7e82554c09df4b2fe8a170e90cf1f0bcf02 /actionpack/test/controller
parentae9e1e9f6d08bbd5f5c1512b72d495168e9fa5e5 (diff)
parented8a0a1c2370ab8715434ba824b2826d807401d5 (diff)
downloadrails-952014315926d370f2a0b681cb765948bf2e6883.tar.gz
rails-952014315926d370f2a0b681cb765948bf2e6883.tar.bz2
rails-952014315926d370f2a0b681cb765948bf2e6883.zip
Merge commit 'rails/master'
Conflicts: activerecord/test/cases/adapter_test.rb activerecord/test/cases/method_scoping_test.rb
Diffstat (limited to 'actionpack/test/controller')
-rw-r--r--actionpack/test/controller/caching_test.rb15
-rw-r--r--actionpack/test/controller/mime_responds_test.rb198
-rw-r--r--actionpack/test/controller/render_test.rb17
-rw-r--r--actionpack/test/controller/render_xml_test.rb4
-rw-r--r--actionpack/test/controller/routing_test.rb2
5 files changed, 193 insertions, 43 deletions
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index c286976315..68529cc8f7 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -625,6 +625,21 @@ class FragmentCachingTest < ActionController::TestCase
assert !fragment_computed
assert_equal 'generated till now -> fragment content', buffer
end
+
+ def test_fragment_for_logging
+ fragment_computed = false
+
+ @controller.class.expects(:benchmark).with('Cached fragment exists?: views/expensive')
+ @controller.class.expects(:benchmark).with('Cached fragment miss: views/expensive')
+ @controller.class.expects(:benchmark).with('Cached fragment hit: views/expensive').never
+
+ buffer = 'generated till now -> '
+ @controller.fragment_for(buffer, 'expensive') { fragment_computed = true }
+
+ assert fragment_computed
+ assert_equal 'generated till now -> ', buffer
+ end
+
end
class FunctionalCachingController < ActionController::Base
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb
index 117f4ea4f0..8319b5c573 100644
--- a/actionpack/test/controller/mime_responds_test.rb
+++ b/actionpack/test/controller/mime_responds_test.rb
@@ -1,4 +1,5 @@
require 'abstract_unit'
+require 'controller/fake_models'
class RespondToController < ActionController::Base
layout :set_layout
@@ -471,22 +472,10 @@ class RespondToControllerTest < ActionController::TestCase
end
end
-class RespondResource
- undef_method :to_json
-
- def to_xml
- "XML"
- end
-
- def to_js
- "JS"
- end
-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|
@@ -498,20 +487,27 @@ 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)
+ respond_with(Customer.new("david", 13))
end
- def using_resource_with_options
- respond_with(RespondResource.new, :status => :unprocessable_entity) do |format|
- format.js
- end
+ def using_resource_with_parent
+ respond_with([Quiz::Store.new("developer?", 11), Customer.new("david", 13)])
end
- def default_overwritten
- respond_to do |format|
- format.html { render :text => "HTML" }
- end
+ def using_resource_with_status_and_location
+ respond_with(Customer.new("david", 13), :location => "http://test.host/", :status => :created)
+ end
+
+ def using_resource_with_responder
+ responder = proc { |c, r, o| c.render :text => "Resource name is #{r.name}" }
+ respond_with(Customer.new("david", 13), :responder => responder)
end
protected
@@ -527,7 +523,7 @@ class InheritedRespondWithController < RespondWithController
respond_to :xml, :json
def index
- respond_with(RespondResource.new) do |format|
+ respond_with(Customer.new("david", 13)) do |format|
format.json { render :text => "JSON" }
end
end
@@ -540,6 +536,11 @@ class RespondWithControllerTest < ActionController::TestCase
super
ActionController::Base.use_accept_header = true
@request.host = "www.example.com"
+
+ ActionController::Routing::Routes.draw do |map|
+ map.resources :customers
+ map.resources :quiz_stores, :has_many => :customers
+ end
end
def teardown
@@ -576,11 +577,17 @@ 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"
+ @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
@@ -593,24 +600,114 @@ class RespondWithControllerTest < ActionController::TestCase
end
end
- def test_using_resource_with_options
+ 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 "http://www.example.com/customers/13", @response.location
+ assert @response.redirect?
+
+ errors = { :name => :invalid }
+ Customer.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"
- get :using_resource_with_options
+
+ post :using_resource
assert_equal "application/xml", @response.content_type
- assert_equal 422, @response.status
+ assert_equal 201, @response.status
assert_equal "XML", @response.body
+ assert_equal "http://www.example.com/customers/13", @response.location
- @request.accept = "text/javascript"
- get :using_resource_with_options
- assert_equal "text/javascript", @response.content_type
+ errors = { :name => :invalid }
+ Customer.any_instance.stubs(:errors).returns(errors)
+ post :using_resource
+ assert_equal "application/xml", @response.content_type
assert_equal 422, @response.status
- assert_equal "JS", @response.body
+ assert_equal errors.to_xml, @response.body
+ assert_nil @response.location
end
- def test_default_overwritten
- get :default_overwritten
+ def test_using_resource_for_put_with_html
+ put :using_resource
assert_equal "text/html", @response.content_type
- assert_equal "HTML", @response.body
+ assert_equal 302, @response.status
+ assert_equal "http://www.example.com/customers/13", @response.location
+ assert @response.redirect?
+
+ errors = { :name => :invalid }
+ Customer.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
+
+ errors = { :name => :invalid }
+ Customer.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_with_html
+ Customer.any_instance.stubs(:destroyed?).returns(true)
+ delete :using_resource
+ assert_equal "text/html", @response.content_type
+ assert_equal 302, @response.status
+ assert_equal "http://www.example.com/customers", @response.location
+ end
+
+ def test_using_resource_for_delete_with_xml
+ Customer.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
+ end
+
+ 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 "http://www.example.com/quiz_stores/11/customers/13", @response.location
+
+ errors = { :name => :invalid }
+ Customer.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
@@ -628,6 +725,29 @@ class RespondWithControllerTest < ActionController::TestCase
assert_equal "XML", @response.body
end
+ def test_no_double_render_is_raised
+ @request.accept = "text/html"
+ assert_raise ActionView::MissingTemplate do
+ get :using_resource
+ end
+ end
+
+ def test_using_resource_with_status_and_location
+ @request.accept = "text/html"
+ post :using_resource_with_status_and_location
+ assert @response.redirect?
+ assert_equal "http://test.host/", @response.location
+
+ @request.accept = "application/xml"
+ get :using_resource_with_status_and_location
+ assert_equal 201, @response.status
+ end
+
+ def test_using_resource_with_responder
+ get :using_resource_with_responder
+ assert_equal "Resource name is david", @response.body
+ end
+
def test_not_acceptable
@request.accept = "application/xml"
get :using_defaults
@@ -642,14 +762,12 @@ 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
class AbstractPostController < ActionController::Base
- respond_to :html, :iphone
-
self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
end
@@ -658,7 +776,7 @@ class PostController < AbstractPostController
around_filter :with_iphone
def index
- respond_to # It will use formats declared above
+ respond_to(:html, :iphone)
end
protected
diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb
index 947ffa9ea6..9546fdb50d 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", 1)
+ end
+
def head_with_symbolic_status
head :status => params[:status].intern
end
@@ -618,6 +622,7 @@ class TestController < ActionController::Base
end
private
+
def determine_layout
case action_name
when "hello_world", "layout_test", "rendering_without_layout",
@@ -1084,6 +1089,18 @@ class RenderTest < ActionController::TestCase
assert_response :ok
end
+ def test_head_with_location_object
+ ActionController::Routing::Routes.draw do |map|
+ map.resources :customers
+ map.connect ':controller/:action/:id'
+ end
+
+ get :head_with_location_object
+ assert @response.body.blank?
+ assert_equal "http://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?
diff --git a/actionpack/test/controller/render_xml_test.rb b/actionpack/test/controller/render_xml_test.rb
index 052b4f0b52..139f55d8bd 100644
--- a/actionpack/test/controller/render_xml_test.rb
+++ b/actionpack/test/controller/render_xml_test.rb
@@ -11,7 +11,7 @@ class TestController < ActionController::Base
def render_with_object_location
customer = Customer.new("Some guy", 1)
- render :xml => "<customer/>", :location => customer_url(customer), :status => :created
+ render :xml => "<customer/>", :location => customer, :status => :created
end
def render_with_to_xml
@@ -78,4 +78,4 @@ class RenderTest < ActionController::TestCase
get :implicit_content_type, :format => 'atom'
assert_equal Mime::ATOM, @response.content_type
end
-end \ No newline at end of file
+end
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index fb83dba395..5f9ae6292c 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -1,3 +1,4 @@
+# encoding: utf-8
require 'abstract_unit'
require 'controller/fake_controllers'
require 'active_support/dependencies'
@@ -1179,7 +1180,6 @@ class LegacyRouteSetTests < Test::Unit::TestCase
assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
- token.force_encoding("UTF-8") if token.respond_to?(:force_encoding)
escaped_token = CGI::escape(token)
assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)