2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "controller/fake_models"
|
|
|
|
require "pathname"
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
class RenderXmlTest < ActionController::TestCase
|
2010-04-22 06:12:38 -04:00
|
|
|
class XmlRenderable
|
|
|
|
def to_xml(options)
|
|
|
|
options[:root] ||= "i-am-xml"
|
|
|
|
"<#{options[:root]}/>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
protect_from_forgery
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def self.controller_path
|
2016-08-06 12:54:50 -04:00
|
|
|
"test"
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_with_location
|
2016-08-06 13:35:13 -04:00
|
|
|
render xml: "<hello/>", location: "http://example.com", status: 201
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_with_object_location
|
|
|
|
customer = Customer.new("Some guy", 1)
|
2016-08-06 13:35:13 -04:00
|
|
|
render xml: "<customer/>", location: customer, status: :created
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_with_to_xml
|
2016-08-06 13:35:13 -04:00
|
|
|
render xml: XmlRenderable.new
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def formatted_xml_erb
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_xml_with_custom_content_type
|
2016-08-06 13:35:13 -04:00
|
|
|
render xml: "<blah/>", content_type: "application/atomsvc+xml"
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2010-04-22 06:12:38 -04:00
|
|
|
|
|
|
|
def render_xml_with_custom_options
|
2016-08-06 13:35:13 -04:00
|
|
|
render xml: XmlRenderable.new, root: "i-am-THE-xml"
|
2010-04-22 06:12:38 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
tests TestController
|
|
|
|
|
|
|
|
def setup
|
|
|
|
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
|
|
|
# a more accurate simulation of what happens in "real life".
|
|
|
|
super
|
2012-01-04 13:52:24 -05:00
|
|
|
@controller.logger = ActiveSupport::Logger.new(nil)
|
2009-05-13 20:00:36 -04:00
|
|
|
|
|
|
|
@request.host = "www.nextangle.com"
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
|
|
|
|
2009-05-13 20:00:36 -04:00
|
|
|
def test_rendering_with_location_should_set_header
|
|
|
|
get :render_with_location
|
|
|
|
assert_equal "http://example.com", @response.headers["Location"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rendering_xml_should_call_to_xml_if_possible
|
|
|
|
get :render_with_to_xml
|
|
|
|
assert_equal "<i-am-xml/>", @response.body
|
|
|
|
end
|
2009-09-19 14:04:12 -04:00
|
|
|
|
2010-04-22 06:12:38 -04:00
|
|
|
def test_rendering_xml_should_call_to_xml_with_extra_options
|
2010-04-24 05:48:47 -04:00
|
|
|
get :render_xml_with_custom_options
|
|
|
|
assert_equal "<i-am-THE-xml/>", @response.body
|
2010-04-22 06:12:38 -04:00
|
|
|
end
|
|
|
|
|
2009-05-13 20:00:36 -04:00
|
|
|
def test_rendering_with_object_location_should_set_header_with_url_for
|
2009-08-16 22:14:26 -04:00
|
|
|
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
|
2016-03-01 03:48:53 -05:00
|
|
|
|
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 12:54:50 -04:00
|
|
|
get ":controller/:action"
|
2016-03-01 03:48:53 -05:00
|
|
|
end
|
2009-08-16 22:14:26 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-08-16 22:14:26 -04:00
|
|
|
get :render_with_object_location
|
|
|
|
assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"]
|
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
end
|
2009-09-19 14:04:12 -04:00
|
|
|
|
2009-05-13 20:00:36 -04:00
|
|
|
def test_should_render_formatted_xml_erb_template
|
2015-01-04 04:35:06 -05:00
|
|
|
get :formatted_xml_erb, format: :xml
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "<test>passed formatted xml erb</test>", @response.body
|
2009-05-13 20:00:36 -04:00
|
|
|
end
|
2009-09-19 14:04:12 -04:00
|
|
|
|
2009-05-13 20:00:36 -04:00
|
|
|
def test_should_render_xml_but_keep_custom_content_type
|
|
|
|
get :render_xml_with_custom_content_type
|
|
|
|
assert_equal "application/atomsvc+xml", @response.content_type
|
|
|
|
end
|
2009-09-19 14:04:12 -04:00
|
|
|
|
2009-05-13 20:00:36 -04:00
|
|
|
def test_should_use_implicit_content_type
|
2016-08-06 12:54:50 -04:00
|
|
|
get :implicit_content_type, format: "atom"
|
2015-10-05 01:14:04 -04:00
|
|
|
assert_equal Mime[:atom], @response.content_type
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2009-08-01 10:47:44 -04:00
|
|
|
end
|