2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
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
|
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
|
|
|
def custom_constant_handling
|
|
|
|
Mime::Type.register("text/x-mobile", :mobile)
|
|
|
|
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.mobile { render :text => "Mobile" }
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
ensure
|
|
|
|
Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
|
2006-06-02 00:38:28 -04:00
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2006-08-29 20:34:36 -04:00
|
|
|
def custom_constant_handling_without_block
|
|
|
|
Mime::Type.register("text/x-mobile", :mobile)
|
|
|
|
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.mobile
|
|
|
|
end
|
2007-05-25 23:11:34 -04:00
|
|
|
|
2007-10-02 01:32:14 -04:00
|
|
|
ensure
|
|
|
|
Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
|
2006-08-29 20:34:36 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
def iphone_with_html_response_type
|
2007-09-24 19:44:36 -04:00
|
|
|
Mime::Type.register_alias("text/html", :iphone)
|
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
|
|
|
|
|
2007-10-02 01:32:14 -04:00
|
|
|
ensure
|
|
|
|
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
|
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
|
|
|
|
Mime::Type.register_alias("text/html", :iphone)
|
|
|
|
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
|
|
|
|
|
2007-10-02 01:32:14 -04:00
|
|
|
ensure
|
|
|
|
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
|
2007-09-25 12:37:43 -04:00
|
|
|
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
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class MimeControllerTest < ActionController::TestCase
|
|
|
|
tests RespondToController
|
|
|
|
|
2006-03-10 20:23:29 -05:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2008-06-27 04:29:04 -04:00
|
|
|
ActionController::Base.use_accept_header = true
|
2006-03-10 20:23:29 -05:00
|
|
|
@request.host = "www.example.com"
|
|
|
|
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
|
2008-06-27 04:29:04 -04:00
|
|
|
ActionController::Base.use_accept_header = false
|
|
|
|
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"
|
2006-03-10 20:23:29 -05:00
|
|
|
get :js_or_html
|
|
|
|
assert_equal 'JS', @response.body
|
|
|
|
|
|
|
|
get :html_or_xml
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
|
|
|
|
get :just_xml
|
|
|
|
assert_response 406
|
|
|
|
end
|
2006-12-06 18:06:38 -05:00
|
|
|
|
2006-12-06 17:27:08 -05:00
|
|
|
def test_json_or_yaml
|
|
|
|
get :json_or_yaml
|
|
|
|
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, */*"
|
2006-03-10 20:23:29 -05:00
|
|
|
get :js_or_html
|
|
|
|
assert_equal 'JS', @response.body
|
|
|
|
|
|
|
|
get :html_or_xml
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
|
|
|
|
get :just_xml
|
|
|
|
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
|
2006-03-12 00:13:55 -05:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/atom+xml"
|
|
|
|
get :made_for_content_type
|
|
|
|
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
|
2006-03-12 00:13:55 -05:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/rss+xml"
|
|
|
|
get :made_for_content_type
|
|
|
|
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
|
|
|
|
|
2007-04-12 16:25:32 -04:00
|
|
|
def test_rjs_type_skips_layout
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/javascript"
|
2006-03-18 16:17:14 -05:00
|
|
|
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)
|
|
|
|
unless args.empty?
|
|
|
|
@action = args.first[:action]
|
|
|
|
end
|
2009-01-22 17:18:10 -05:00
|
|
|
response.body = "#{@action} - #{@template.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
|
2007-09-25 12:37:43 -04:00
|
|
|
|
|
|
|
def test_format_with_custom_response_type_and_request_headers_with_only_one_layout_present
|
|
|
|
get :iphone_with_html_response_type_without_layout
|
2008-06-25 16:24:12 -04:00
|
|
|
assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body
|
2007-09-25 12:37:43 -04:00
|
|
|
|
2008-08-08 05:31:12 -04:00
|
|
|
@request.accept = "text/iphone"
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
|
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
|
|
|
|
|
|
|
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
|
|
|
|
respond_to do |type|
|
|
|
|
type.html
|
|
|
|
type.iphone
|
|
|
|
end
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2007-09-27 21:23:20 -04:00
|
|
|
protected
|
2007-10-02 01:32:14 -04:00
|
|
|
def with_iphone
|
|
|
|
Mime::Type.register_alias("text/html", :iphone)
|
|
|
|
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
|
|
|
|
end
|
|
|
|
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
|
|
|
def index
|
|
|
|
respond_to do |type|
|
|
|
|
type.html
|
|
|
|
type.iphone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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"
|
|
|
|
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
|
|
|
|
2007-09-27 21:23:20 -04:00
|
|
|
def test_format_with_inherited_layouts
|
|
|
|
@controller = SuperPostController.new
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2007-09-27 21:23:20 -04:00
|
|
|
get :index
|
|
|
|
assert_equal 'Super Firefox', @response.body
|
2008-06-25 16:24:12 -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 '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
|
|
|
|
end
|
|
|
|
end
|