2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2009-08-01 10:47:44 -04:00
|
|
|
require 'controller/fake_models'
|
2010-04-16 22:44:39 -04:00
|
|
|
require 'active_support/core_ext/hash/conversions'
|
2006-03-10 20:23:29 -05:00
|
|
|
|
2010-11-22 08:53:47 -05:00
|
|
|
class StarStarMimeController < ActionController::Base
|
|
|
|
layout nil
|
|
|
|
|
|
|
|
def index
|
|
|
|
render
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-10 20:23:29 -05:00
|
|
|
class RespondToController < ActionController::Base
|
2006-03-18 16:17:14 -05:00
|
|
|
layout :set_layout
|
|
|
|
|
2006-03-10 20:23:29 -05:00
|
|
|
def html_xml_or_rss
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.xml { render :text => "XML" }
|
|
|
|
type.rss { render :text => "RSS" }
|
|
|
|
type.all { render :text => "Nothing" }
|
|
|
|
end
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-10 20:23:29 -05:00
|
|
|
def js_or_html
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.js { render :text => "JS" }
|
|
|
|
type.all { render :text => "Nothing" }
|
|
|
|
end
|
|
|
|
end
|
2006-12-06 18:16:25 -05:00
|
|
|
|
2006-12-06 17:27:08 -05:00
|
|
|
def json_or_yaml
|
|
|
|
respond_to do |type|
|
|
|
|
type.json { render :text => "JSON" }
|
2006-12-06 18:16:25 -05:00
|
|
|
type.yaml { render :text => "YAML" }
|
2006-12-06 17:27:08 -05:00
|
|
|
end
|
|
|
|
end
|
2006-03-10 20:23:29 -05:00
|
|
|
|
|
|
|
def html_or_xml
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.xml { render :text => "XML" }
|
|
|
|
type.all { render :text => "Nothing" }
|
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-15 08:10:40 -04:00
|
|
|
def json_xml_or_html
|
|
|
|
respond_to do |type|
|
|
|
|
type.json { render :text => 'JSON' }
|
|
|
|
type.xml { render :xml => 'XML' }
|
|
|
|
type.html { render :text => 'HTML' }
|
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2007-09-15 00:18:32 -04:00
|
|
|
def forced_xml
|
|
|
|
request.format = :xml
|
|
|
|
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.xml { render :text => "XML" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-10 20:23:29 -05:00
|
|
|
def just_xml
|
|
|
|
respond_to do |type|
|
|
|
|
type.xml { render :text => "XML" }
|
|
|
|
end
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-11 22:39:58 -05:00
|
|
|
def using_defaults
|
|
|
|
respond_to do |type|
|
|
|
|
type.html
|
|
|
|
type.js
|
|
|
|
type.xml
|
|
|
|
end
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-18 01:43:37 -05:00
|
|
|
def using_defaults_with_type_list
|
|
|
|
respond_to(:html, :js, :xml)
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-12 00:13:55 -05:00
|
|
|
def made_for_content_type
|
|
|
|
respond_to do |type|
|
|
|
|
type.rss { render :text => "RSS" }
|
|
|
|
type.atom { render :text => "ATOM" }
|
|
|
|
type.all { render :text => "Nothing" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-12 01:02:44 -05:00
|
|
|
def custom_type_handling
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.custom("application/crazy-xml") { render :text => "Crazy XML" }
|
|
|
|
type.all { render :text => "Nothing" }
|
|
|
|
end
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-06-02 00:38:28 -04:00
|
|
|
|
2009-08-15 01:32:40 -04:00
|
|
|
def custom_constant_handling
|
2006-06-02 00:38:28 -04:00
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.mobile { render :text => "Mobile" }
|
|
|
|
end
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-08-29 20:34:36 -04:00
|
|
|
def custom_constant_handling_without_block
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.mobile
|
|
|
|
end
|
|
|
|
end
|
2006-03-12 01:02:44 -05:00
|
|
|
|
2006-03-15 13:27:26 -05:00
|
|
|
def handle_any
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.any(:js, :xml) { render :text => "Either JS or XML" }
|
|
|
|
end
|
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2008-03-07 06:17:05 -05:00
|
|
|
def handle_any_any
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => 'HTML' }
|
|
|
|
type.any { render :text => 'Whatever you ask for, I got it' }
|
|
|
|
end
|
|
|
|
end
|
2006-03-15 13:27:26 -05:00
|
|
|
|
2006-03-18 16:17:14 -05:00
|
|
|
def all_types_with_layout
|
|
|
|
respond_to do |type|
|
|
|
|
type.html
|
|
|
|
type.js
|
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
end
|
|
|
|
|
2009-08-15 01:32:40 -04:00
|
|
|
|
2008-06-25 16:24:12 -04:00
|
|
|
def iphone_with_html_response_type
|
2007-12-03 14:49:22 -05:00
|
|
|
request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2007-09-18 19:10:34 -04:00
|
|
|
respond_to do |type|
|
|
|
|
type.html { @type = "Firefox" }
|
2007-09-24 19:44:36 -04:00
|
|
|
type.iphone { @type = "iPhone" }
|
2007-09-18 19:10:34 -04:00
|
|
|
end
|
2006-03-18 16:17:14 -05:00
|
|
|
end
|
|
|
|
|
2007-09-25 12:37:43 -04:00
|
|
|
def iphone_with_html_response_type_without_layout
|
|
|
|
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2007-09-25 12:37:43 -04:00
|
|
|
respond_to do |type|
|
|
|
|
type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" }
|
|
|
|
type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-10 20:23:29 -05:00
|
|
|
def rescue_action(e)
|
2006-03-18 12:15:22 -05:00
|
|
|
raise
|
2006-03-10 20:23:29 -05:00
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-18 16:17:14 -05:00
|
|
|
protected
|
|
|
|
def set_layout
|
2007-09-18 19:10:34 -04:00
|
|
|
if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
|
2007-09-25 11:09:21 -04:00
|
|
|
"respond_to/layouts/standard"
|
2007-09-25 12:37:43 -04:00
|
|
|
elsif action_name == "iphone_with_html_response_type_without_layout"
|
|
|
|
"respond_to/layouts/missing"
|
2006-03-18 16:17:14 -05:00
|
|
|
end
|
|
|
|
end
|
2006-03-10 20:23:29 -05:00
|
|
|
end
|
|
|
|
|
2010-11-22 08:53:47 -05:00
|
|
|
class StarStarMimeControllerTest < ActionController::TestCase
|
|
|
|
tests StarStarMimeController
|
|
|
|
|
|
|
|
def test_javascript_with_format
|
|
|
|
@request.accept = "text/javascript"
|
|
|
|
get :index, :format => 'js'
|
|
|
|
assert_match "function addition(a,b){ return a+b; }", @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_javascript_with_no_format
|
|
|
|
@request.accept = "text/javascript"
|
|
|
|
get :index
|
|
|
|
assert_match "function addition(a,b){ return a+b; }", @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_javascript_with_no_format_only_star_star
|
|
|
|
@request.accept = "*/*"
|
|
|
|
get :index
|
|
|
|
assert_match "function addition(a,b){ return a+b; }", @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-07-29 05:13:08 -04:00
|
|
|
class RespondToControllerTest < ActionController::TestCase
|
2008-11-07 15:42:34 -05:00
|
|
|
tests RespondToController
|
|
|
|
|
2006-03-10 20:23:29 -05:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2006-03-10 20:23:29 -05:00
|
|
|
@request.host = "www.example.com"
|
2010-11-22 11:51:43 -05:00
|
|
|
Mime::Type.register_alias("text/html", :iphone)
|
|
|
|
Mime::Type.register("text/x-mobile", :mobile)
|
2006-03-10 20:23:29 -05:00
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2008-06-27 04:29:04 -04:00
|
|
|
def teardown
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2010-11-28 06:48:50 -05:00
|
|
|
Mime::Type.unregister(:iphone)
|
|
|
|
Mime::Type.unregister(:mobile)
|
2008-06-27 04:29:04 -04:00
|
|
|
end
|
|
|
|
|
2006-03-10 20:23:29 -05:00
|
|
|
def test_html
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/html"
|
2006-03-10 20:23:29 -05:00
|
|
|
get :js_or_html
|
|
|
|
assert_equal 'HTML', @response.body
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-10 20:23:29 -05:00
|
|
|
get :html_or_xml
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
|
|
|
|
get :just_xml
|
|
|
|
assert_response 406
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_all
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "*/*"
|
2006-03-10 20:23:29 -05:00
|
|
|
get :js_or_html
|
|
|
|
assert_equal 'HTML', @response.body # js is not part of all
|
|
|
|
|
|
|
|
get :html_or_xml
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
|
|
|
|
get :just_xml
|
|
|
|
assert_equal 'XML', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xml
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "application/xml"
|
2006-03-10 20:23:29 -05:00
|
|
|
get :html_xml_or_rss
|
|
|
|
assert_equal 'XML', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_js_or_html
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/javascript, text/html"
|
2009-08-15 01:32:40 -04:00
|
|
|
xhr :get, :js_or_html
|
2006-03-10 20:23:29 -05:00
|
|
|
assert_equal 'JS', @response.body
|
|
|
|
|
2009-08-15 01:32:40 -04:00
|
|
|
@request.accept = "text/javascript, text/html"
|
|
|
|
xhr :get, :html_or_xml
|
2006-03-10 20:23:29 -05:00
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
|
2009-08-15 01:32:40 -04:00
|
|
|
@request.accept = "text/javascript, text/html"
|
|
|
|
xhr :get, :just_xml
|
2006-03-10 20:23:29 -05:00
|
|
|
assert_response 406
|
|
|
|
end
|
2006-12-06 18:06:38 -05:00
|
|
|
|
2010-11-22 02:21:57 -05:00
|
|
|
def test_json_or_yaml_with_leading_star_star
|
|
|
|
@request.accept = "*/*, application/json"
|
|
|
|
get :json_xml_or_html
|
|
|
|
assert_equal 'HTML', @response.body
|
2010-11-22 08:58:01 -05:00
|
|
|
|
|
|
|
@request.accept = "*/* , application/json"
|
|
|
|
get :json_xml_or_html
|
|
|
|
assert_equal 'HTML', @response.body
|
2010-11-22 02:21:57 -05:00
|
|
|
end
|
|
|
|
|
2006-12-06 17:27:08 -05:00
|
|
|
def test_json_or_yaml
|
2009-08-15 01:32:40 -04:00
|
|
|
xhr :get, :json_or_yaml
|
2006-12-06 17:27:08 -05:00
|
|
|
assert_equal 'JSON', @response.body
|
2006-12-06 18:06:38 -05:00
|
|
|
|
|
|
|
get :json_or_yaml, :format => 'json'
|
2006-12-06 17:27:08 -05:00
|
|
|
assert_equal 'JSON', @response.body
|
2006-12-06 18:06:38 -05:00
|
|
|
|
|
|
|
get :json_or_yaml, :format => 'yaml'
|
|
|
|
assert_equal 'YAML', @response.body
|
|
|
|
|
|
|
|
{ 'YAML' => %w(text/yaml),
|
|
|
|
'JSON' => %w(application/json text/x-json)
|
|
|
|
}.each do |body, content_types|
|
|
|
|
content_types.each do |content_type|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = content_type
|
2006-12-06 18:06:38 -05:00
|
|
|
get :json_or_yaml
|
|
|
|
assert_equal body, @response.body
|
|
|
|
end
|
|
|
|
end
|
2006-12-06 17:27:08 -05:00
|
|
|
end
|
2006-03-10 20:23:29 -05:00
|
|
|
|
|
|
|
def test_js_or_anything
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/javascript, */*"
|
2009-08-15 01:32:40 -04:00
|
|
|
xhr :get, :js_or_html
|
2006-03-10 20:23:29 -05:00
|
|
|
assert_equal 'JS', @response.body
|
|
|
|
|
2009-08-15 01:32:40 -04:00
|
|
|
xhr :get, :html_or_xml
|
2006-03-10 20:23:29 -05:00
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
|
2009-08-15 01:32:40 -04:00
|
|
|
xhr :get, :just_xml
|
2006-03-10 20:23:29 -05:00
|
|
|
assert_equal 'XML', @response.body
|
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2006-03-11 22:39:58 -05:00
|
|
|
def test_using_defaults
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "*/*"
|
2006-03-11 22:39:58 -05:00
|
|
|
get :using_defaults
|
2007-05-25 23:11:34 -04:00
|
|
|
assert_equal "text/html", @response.content_type
|
2006-03-11 22:39:58 -05:00
|
|
|
assert_equal 'Hello world!', @response.body
|
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/javascript"
|
2006-03-11 22:39:58 -05:00
|
|
|
get :using_defaults
|
2007-05-25 23:11:34 -04:00
|
|
|
assert_equal "text/javascript", @response.content_type
|
2006-03-12 21:11:59 -05:00
|
|
|
assert_equal '$("body").visualEffect("highlight");', @response.body
|
2006-03-11 22:39:58 -05:00
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "application/xml"
|
2006-03-11 22:39:58 -05:00
|
|
|
get :using_defaults
|
2007-05-25 23:11:34 -04:00
|
|
|
assert_equal "application/xml", @response.content_type
|
2006-03-11 22:39:58 -05:00
|
|
|
assert_equal "<p>Hello world!</p>\n", @response.body
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-18 01:43:37 -05:00
|
|
|
def test_using_defaults_with_type_list
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "*/*"
|
2006-03-18 01:43:37 -05:00
|
|
|
get :using_defaults_with_type_list
|
2007-05-25 23:11:34 -04:00
|
|
|
assert_equal "text/html", @response.content_type
|
2006-03-18 01:43:37 -05:00
|
|
|
assert_equal 'Hello world!', @response.body
|
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/javascript"
|
2006-03-18 01:43:37 -05:00
|
|
|
get :using_defaults_with_type_list
|
2007-05-25 23:11:34 -04:00
|
|
|
assert_equal "text/javascript", @response.content_type
|
2006-03-18 01:43:37 -05:00
|
|
|
assert_equal '$("body").visualEffect("highlight");', @response.body
|
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "application/xml"
|
2006-03-18 01:43:37 -05:00
|
|
|
get :using_defaults_with_type_list
|
2007-05-25 23:11:34 -04:00
|
|
|
assert_equal "application/xml", @response.content_type
|
2006-03-18 01:43:37 -05:00
|
|
|
assert_equal "<p>Hello world!</p>\n", @response.body
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2007-05-18 02:24:50 -04:00
|
|
|
def test_with_atom_content_type
|
2009-08-15 01:32:40 -04:00
|
|
|
@request.accept = ""
|
2006-03-12 00:13:55 -05:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/atom+xml"
|
2009-08-15 01:32:40 -04:00
|
|
|
xhr :get, :made_for_content_type
|
2006-03-12 00:13:55 -05:00
|
|
|
assert_equal "ATOM", @response.body
|
2007-05-18 02:24:50 -04:00
|
|
|
end
|
2006-03-12 00:13:55 -05:00
|
|
|
|
2007-05-18 02:24:50 -04:00
|
|
|
def test_with_rss_content_type
|
2009-08-15 01:32:40 -04:00
|
|
|
@request.accept = ""
|
2006-03-12 00:13:55 -05:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/rss+xml"
|
2009-08-15 01:32:40 -04:00
|
|
|
xhr :get, :made_for_content_type
|
2006-03-12 00:13:55 -05:00
|
|
|
assert_equal "RSS", @response.body
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-12 01:02:44 -05:00
|
|
|
def test_synonyms
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "application/javascript"
|
2006-03-12 01:02:44 -05:00
|
|
|
get :js_or_html
|
|
|
|
assert_equal 'JS', @response.body
|
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "application/x-xml"
|
2006-03-18 16:17:14 -05:00
|
|
|
get :html_xml_or_rss
|
|
|
|
assert_equal "XML", @response.body
|
2006-03-12 01:02:44 -05:00
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-12 01:02:44 -05:00
|
|
|
def test_custom_types
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "application/crazy-xml"
|
2006-03-12 01:02:44 -05:00
|
|
|
get :custom_type_handling
|
2007-05-25 23:11:34 -04:00
|
|
|
assert_equal "application/crazy-xml", @response.content_type
|
2006-03-12 01:02:44 -05:00
|
|
|
assert_equal 'Crazy XML', @response.body
|
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/html"
|
2006-03-12 01:02:44 -05:00
|
|
|
get :custom_type_handling
|
2007-05-25 23:11:34 -04:00
|
|
|
assert_equal "text/html", @response.content_type
|
2006-03-12 01:02:44 -05:00
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
2006-03-12 13:50:14 -05:00
|
|
|
|
|
|
|
def test_xhtml_alias
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "application/xhtml+xml,application/xml"
|
2006-03-12 13:50:14 -05:00
|
|
|
get :html_or_xml
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-03-12 13:50:14 -05:00
|
|
|
def test_firefox_simulation
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
|
2006-03-12 13:50:14 -05:00
|
|
|
get :html_or_xml
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
2006-03-15 13:27:26 -05:00
|
|
|
|
|
|
|
def test_handle_any
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "*/*"
|
2006-03-15 13:27:26 -05:00
|
|
|
get :handle_any
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/javascript"
|
2006-03-15 13:27:26 -05:00
|
|
|
get :handle_any
|
|
|
|
assert_equal 'Either JS or XML', @response.body
|
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/xml"
|
2006-03-15 13:27:26 -05:00
|
|
|
get :handle_any
|
|
|
|
assert_equal 'Either JS or XML', @response.body
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2008-03-07 06:17:05 -05:00
|
|
|
def test_handle_any_any
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "*/*"
|
2008-03-07 06:17:05 -05:00
|
|
|
get :handle_any_any
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2008-03-07 06:17:05 -05:00
|
|
|
def test_handle_any_any_parameter_format
|
|
|
|
get :handle_any_any, {:format=>'html'}
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2008-03-07 06:17:05 -05:00
|
|
|
def test_handle_any_any_explicit_html
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/html"
|
2008-03-07 06:17:05 -05:00
|
|
|
get :handle_any_any
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handle_any_any_javascript
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/javascript"
|
2008-03-07 06:17:05 -05:00
|
|
|
get :handle_any_any
|
|
|
|
assert_equal 'Whatever you ask for, I got it', @response.body
|
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2008-03-07 06:17:05 -05:00
|
|
|
def test_handle_any_any_xml
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/xml"
|
2008-03-07 06:17:05 -05:00
|
|
|
get :handle_any_any
|
|
|
|
assert_equal 'Whatever you ask for, I got it', @response.body
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-15 08:10:40 -04:00
|
|
|
def test_browser_check_with_any_any
|
|
|
|
@request.accept = "application/json, application/xml"
|
|
|
|
get :json_xml_or_html
|
|
|
|
assert_equal 'JSON', @response.body
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-15 08:10:40 -04:00
|
|
|
@request.accept = "application/json, application/xml, */*"
|
|
|
|
get :json_xml_or_html
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-03-07 06:17:05 -05:00
|
|
|
|
2007-04-12 16:25:32 -04:00
|
|
|
def test_rjs_type_skips_layout
|
2009-06-17 15:36:15 -04:00
|
|
|
@request.accept = "text/javascript"
|
|
|
|
get :all_types_with_layout
|
|
|
|
assert_equal 'RJS for all_types_with_layout', @response.body
|
2007-04-12 16:25:32 -04:00
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2007-04-12 16:25:32 -04:00
|
|
|
def test_html_type_with_layout
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/html"
|
2006-03-18 16:17:14 -05:00
|
|
|
get :all_types_with_layout
|
2007-09-25 11:09:21 -04:00
|
|
|
assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
|
2006-03-18 16:17:14 -05:00
|
|
|
end
|
2006-03-18 16:23:14 -05:00
|
|
|
|
|
|
|
def test_xhr
|
|
|
|
xhr :get, :js_or_html
|
|
|
|
assert_equal 'JS', @response.body
|
|
|
|
|
|
|
|
xhr :get, :using_defaults
|
|
|
|
assert_equal '$("body").visualEffect("highlight");', @response.body
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-06-02 00:38:28 -04:00
|
|
|
def test_custom_constant
|
|
|
|
get :custom_constant_handling, :format => "mobile"
|
2007-05-25 23:11:34 -04:00
|
|
|
assert_equal "text/x-mobile", @response.content_type
|
2006-06-02 00:38:28 -04:00
|
|
|
assert_equal "Mobile", @response.body
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
|
|
|
def test_custom_constant_handling_without_block
|
|
|
|
get :custom_constant_handling_without_block, :format => "mobile"
|
|
|
|
assert_equal "text/x-mobile", @response.content_type
|
|
|
|
assert_equal "Mobile", @response.body
|
2006-08-29 20:34:36 -04:00
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-05-31 14:52:24 -04:00
|
|
|
def test_forced_format
|
|
|
|
get :html_xml_or_rss
|
|
|
|
assert_equal "HTML", @response.body
|
|
|
|
|
|
|
|
get :html_xml_or_rss, :format => "html"
|
|
|
|
assert_equal "HTML", @response.body
|
|
|
|
|
|
|
|
get :html_xml_or_rss, :format => "xml"
|
|
|
|
assert_equal "XML", @response.body
|
|
|
|
|
|
|
|
get :html_xml_or_rss, :format => "rss"
|
|
|
|
assert_equal "RSS", @response.body
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2007-09-15 00:18:32 -04:00
|
|
|
def test_internally_forced_format
|
|
|
|
get :forced_xml
|
|
|
|
assert_equal "XML", @response.body
|
|
|
|
|
|
|
|
get :forced_xml, :format => "html"
|
|
|
|
assert_equal "XML", @response.body
|
|
|
|
end
|
|
|
|
|
2006-12-02 17:48:20 -05:00
|
|
|
def test_extension_synonyms
|
|
|
|
get :html_xml_or_rss, :format => "xhtml"
|
|
|
|
assert_equal "HTML", @response.body
|
|
|
|
end
|
2006-10-08 16:30:24 -04:00
|
|
|
|
|
|
|
def test_render_action_for_html
|
|
|
|
@controller.instance_eval do
|
|
|
|
def render(*args)
|
2009-07-28 09:49:59 -04:00
|
|
|
@action = args.first[:action] unless args.empty?
|
|
|
|
@action ||= action_name
|
|
|
|
|
2009-07-29 13:31:03 -04:00
|
|
|
response.body = "#{@action} - #{formats}"
|
2006-10-08 16:30:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get :using_defaults
|
2009-04-13 19:56:04 -04:00
|
|
|
assert_equal "using_defaults - #{[:html].to_s}", @response.body
|
2006-10-08 16:30:24 -04:00
|
|
|
|
|
|
|
get :using_defaults, :format => "xml"
|
2009-04-13 19:56:04 -04:00
|
|
|
assert_equal "using_defaults - #{[:xml].to_s}", @response.body
|
2008-06-25 16:24:12 -04:00
|
|
|
end
|
|
|
|
|
2007-09-18 19:10:34 -04:00
|
|
|
def test_format_with_custom_response_type
|
|
|
|
get :iphone_with_html_response_type
|
2008-06-25 16:24:12 -04:00
|
|
|
assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body
|
|
|
|
|
2007-09-18 19:10:34 -04:00
|
|
|
get :iphone_with_html_response_type, :format => "iphone"
|
|
|
|
assert_equal "text/html", @response.content_type
|
2007-09-25 11:09:21 -04:00
|
|
|
assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
|
2008-06-25 16:24:12 -04:00
|
|
|
end
|
|
|
|
|
2007-09-18 19:10:34 -04:00
|
|
|
def test_format_with_custom_response_type_and_request_headers
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/iphone"
|
2007-09-18 19:10:34 -04:00
|
|
|
get :iphone_with_html_response_type
|
2007-09-25 11:09:21 -04:00
|
|
|
assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
|
2007-09-18 19:10:34 -04:00
|
|
|
assert_equal "text/html", @response.content_type
|
2008-06-25 16:24:12 -04:00
|
|
|
end
|
2006-03-18 16:23:14 -05:00
|
|
|
end
|
2007-09-27 21:23:20 -04:00
|
|
|
|
2009-07-29 05:13:08 -04:00
|
|
|
class RespondWithController < ActionController::Base
|
2009-07-29 06:18:03 -04:00
|
|
|
respond_to :html, :json
|
2010-01-10 14:21:08 -05:00
|
|
|
respond_to :xml, :except => :using_resource_with_block
|
|
|
|
respond_to :js, :only => [ :using_resource_with_block, :using_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
|
|
|
|
|
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
|
|
|
|
|
2009-07-29 06:18:03 -04:00
|
|
|
protected
|
|
|
|
|
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 06:18:03 -04:00
|
|
|
def _render_js(js, options)
|
|
|
|
self.content_type ||= Mime::JS
|
|
|
|
self.response_body = js.respond_to?(:to_js) ? js.to_js : js
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
Mime::Type.unregister(:mobile)
|
2009-07-29 05:13:08 -04:00
|
|
|
end
|
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
def test_using_resource
|
|
|
|
@request.accept = "text/javascript"
|
|
|
|
get :using_resource
|
|
|
|
assert_equal "text/javascript", @response.content_type
|
|
|
|
assert_equal '$("body").visualEffect("highlight");', @response.body
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
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
|
|
|
|
assert_equal %Q[{"result":["david",13]}], @response.body
|
|
|
|
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"
|
2010-01-10 14:21:08 -05:00
|
|
|
get :using_resource_with_block
|
|
|
|
assert_equal 406, @response.status
|
2009-07-29 06:18:03 -04:00
|
|
|
|
2010-01-10 14:21:08 -05:00
|
|
|
@request.accept = "text/javascript"
|
|
|
|
get :using_resource_with_overwrite_block
|
|
|
|
assert_equal 406, @response.status
|
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
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_put_with_xml_yields_ok_on_success
|
2009-07-31 05:59:05 -04:00
|
|
|
@request.accept = "application/xml"
|
|
|
|
put :using_resource
|
|
|
|
assert_equal "application/xml", @response.content_type
|
|
|
|
assert_equal 200, @response.status
|
|
|
|
assert_equal " ", @response.body
|
2009-12-02 19:47:09 -05:00
|
|
|
end
|
2009-07-31 05:59:05 -04:00
|
|
|
|
2010-10-11 13:37:03 -04:00
|
|
|
def test_using_resource_for_put_with_json_yields_ok_on_success
|
|
|
|
Customer.any_instance.stubs(:to_json).returns('{"name": "David"}')
|
|
|
|
@request.accept = "application/json"
|
|
|
|
put :using_resource
|
|
|
|
assert_equal "application/json", @response.content_type
|
|
|
|
assert_equal 200, @response.status
|
|
|
|
assert_equal "{}", @response.body
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2009-12-02 19:47:09 -05:00
|
|
|
def test_using_resource_for_delete_with_xml_yields_ok_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
|
|
|
|
assert_equal 200, @response.status
|
|
|
|
assert_equal " ", @response.body
|
2009-07-29 06:18:03 -04:00
|
|
|
end
|
|
|
|
|
2010-10-11 13:37:03 -04:00
|
|
|
def test_using_resource_for_delete_with_json_yields_ok_on_success
|
|
|
|
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
|
|
|
|
assert_equal 200, @response.status
|
|
|
|
assert_equal "{}", @response.body
|
|
|
|
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"
|
|
|
|
get :index
|
|
|
|
assert_equal 406, @response.status
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
match ":controller/:action"
|
2009-10-04 00:31:38 -04:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2009-07-28 15:29:29 -04:00
|
|
|
end
|
|
|
|
|
2007-09-27 21:23:20 -04:00
|
|
|
class AbstractPostController < ActionController::Base
|
2008-01-21 15:45:04 -05:00
|
|
|
self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
|
2007-09-27 21:23:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# For testing layouts which are set automatically
|
|
|
|
class PostController < AbstractPostController
|
|
|
|
around_filter :with_iphone
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2007-09-27 21:23:20 -04:00
|
|
|
def index
|
2009-07-31 05:59:05 -04:00
|
|
|
respond_to(:html, :iphone)
|
2007-09-27 21:23:20 -04:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2009-05-20 18:33:08 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def with_iphone
|
|
|
|
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
|
|
|
|
yield
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|
2007-09-27 21:23:20 -04:00
|
|
|
|
2008-06-25 16:24:12 -04:00
|
|
|
class SuperPostController < PostController
|
2007-09-27 21:23:20 -04:00
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class MimeControllerLayoutsTest < ActionController::TestCase
|
|
|
|
tests PostController
|
2007-09-27 21:23:20 -04:00
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2007-09-27 21:23:20 -04:00
|
|
|
@request.host = "www.example.com"
|
2010-11-22 11:51:43 -05:00
|
|
|
Mime::Type.register_alias("text/html", :iphone)
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
2010-11-28 06:48:50 -05:00
|
|
|
Mime::Type.unregister(:iphone)
|
2007-09-27 21:23:20 -04:00
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2007-09-27 21:23:20 -04:00
|
|
|
def test_missing_layout_renders_properly
|
|
|
|
get :index
|
2008-06-25 16:24:12 -04:00
|
|
|
assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
|
2007-09-27 21:23:20 -04:00
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/iphone"
|
2007-09-27 21:23:20 -04:00
|
|
|
get :index
|
|
|
|
assert_equal 'Hello iPhone', @response.body
|
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2009-06-17 19:51:51 -04:00
|
|
|
def test_format_with_inherited_layouts
|
|
|
|
@controller = SuperPostController.new
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2009-06-17 19:51:51 -04:00
|
|
|
get :index
|
|
|
|
assert_equal '<html><div id="html">Super Firefox</div></html>', @response.body
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2009-06-17 19:51:51 -04:00
|
|
|
@request.accept = "text/iphone"
|
|
|
|
get :index
|
|
|
|
assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
|
2007-09-27 21:23:20 -04:00
|
|
|
end
|
|
|
|
end
|