2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2009-08-01 10:47:44 -04:00
|
|
|
require 'controller/fake_models'
|
2007-09-27 21:23:20 -04:00
|
|
|
|
2009-07-29 05:13:08 -04:00
|
|
|
class RespondWithController < ActionController::Base
|
2012-07-06 11:19:20 -04:00
|
|
|
respond_to :html, :json, :touch
|
2010-01-10 14:21:08 -05:00
|
|
|
respond_to :xml, :except => :using_resource_with_block
|
2011-10-11 04:48:03 -04:00
|
|
|
respond_to :js, :only => [ :using_resource_with_block, :using_resource, 'using_hash_resource' ]
|
2009-07-28 15:29:29 -04:00
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def using_resource
|
|
|
|
respond_with(resource)
|
2009-07-29 05:13:08 -04:00
|
|
|
end
|
|
|
|
|
2010-11-04 22:28:35 -04:00
|
|
|
def using_hash_resource
|
|
|
|
respond_with({:result => resource})
|
|
|
|
end
|
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def using_resource_with_block
|
|
|
|
respond_with(resource) do |format|
|
|
|
|
format.csv { render :text => "CSV" }
|
|
|
|
end
|
2009-07-29 05:13:08 -04:00
|
|
|
end
|
2009-07-29 06:18:03 -04:00
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def using_resource_with_overwrite_block
|
|
|
|
respond_with(resource) do |format|
|
2009-07-31 14:56:53 -04:00
|
|
|
format.html { render :text => "HTML" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-13 04:27:41 -04:00
|
|
|
def using_resource_with_collection
|
2010-01-10 14:21:08 -05:00
|
|
|
respond_with([resource, Customer.new("jamis", 9)])
|
2009-08-13 04:27:41 -04:00
|
|
|
end
|
|
|
|
|
2009-07-31 14:56:53 -04:00
|
|
|
def using_resource_with_parent
|
2009-08-13 04:27:41 -04:00
|
|
|
respond_with(Quiz::Store.new("developer?", 11), Customer.new("david", 13))
|
2009-07-29 08:32:56 -04:00
|
|
|
end
|
|
|
|
|
2009-08-06 17:48:48 -04:00
|
|
|
def using_resource_with_status_and_location
|
2010-01-10 14:21:08 -05:00
|
|
|
respond_with(resource, :location => "http://test.host/", :status => :created)
|
2009-08-06 17:48:48 -04:00
|
|
|
end
|
|
|
|
|
2011-03-31 12:18:11 -04:00
|
|
|
def using_invalid_resource_with_template
|
|
|
|
respond_with(resource)
|
|
|
|
end
|
|
|
|
|
2011-03-31 12:25:29 -04:00
|
|
|
def using_options_with_template
|
|
|
|
@customer = resource
|
|
|
|
respond_with(@customer, :status => 123, :location => "http://test.host/")
|
|
|
|
end
|
|
|
|
|
2009-08-08 05:07:07 -04:00
|
|
|
def using_resource_with_responder
|
2009-08-13 04:27:41 -04:00
|
|
|
responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
|
2010-01-10 14:21:08 -05:00
|
|
|
respond_with(resource, :responder => responder)
|
2009-08-05 06:36:43 -04:00
|
|
|
end
|
|
|
|
|
2009-11-11 11:45:21 -05:00
|
|
|
def using_resource_with_action
|
2010-01-10 14:21:08 -05:00
|
|
|
respond_with(resource, :action => :foo) do |format|
|
2010-12-27 02:44:51 -05:00
|
|
|
format.html { raise ActionView::MissingTemplate.new([], "bar", ["foo"], {}, false) }
|
2009-11-11 11:45:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-01 10:11:24 -05:00
|
|
|
def using_responder_with_respond
|
|
|
|
responder = Class.new(ActionController::Responder) do
|
|
|
|
def respond; @controller.render :text => "respond #{format}"; end
|
|
|
|
end
|
2010-01-10 14:21:08 -05:00
|
|
|
respond_with(resource, :responder => responder)
|
2009-12-01 10:11:24 -05:00
|
|
|
end
|
|
|
|
|
2013-08-30 05:53:03 -04:00
|
|
|
def respond_with_additional_params
|
|
|
|
@params = RespondWithController.params
|
|
|
|
respond_with({:result => resource}, @params)
|
|
|
|
end
|
|
|
|
|
2009-07-29 06:18:03 -04:00
|
|
|
protected
|
2013-08-30 05:53:03 -04:00
|
|
|
def self.params
|
|
|
|
{
|
|
|
|
:foo => 'bar'
|
|
|
|
}
|
|
|
|
end
|
2009-07-29 06:18:03 -04:00
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def resource
|
2010-02-21 05:09:21 -05:00
|
|
|
Customer.new("david", request.delete? ? nil : 13)
|
2010-01-10 14:21:08 -05:00
|
|
|
end
|
2009-07-29 05:13:08 -04:00
|
|
|
end
|
|
|
|
|
2009-07-29 08:32:56 -04:00
|
|
|
class InheritedRespondWithController < RespondWithController
|
|
|
|
clear_respond_to
|
|
|
|
respond_to :xml, :json
|
|
|
|
|
|
|
|
def index
|
2010-01-10 14:21:08 -05:00
|
|
|
respond_with(resource) do |format|
|
2009-07-29 08:32:56 -04:00
|
|
|
format.json { render :text => "JSON" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-15 17:52:39 -05:00
|
|
|
class RenderJsonRespondWithController < RespondWithController
|
|
|
|
clear_respond_to
|
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
def index
|
|
|
|
respond_with(resource) do |format|
|
|
|
|
format.json { render :json => RenderJsonTestException.new('boom') }
|
|
|
|
end
|
|
|
|
end
|
2012-02-03 11:47:47 -05:00
|
|
|
|
|
|
|
def create
|
|
|
|
resource = ValidatedCustomer.new(params[:name], 1)
|
|
|
|
respond_with(resource) do |format|
|
|
|
|
format.json do
|
|
|
|
if resource.errors.empty?
|
|
|
|
render :json => { :valid => true }
|
|
|
|
else
|
|
|
|
render :json => { :valid => false }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-11-15 17:52:39 -05:00
|
|
|
end
|
|
|
|
|
2013-08-17 21:45:50 -04:00
|
|
|
class CsvRespondWithController < ActionController::Base
|
|
|
|
respond_to :csv
|
|
|
|
|
|
|
|
class RespondWithCsv
|
|
|
|
def to_csv
|
|
|
|
"c,s,v"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
|
|
|
respond_with(RespondWithCsv.new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
class EmptyRespondWithController < ActionController::Base
|
|
|
|
def index
|
|
|
|
respond_with(Customer.new("david", 13))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-29 05:13:08 -04:00
|
|
|
class RespondWithControllerTest < ActionController::TestCase
|
|
|
|
tests RespondWithController
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@request.host = "www.example.com"
|
2010-11-28 06:48:50 -05:00
|
|
|
Mime::Type.register_alias('text/html', :iphone)
|
2012-07-06 11:19:20 -04:00
|
|
|
Mime::Type.register_alias('text/html', :touch)
|
2010-11-28 06:48:50 -05:00
|
|
|
Mime::Type.register('text/x-mobile', :mobile)
|
2009-07-29 05:13:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
2010-11-28 06:48:50 -05:00
|
|
|
Mime::Type.unregister(:iphone)
|
2012-07-06 11:19:20 -04:00
|
|
|
Mime::Type.unregister(:touch)
|
2010-11-28 06:48:50 -05:00
|
|
|
Mime::Type.unregister(:mobile)
|
2009-07-29 05:13:08 -04:00
|
|
|
end
|
|
|
|
|
2013-08-30 05:53:03 -04:00
|
|
|
def test_respond_with_shouldnt_modify_original_hash
|
|
|
|
get :respond_with_additional_params
|
|
|
|
assert_equal RespondWithController.params, assigns(:params)
|
|
|
|
end
|
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def test_using_resource
|
|
|
|
@request.accept = "application/xml"
|
|
|
|
get :using_resource
|
|
|
|
assert_equal "application/xml", @response.content_type
|
|
|
|
assert_equal "<name>david</name>", @response.body
|
|
|
|
|
|
|
|
@request.accept = "application/json"
|
|
|
|
assert_raise ActionView::MissingTemplate do
|
|
|
|
get :using_resource
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-30 10:47:14 -04:00
|
|
|
def test_using_resource_with_js_simply_tries_to_render_the_template
|
|
|
|
@request.accept = "text/javascript"
|
|
|
|
get :using_resource
|
|
|
|
assert_equal "text/javascript", @response.content_type
|
|
|
|
assert_equal "alert(\"Hi\");", @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_using_hash_resource_with_js_raises_an_error_if_template_cant_be_found
|
|
|
|
@request.accept = "text/javascript"
|
|
|
|
assert_raise ActionView::MissingTemplate do
|
|
|
|
get :using_hash_resource
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-04 22:28:35 -04:00
|
|
|
def test_using_hash_resource
|
|
|
|
@request.accept = "application/xml"
|
|
|
|
get :using_hash_resource
|
|
|
|
assert_equal "application/xml", @response.content_type
|
|
|
|
assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <name>david</name>\n</hash>\n", @response.body
|
|
|
|
|
|
|
|
@request.accept = "application/json"
|
|
|
|
get :using_hash_resource
|
|
|
|
assert_equal "application/json", @response.content_type
|
2011-10-24 04:41:20 -04:00
|
|
|
assert @response.body.include?("result")
|
|
|
|
assert @response.body.include?('"name":"david"')
|
|
|
|
assert @response.body.include?('"id":13')
|
2010-11-04 22:28:35 -04:00
|
|
|
end
|
|
|
|
|
2011-07-02 12:02:43 -04:00
|
|
|
def test_using_hash_resource_with_post
|
|
|
|
@request.accept = "application/json"
|
|
|
|
assert_raise ArgumentError, "Nil location provided. Can't build URI." do
|
|
|
|
post :using_hash_resource
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def test_using_resource_with_block
|
2009-07-29 05:13:08 -04:00
|
|
|
@request.accept = "*/*"
|
2010-01-10 14:21:08 -05:00
|
|
|
get :using_resource_with_block
|
2009-07-29 05:13:08 -04:00
|
|
|
assert_equal "text/html", @response.content_type
|
|
|
|
assert_equal 'Hello world!', @response.body
|
|
|
|
|
|
|
|
@request.accept = "text/csv"
|
2010-01-10 14:21:08 -05:00
|
|
|
get :using_resource_with_block
|
2009-07-29 05:13:08 -04:00
|
|
|
assert_equal "text/csv", @response.content_type
|
|
|
|
assert_equal "CSV", @response.body
|
|
|
|
|
|
|
|
@request.accept = "application/xml"
|
2010-01-10 14:21:08 -05:00
|
|
|
get :using_resource
|
2009-07-29 05:13:08 -04:00
|
|
|
assert_equal "application/xml", @response.content_type
|
2010-01-10 14:21:08 -05:00
|
|
|
assert_equal "<name>david</name>", @response.body
|
2009-07-29 05:13:08 -04:00
|
|
|
end
|
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def test_using_resource_with_overwrite_block
|
|
|
|
get :using_resource_with_overwrite_block
|
2009-07-31 14:56:53 -04:00
|
|
|
assert_equal "text/html", @response.content_type
|
|
|
|
assert_equal "HTML", @response.body
|
|
|
|
end
|
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def test_not_acceptable
|
2009-07-29 06:18:03 -04:00
|
|
|
@request.accept = "application/xml"
|
2011-07-27 18:37:06 -04:00
|
|
|
assert_raises(ActionController::UnknownFormat) do
|
|
|
|
get :using_resource_with_block
|
|
|
|
end
|
2009-07-29 06:18:03 -04:00
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
@request.accept = "text/javascript"
|
2011-07-27 18:37:06 -04:00
|
|
|
assert_raises(ActionController::UnknownFormat) do
|
|
|
|
get :using_resource_with_overwrite_block
|
|
|
|
end
|
2009-07-29 06:18:03 -04:00
|
|
|
end
|
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_post_with_html_redirects_on_success
|
2009-10-04 00:31:38 -04:00
|
|
|
with_test_route_set do
|
|
|
|
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?
|
2009-12-02 19:47:09 -05:00
|
|
|
end
|
|
|
|
end
|
2009-10-04 00:31:38 -04:00
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_post_with_html_rerender_on_failure
|
|
|
|
with_test_route_set do
|
2009-10-04 00:31:38 -04:00
|
|
|
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
|
2009-08-01 09:29:39 -04:00
|
|
|
end
|
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_post_with_xml_yields_created_on_success
|
2009-10-04 00:31:38 -04:00
|
|
|
with_test_route_set do
|
|
|
|
@request.accept = "application/xml"
|
|
|
|
post :using_resource
|
|
|
|
assert_equal "application/xml", @response.content_type
|
|
|
|
assert_equal 201, @response.status
|
|
|
|
assert_equal "<name>david</name>", @response.body
|
|
|
|
assert_equal "http://www.example.com/customers/13", @response.location
|
2009-12-02 19:47:09 -05:00
|
|
|
end
|
|
|
|
end
|
2009-10-04 00:31:38 -04:00
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_post_with_xml_yields_unprocessable_entity_on_failure
|
|
|
|
with_test_route_set do
|
|
|
|
@request.accept = "application/xml"
|
2009-10-04 00:31:38 -04:00
|
|
|
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
|
2009-07-31 05:59:05 -04:00
|
|
|
end
|
|
|
|
|
2011-10-10 01:42:12 -04:00
|
|
|
def test_using_resource_for_post_with_json_yields_unprocessable_entity_on_failure
|
|
|
|
with_test_route_set do
|
|
|
|
@request.accept = "application/json"
|
|
|
|
errors = { :name => :invalid }
|
|
|
|
Customer.any_instance.stubs(:errors).returns(errors)
|
|
|
|
post :using_resource
|
|
|
|
assert_equal "application/json", @response.content_type
|
|
|
|
assert_equal 422, @response.status
|
|
|
|
errors = {:errors => errors}
|
|
|
|
assert_equal errors.to_json, @response.body
|
|
|
|
assert_nil @response.location
|
2011-05-06 17:03:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_using_resource_for_patch_with_html_redirects_on_success
|
|
|
|
with_test_route_set do
|
|
|
|
patch :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?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_using_resource_for_patch_with_html_rerender_on_failure
|
|
|
|
with_test_route_set do
|
|
|
|
errors = { :name => :invalid }
|
|
|
|
Customer.any_instance.stubs(:errors).returns(errors)
|
|
|
|
patch :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
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_using_resource_for_patch_with_html_rerender_on_failure_even_on_method_override
|
|
|
|
with_test_route_set do
|
|
|
|
errors = { :name => :invalid }
|
|
|
|
Customer.any_instance.stubs(:errors).returns(errors)
|
|
|
|
@request.env["rack.methodoverride.original_method"] = "POST"
|
|
|
|
patch :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
|
2011-10-10 01:42:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_put_with_html_redirects_on_success
|
2009-10-04 00:31:38 -04:00
|
|
|
with_test_route_set do
|
|
|
|
put :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?
|
2009-12-02 19:47:09 -05:00
|
|
|
end
|
|
|
|
end
|
2009-10-04 00:31:38 -04:00
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_put_with_html_rerender_on_failure
|
|
|
|
with_test_route_set do
|
2009-10-04 00:31:38 -04:00
|
|
|
errors = { :name => :invalid }
|
|
|
|
Customer.any_instance.stubs(:errors).returns(errors)
|
|
|
|
put :using_resource
|
2013-07-05 08:34:39 -04:00
|
|
|
|
2009-10-04 00:31:38 -04:00
|
|
|
assert_equal "text/html", @response.content_type
|
|
|
|
assert_equal 200, @response.status
|
|
|
|
assert_equal "Edit world!\n", @response.body
|
|
|
|
assert_nil @response.location
|
|
|
|
end
|
2009-08-01 09:29:39 -04:00
|
|
|
end
|
|
|
|
|
2010-05-24 07:12:40 -04:00
|
|
|
def test_using_resource_for_put_with_html_rerender_on_failure_even_on_method_override
|
|
|
|
with_test_route_set do
|
|
|
|
errors = { :name => :invalid }
|
|
|
|
Customer.any_instance.stubs(:errors).returns(errors)
|
|
|
|
@request.env["rack.methodoverride.original_method"] = "POST"
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2011-10-26 03:31:56 -04:00
|
|
|
def test_using_resource_for_put_with_xml_yields_no_content_on_success
|
2009-07-31 05:59:05 -04:00
|
|
|
@request.accept = "application/xml"
|
|
|
|
put :using_resource
|
|
|
|
assert_equal "application/xml", @response.content_type
|
2011-10-26 03:31:56 -04:00
|
|
|
assert_equal 204, @response.status
|
2012-08-02 18:58:45 -04:00
|
|
|
assert_equal "", @response.body
|
2009-12-02 19:47:09 -05:00
|
|
|
end
|
2009-07-31 05:59:05 -04:00
|
|
|
|
2011-10-26 03:31:56 -04:00
|
|
|
def test_using_resource_for_put_with_json_yields_no_content_on_success
|
2010-10-11 13:37:03 -04:00
|
|
|
Customer.any_instance.stubs(:to_json).returns('{"name": "David"}')
|
|
|
|
@request.accept = "application/json"
|
|
|
|
put :using_resource
|
|
|
|
assert_equal "application/json", @response.content_type
|
2011-10-26 03:31:56 -04:00
|
|
|
assert_equal 204, @response.status
|
2012-08-02 18:58:45 -04:00
|
|
|
assert_equal "", @response.body
|
2010-10-11 13:37:03 -04:00
|
|
|
end
|
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_put_with_xml_yields_unprocessable_entity_on_failure
|
|
|
|
@request.accept = "application/xml"
|
2009-07-31 05:59:05 -04:00
|
|
|
errors = { :name => :invalid }
|
2009-08-01 10:47:44 -04:00
|
|
|
Customer.any_instance.stubs(:errors).returns(errors)
|
2009-07-31 05:59:05 -04:00
|
|
|
put :using_resource
|
|
|
|
assert_equal "application/xml", @response.content_type
|
|
|
|
assert_equal 422, @response.status
|
|
|
|
assert_equal errors.to_xml, @response.body
|
2009-08-01 09:29:39 -04:00
|
|
|
assert_nil @response.location
|
2009-07-31 05:59:05 -04:00
|
|
|
end
|
|
|
|
|
2011-10-10 01:42:12 -04:00
|
|
|
def test_using_resource_for_put_with_json_yields_unprocessable_entity_on_failure
|
|
|
|
@request.accept = "application/json"
|
|
|
|
errors = { :name => :invalid }
|
|
|
|
Customer.any_instance.stubs(:errors).returns(errors)
|
|
|
|
put :using_resource
|
|
|
|
assert_equal "application/json", @response.content_type
|
|
|
|
assert_equal 422, @response.status
|
|
|
|
errors = {:errors => errors}
|
|
|
|
assert_equal errors.to_json, @response.body
|
|
|
|
assert_nil @response.location
|
|
|
|
end
|
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_delete_with_html_redirects_on_success
|
2009-10-04 00:31:38 -04:00
|
|
|
with_test_route_set do
|
|
|
|
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
|
2009-08-01 09:29:39 -04:00
|
|
|
end
|
|
|
|
|
2011-10-26 03:31:56 -04:00
|
|
|
def test_using_resource_for_delete_with_xml_yields_no_content_on_success
|
2009-08-01 10:47:44 -04:00
|
|
|
Customer.any_instance.stubs(:destroyed?).returns(true)
|
2009-07-31 05:59:05 -04:00
|
|
|
@request.accept = "application/xml"
|
|
|
|
delete :using_resource
|
|
|
|
assert_equal "application/xml", @response.content_type
|
2011-10-26 03:31:56 -04:00
|
|
|
assert_equal 204, @response.status
|
2012-08-02 18:58:45 -04:00
|
|
|
assert_equal "", @response.body
|
2009-07-29 06:18:03 -04:00
|
|
|
end
|
|
|
|
|
2011-10-26 03:31:56 -04:00
|
|
|
def test_using_resource_for_delete_with_json_yields_no_content_on_success
|
2010-10-11 13:37:03 -04:00
|
|
|
Customer.any_instance.stubs(:to_json).returns('{"name": "David"}')
|
|
|
|
Customer.any_instance.stubs(:destroyed?).returns(true)
|
|
|
|
@request.accept = "application/json"
|
|
|
|
delete :using_resource
|
|
|
|
assert_equal "application/json", @response.content_type
|
2011-10-26 03:31:56 -04:00
|
|
|
assert_equal 204, @response.status
|
2012-08-02 18:58:45 -04:00
|
|
|
assert_equal "", @response.body
|
2010-10-11 13:37:03 -04:00
|
|
|
end
|
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_delete_with_html_redirects_on_failure
|
|
|
|
with_test_route_set do
|
|
|
|
errors = { :name => :invalid }
|
|
|
|
Customer.any_instance.stubs(:errors).returns(errors)
|
|
|
|
Customer.any_instance.stubs(:destroyed?).returns(false)
|
|
|
|
delete :using_resource
|
|
|
|
assert_equal "text/html", @response.content_type
|
|
|
|
assert_equal 302, @response.status
|
2010-02-21 05:09:21 -05:00
|
|
|
assert_equal "http://www.example.com/customers", @response.location
|
2009-12-02 19:47:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-01 09:29:39 -04:00
|
|
|
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
|
2009-08-13 04:27:41 -04:00
|
|
|
assert_equal "<name>david</name>", @response.body
|
2009-08-01 09:29:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_using_resource_with_parent_for_post
|
2009-10-04 00:31:38 -04:00
|
|
|
with_test_route_set do
|
|
|
|
@request.accept = "application/xml"
|
|
|
|
|
|
|
|
post :using_resource_with_parent
|
|
|
|
assert_equal "application/xml", @response.content_type
|
|
|
|
assert_equal 201, @response.status
|
|
|
|
assert_equal "<name>david</name>", @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
|
2009-07-29 08:32:56 -04:00
|
|
|
end
|
|
|
|
|
2009-08-13 04:27:41 -04:00
|
|
|
def test_using_resource_with_collection
|
|
|
|
@request.accept = "application/xml"
|
|
|
|
get :using_resource_with_collection
|
|
|
|
assert_equal "application/xml", @response.content_type
|
|
|
|
assert_equal 200, @response.status
|
2010-09-22 15:03:39 -04:00
|
|
|
assert_match(/<name>david<\/name>/, @response.body)
|
|
|
|
assert_match(/<name>jamis<\/name>/, @response.body)
|
2009-08-13 04:27:41 -04:00
|
|
|
end
|
|
|
|
|
2009-11-11 11:45:21 -05:00
|
|
|
def test_using_resource_with_action
|
|
|
|
@controller.instance_eval do
|
|
|
|
def render(params={})
|
|
|
|
self.response_body = "#{params[:action]} - #{formats}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
errors = { :name => :invalid }
|
|
|
|
Customer.any_instance.stubs(:errors).returns(errors)
|
|
|
|
|
|
|
|
post :using_resource_with_action
|
2010-03-19 21:04:00 -04:00
|
|
|
assert_equal "foo - #{[:html].to_s}", @controller.response.body
|
2009-11-11 11:45:21 -05:00
|
|
|
end
|
|
|
|
|
2009-12-01 10:11:24 -05:00
|
|
|
def test_respond_as_responder_entry_point
|
|
|
|
@request.accept = "text/html"
|
|
|
|
get :using_responder_with_respond
|
|
|
|
assert_equal "respond html", @response.body
|
|
|
|
|
|
|
|
@request.accept = "application/xml"
|
|
|
|
get :using_responder_with_respond
|
|
|
|
assert_equal "respond xml", @response.body
|
|
|
|
end
|
|
|
|
|
2009-07-29 08:32:56 -04:00
|
|
|
def test_clear_respond_to
|
|
|
|
@controller = InheritedRespondWithController.new
|
|
|
|
@request.accept = "text/html"
|
2011-07-27 18:37:06 -04:00
|
|
|
assert_raises(ActionController::UnknownFormat) do
|
|
|
|
get :index
|
|
|
|
end
|
2009-07-29 08:32:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_first_in_respond_to_has_higher_priority
|
|
|
|
@controller = InheritedRespondWithController.new
|
|
|
|
@request.accept = "*/*"
|
|
|
|
get :index
|
|
|
|
assert_equal "application/xml", @response.content_type
|
2009-08-13 04:27:41 -04:00
|
|
|
assert_equal "<name>david</name>", @response.body
|
2009-07-29 08:32:56 -04:00
|
|
|
end
|
|
|
|
|
2009-08-29 11:48:11 -04:00
|
|
|
def test_block_inside_respond_with_is_rendered
|
|
|
|
@controller = InheritedRespondWithController.new
|
|
|
|
@request.accept = "application/json"
|
|
|
|
get :index
|
|
|
|
assert_equal "JSON", @response.body
|
|
|
|
end
|
|
|
|
|
2010-11-15 17:52:39 -05:00
|
|
|
def test_render_json_object_responds_to_str_still_produce_json
|
|
|
|
@controller = RenderJsonRespondWithController.new
|
|
|
|
@request.accept = "application/json"
|
|
|
|
get :index, :format => :json
|
2010-11-28 06:48:50 -05:00
|
|
|
assert_match(/"message":"boom"/, @response.body)
|
|
|
|
assert_match(/"error":"RenderJsonTestException"/, @response.body)
|
2010-11-15 17:52:39 -05:00
|
|
|
end
|
|
|
|
|
2012-02-03 11:47:47 -05:00
|
|
|
def test_api_response_with_valid_resource_respect_override_block
|
|
|
|
@controller = RenderJsonRespondWithController.new
|
|
|
|
post :create, :name => "sikachu", :format => :json
|
|
|
|
assert_equal '{"valid":true}', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_api_response_with_invalid_resource_respect_override_block
|
|
|
|
@controller = RenderJsonRespondWithController.new
|
|
|
|
post :create, :name => "david", :format => :json
|
|
|
|
assert_equal '{"valid":false}', @response.body
|
|
|
|
end
|
|
|
|
|
2009-08-05 06:36:43 -04:00
|
|
|
def test_no_double_render_is_raised
|
|
|
|
@request.accept = "text/html"
|
|
|
|
assert_raise ActionView::MissingTemplate do
|
|
|
|
get :using_resource
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-06 17:48:48 -04:00
|
|
|
def test_using_resource_with_status_and_location
|
2009-08-05 06:36:43 -04:00
|
|
|
@request.accept = "text/html"
|
2009-08-06 17:48:48 -04:00
|
|
|
post :using_resource_with_status_and_location
|
2009-08-05 06:36:43 -04:00
|
|
|
assert @response.redirect?
|
|
|
|
assert_equal "http://test.host/", @response.location
|
2009-08-06 17:48:48 -04:00
|
|
|
|
|
|
|
@request.accept = "application/xml"
|
|
|
|
get :using_resource_with_status_and_location
|
|
|
|
assert_equal 201, @response.status
|
|
|
|
end
|
|
|
|
|
2011-03-30 14:53:42 -04:00
|
|
|
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
|
|
|
|
|
2011-03-31 12:18:11 -04:00
|
|
|
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
|
|
|
|
|
2011-03-31 12:25:29 -04:00
|
|
|
def test_using_options_with_template
|
|
|
|
@request.accept = "text/xml"
|
|
|
|
|
|
|
|
post :using_options_with_template
|
|
|
|
assert_equal "<customer-name>david</customer-name>", @response.body
|
|
|
|
assert_equal 123, @response.status
|
|
|
|
assert_equal "http://test.host/", @response.location
|
|
|
|
|
|
|
|
put :using_options_with_template
|
|
|
|
assert_equal "<customer-name>david</customer-name>", @response.body
|
|
|
|
assert_equal 123, @response.status
|
|
|
|
assert_equal "http://test.host/", @response.location
|
|
|
|
end
|
|
|
|
|
2009-08-08 05:07:07 -04:00
|
|
|
def test_using_resource_with_responder
|
|
|
|
get :using_resource_with_responder
|
2009-08-06 17:48:48 -04:00
|
|
|
assert_equal "Resource name is david", @response.body
|
2009-08-05 06:36:43 -04:00
|
|
|
end
|
|
|
|
|
2009-11-11 11:45:21 -05:00
|
|
|
def test_using_resource_with_set_responder
|
2009-10-17 11:54:03 -04:00
|
|
|
RespondWithController.responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
|
|
|
|
get :using_resource
|
|
|
|
assert_equal "Resource name is david", @response.body
|
|
|
|
ensure
|
|
|
|
RespondWithController.responder = ActionController::Responder
|
|
|
|
end
|
|
|
|
|
2013-08-17 21:45:50 -04:00
|
|
|
def test_uses_renderer_if_an_api_behavior
|
|
|
|
ActionController::Renderers.add :csv do |obj, options|
|
|
|
|
send_data obj.to_csv, type: Mime::CSV
|
|
|
|
end
|
|
|
|
@controller = CsvRespondWithController.new
|
|
|
|
get :index, format: 'csv'
|
|
|
|
assert_equal Mime::CSV, @response.content_type
|
|
|
|
assert_equal "c,s,v", @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_raises_missing_renderer_if_an_api_behavior_with_no_renderer
|
|
|
|
@controller = CsvRespondWithController.new
|
|
|
|
assert_raise ActionController::MissingRenderer do
|
|
|
|
get :index, format: 'csv'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def test_error_is_raised_if_no_respond_to_is_declared_and_respond_with_is_called
|
|
|
|
@controller = EmptyRespondWithController.new
|
|
|
|
@request.accept = "*/*"
|
|
|
|
assert_raise RuntimeError do
|
|
|
|
get :index
|
|
|
|
end
|
2009-07-29 05:13:08 -04:00
|
|
|
end
|
2009-10-04 00:31:38 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def with_test_route_set
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2009-12-08 17:52:26 -05:00
|
|
|
resources :customers
|
|
|
|
resources :quiz_stores do
|
|
|
|
resources :customers
|
|
|
|
end
|
2012-04-24 23:32:09 -04:00
|
|
|
get ":controller/:action"
|
2009-10-04 00:31:38 -04:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2009-07-28 15:29:29 -04:00
|
|
|
end
|
|
|
|
|
2012-03-05 09:26:45 -05:00
|
|
|
class FlashResponder < ActionController::Responder
|
|
|
|
def initialize(controller, resources, options={})
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_html
|
|
|
|
controller.flash[:notice] = 'Success'
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FlashResponderController < ActionController::Base
|
|
|
|
self.responder = FlashResponder
|
|
|
|
respond_to :html
|
|
|
|
|
|
|
|
def index
|
|
|
|
respond_with Object.new do |format|
|
|
|
|
format.html { render :text => 'HTML' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FlashResponderControllerTest < ActionController::TestCase
|
|
|
|
tests FlashResponderController
|
|
|
|
|
|
|
|
def test_respond_with_block_executed
|
|
|
|
get :index
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_flash_responder_executed
|
|
|
|
get :index
|
|
|
|
assert_equal 'Success', flash[:notice]
|
|
|
|
end
|
|
|
|
end
|