2008-05-18 06:22:34 +02:00
|
|
|
require 'abstract_unit'
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
class RespondToController < ActionController::Base
|
|
|
|
layout :set_layout
|
|
|
|
|
|
|
|
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-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01: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
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def json_or_yaml
|
|
|
|
respond_to do |type|
|
|
|
|
type.json { render :text => "JSON" }
|
|
|
|
type.yaml { render :text => "YAML" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01: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-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
def forced_xml
|
|
|
|
request.format = :xml
|
|
|
|
|
|
|
|
respond_to do |type|
|
|
|
|
type.html { render :text => "HTML" }
|
|
|
|
type.xml { render :text => "XML" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def just_xml
|
|
|
|
respond_to do |type|
|
|
|
|
type.xml { render :text => "XML" }
|
|
|
|
end
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def using_defaults
|
|
|
|
respond_to do |type|
|
|
|
|
type.html
|
|
|
|
type.js
|
|
|
|
type.xml
|
|
|
|
end
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def using_defaults_with_type_list
|
|
|
|
respond_to(:html, :js, :xml)
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01: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
|
|
|
|
|
|
|
|
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-12-21 08:48:59 +01:00
|
|
|
|
2007-02-09 09:04:31 +01: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-12-21 08:48:59 +01:00
|
|
|
ensure
|
|
|
|
Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-02-09 09:04:31 +01: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-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
ensure
|
|
|
|
Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01: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-10-27 07:47:01 +01:00
|
|
|
|
2008-05-18 06:22:34 +02: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
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
def all_types_with_layout
|
|
|
|
respond_to do |type|
|
|
|
|
type.html
|
|
|
|
type.js
|
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def iphone_with_html_response_type
|
2007-12-21 08:48:59 +01:00
|
|
|
Mime::Type.register_alias("text/html", :iphone)
|
|
|
|
request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
respond_to do |type|
|
|
|
|
type.html { @type = "Firefox" }
|
|
|
|
type.iphone { @type = "iPhone" }
|
|
|
|
end
|
|
|
|
|
|
|
|
ensure
|
|
|
|
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
|
|
|
|
end
|
|
|
|
|
|
|
|
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-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01: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
|
|
|
|
|
|
|
|
ensure
|
|
|
|
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def rescue_action(e)
|
|
|
|
raise
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
protected
|
|
|
|
def set_layout
|
2007-12-21 08:48:59 +01:00
|
|
|
if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
|
|
|
|
"respond_to/layouts/standard"
|
|
|
|
elsif action_name == "iphone_with_html_response_type_without_layout"
|
|
|
|
"respond_to/layouts/missing"
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
class MimeControllerTest < ActionController::TestCase
|
|
|
|
tests RespondToController
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def setup
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.use_accept_header = true
|
2007-01-22 14:43:50 +01:00
|
|
|
@request.host = "www.example.com"
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def teardown
|
|
|
|
ActionController::Base.use_accept_header = false
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_html
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/html"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :js_or_html
|
|
|
|
assert_equal 'HTML', @response.body
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
get :html_or_xml
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
|
|
|
|
get :just_xml
|
|
|
|
assert_response 406
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_all
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "*/*"
|
2007-01-22 14:43:50 +01: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-10-27 07:47:01 +01:00
|
|
|
@request.accept = "application/xml"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :html_xml_or_rss
|
|
|
|
assert_equal 'XML', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_js_or_html
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/javascript, text/html"
|
2007-01-22 14:43:50 +01: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
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def test_json_or_yaml
|
|
|
|
get :json_or_yaml
|
|
|
|
assert_equal 'JSON', @response.body
|
|
|
|
|
|
|
|
get :json_or_yaml, :format => 'json'
|
|
|
|
assert_equal 'JSON', @response.body
|
|
|
|
|
|
|
|
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-10-27 07:47:01 +01:00
|
|
|
@request.accept = content_type
|
2007-02-09 09:04:31 +01:00
|
|
|
get :json_or_yaml
|
|
|
|
assert_equal body, @response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_js_or_anything
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/javascript, */*"
|
2007-01-22 14:43:50 +01: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-10-27 07:47:01 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_using_defaults
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "*/*"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :using_defaults
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "text/html", @response.content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal 'Hello world!', @response.body
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/javascript"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :using_defaults
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "text/javascript", @response.content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal '$("body").visualEffect("highlight");', @response.body
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "application/xml"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :using_defaults
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "application/xml", @response.content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal "<p>Hello world!</p>\n", @response.body
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_using_defaults_with_type_list
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "*/*"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :using_defaults_with_type_list
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "text/html", @response.content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal 'Hello world!', @response.body
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/javascript"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :using_defaults_with_type_list
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "text/javascript", @response.content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal '$("body").visualEffect("highlight");', @response.body
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "application/xml"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :using_defaults_with_type_list
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "application/xml", @response.content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal "<p>Hello world!</p>\n", @response.body
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
def test_with_atom_content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/atom+xml"
|
|
|
|
get :made_for_content_type
|
|
|
|
assert_equal "ATOM", @response.body
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_with_rss_content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/rss+xml"
|
|
|
|
get :made_for_content_type
|
|
|
|
assert_equal "RSS", @response.body
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_synonyms
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "application/javascript"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :js_or_html
|
|
|
|
assert_equal 'JS', @response.body
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "application/x-xml"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :html_xml_or_rss
|
|
|
|
assert_equal "XML", @response.body
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_custom_types
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "application/crazy-xml"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :custom_type_handling
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "application/crazy-xml", @response.content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal 'Crazy XML', @response.body
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/html"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :custom_type_handling
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "text/html", @response.content_type
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xhtml_alias
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "application/xhtml+xml,application/xml"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :html_or_xml
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_firefox_simulation
|
2008-10-27 07:47:01 +01: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"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :html_or_xml
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handle_any
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "*/*"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :handle_any
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/javascript"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :handle_any
|
|
|
|
assert_equal 'Either JS or XML', @response.body
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/xml"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :handle_any
|
|
|
|
assert_equal 'Either JS or XML', @response.body
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_handle_any_any
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "*/*"
|
2008-05-18 06:22:34 +02:00
|
|
|
get :handle_any_any
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_handle_any_any_parameter_format
|
|
|
|
get :handle_any_any, {:format=>'html'}
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_handle_any_any_explicit_html
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/html"
|
2008-05-18 06:22:34 +02:00
|
|
|
get :handle_any_any
|
|
|
|
assert_equal 'HTML', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handle_any_any_javascript
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/javascript"
|
2008-05-18 06:22:34 +02:00
|
|
|
get :handle_any_any
|
|
|
|
assert_equal 'Whatever you ask for, I got it', @response.body
|
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_handle_any_any_xml
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/xml"
|
2008-05-18 06:22:34 +02:00
|
|
|
get :handle_any_any
|
|
|
|
assert_equal 'Whatever you ask for, I got it', @response.body
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_rjs_type_skips_layout
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/javascript"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :all_types_with_layout
|
|
|
|
assert_equal 'RJS for all_types_with_layout', @response.body
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_html_type_with_layout
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/html"
|
2007-01-22 14:43:50 +01:00
|
|
|
get :all_types_with_layout
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
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-12-21 08:48:59 +01:00
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def test_custom_constant
|
|
|
|
get :custom_constant_handling, :format => "mobile"
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "text/x-mobile", @response.content_type
|
2007-02-09 09:04:31 +01:00
|
|
|
assert_equal "Mobile", @response.body
|
|
|
|
end
|
2007-12-21 08:48:59 +01: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
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-02-09 09:04:31 +01: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-12-21 08:48:59 +01: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
|
|
|
|
|
|
|
|
def test_extension_synonyms
|
|
|
|
get :html_xml_or_rss, :format => "xhtml"
|
|
|
|
assert_equal "HTML", @response.body
|
|
|
|
end
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def test_render_action_for_html
|
|
|
|
@controller.instance_eval do
|
|
|
|
def render(*args)
|
|
|
|
unless args.empty?
|
|
|
|
@action = args.first[:action]
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
response.body = "#{@action} - #{@template.template_format}"
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get :using_defaults
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "using_defaults - html", @response.body
|
2007-02-09 09:04:31 +01:00
|
|
|
|
|
|
|
get :using_defaults, :format => "xml"
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal "using_defaults - xml", @response.body
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_format_with_custom_response_type
|
|
|
|
get :iphone_with_html_response_type
|
2008-10-27 07:47:01 +01:00
|
|
|
assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
get :iphone_with_html_response_type, :format => "iphone"
|
|
|
|
assert_equal "text/html", @response.content_type
|
|
|
|
assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_format_with_custom_response_type_and_request_headers
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/iphone"
|
2007-12-21 08:48:59 +01:00
|
|
|
get :iphone_with_html_response_type
|
|
|
|
assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
|
|
|
|
assert_equal "text/html", @response.content_type
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01: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-10-27 07:47:01 +01:00
|
|
|
assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/iphone"
|
2009-03-16 15:55:30 +01:00
|
|
|
assert_raise(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
class AbstractPostController < ActionController::Base
|
2008-05-18 06:22:34 +02:00
|
|
|
self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# For testing layouts which are set automatically
|
|
|
|
class PostController < AbstractPostController
|
|
|
|
around_filter :with_iphone
|
|
|
|
|
|
|
|
def index
|
|
|
|
respond_to do |type|
|
|
|
|
type.html
|
|
|
|
type.iphone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
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
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
class SuperPostController < PostController
|
2007-12-21 08:48:59 +01:00
|
|
|
def index
|
|
|
|
respond_to do |type|
|
|
|
|
type.html
|
|
|
|
type.iphone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
class MimeControllerLayoutsTest < ActionController::TestCase
|
|
|
|
tests PostController
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def setup
|
2007-12-21 08:48:59 +01:00
|
|
|
@request.host = "www.example.com"
|
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_missing_layout_renders_properly
|
|
|
|
get :index
|
2008-10-27 07:47:01 +01:00
|
|
|
assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
@request.accept = "text/iphone"
|
2007-12-21 08:48:59 +01:00
|
|
|
get :index
|
|
|
|
assert_equal 'Hello iPhone', @response.body
|
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_format_with_inherited_layouts
|
|
|
|
@controller = SuperPostController.new
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
get :index
|
|
|
|
assert_equal 'Super Firefox', @response.body
|
2008-10-27 07:47:01 +01:00
|
|
|
|
|
|
|
@request.accept = "text/iphone"
|
2007-12-21 08:48:59 +01:00
|
|
|
get :index
|
|
|
|
assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|