From b98e496c039b7bde4a2de9c02809e0b4ee0679ae Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Wed, 29 Apr 2009 17:32:55 -0700 Subject: Support implicit and explicit content types --- actionpack/test/controller/content_type_test.rb | 10 +++ actionpack/test/new_base/content_type_test.rb | 111 ++++++++++++++++++++++++ actionpack/test/new_base/test_helper.rb | 1 + 3 files changed, 122 insertions(+) create mode 100644 actionpack/test/new_base/content_type_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb index 7377546631..64b8b10d5b 100644 --- a/actionpack/test/controller/content_type_test.rb +++ b/actionpack/test/controller/content_type_test.rb @@ -1,24 +1,29 @@ require 'abstract_unit' class ContentTypeController < ActionController::Base + # :ported: def render_content_type_from_body response.content_type = Mime::RSS render :text => "hello world!" end + # :ported: def render_defaults render :text => "hello world!" end + # :ported: def render_content_type_from_render render :text => "hello world!", :content_type => Mime::RSS end + # :ported: def render_charset_from_body response.charset = "utf-16" render :text => "hello world!" end + # :ported: def render_nil_charset_from_body response.charset = nil render :text => "hello world!" @@ -60,6 +65,7 @@ class ContentTypeTest < ActionController::TestCase @controller.logger = Logger.new(nil) end + # :ported: def test_render_defaults get :render_defaults assert_equal "utf-8", @response.charset @@ -74,24 +80,28 @@ class ContentTypeTest < ActionController::TestCase ContentTypeController.default_charset = "utf-8" end + # :ported: def test_content_type_from_body get :render_content_type_from_body assert_equal "application/rss+xml", @response.content_type assert_equal "utf-8", @response.charset end + # :ported: def test_content_type_from_render get :render_content_type_from_render assert_equal "application/rss+xml", @response.content_type assert_equal "utf-8", @response.charset end + # :ported: def test_charset_from_body get :render_charset_from_body assert_equal Mime::HTML, @response.content_type assert_equal "utf-16", @response.charset end + # :ported: def test_nil_charset_from_body get :render_nil_charset_from_body assert_equal Mime::HTML, @response.content_type diff --git a/actionpack/test/new_base/content_type_test.rb b/actionpack/test/new_base/content_type_test.rb new file mode 100644 index 0000000000..10a28da496 --- /dev/null +++ b/actionpack/test/new_base/content_type_test.rb @@ -0,0 +1,111 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module ContentType + class BaseController < ActionController::Base2 + def index + render :text => "Hello world!" + end + + def set_on_response_obj + response.content_type = Mime::RSS + render :text => "Hello world!" + end + + def set_on_render + render :text => "Hello world!", :content_type => Mime::RSS + end + end + + class TestDefault < SimpleRouteCase + describe "a default response is HTML and UTF8" + + get "/content_type/base" + assert_body "Hello world!" + assert_header "Content-Type", "text/html; charset=utf-8" + end + + class TestSetOnResponseObj < SimpleRouteCase + describe "setting the content type of the response directly on the response object" + + get "/content_type/base/set_on_response_obj" + assert_body "Hello world!" + assert_header "Content-Type", "application/rss+xml; charset=utf-8" + end + + class TestSetOnRender < SimpleRouteCase + describe "setting the content type of the response as an option to render" + + get "/content_type/base/set_on_render" + assert_body "Hello world!" + assert_header "Content-Type", "application/rss+xml; charset=utf-8" + end + + class ImpliedController < ActionController::Base2 + self.view_paths = [ActionView::Template::FixturePath.new( + "content_type/implied/i_am_html_erb.html.erb" => "Hello world!", + "content_type/implied/i_am_xml_erb.xml.erb" => "Hello world!", + "content_type/implied/i_am_html_builder.html.builder" => "xml.p 'Hello'", + "content_type/implied/i_am_xml_builder.xml.builder" => "xml.awesome 'Hello'" + )] + + def i_am_html_erb() end + def i_am_xml_erb() end + def i_am_html_builder() end + def i_am_xml_builder() end + end + + class TestImpliedController < SimpleRouteCase + describe "the template's mime type is used if no content_type is specified" + + test "sets Content-Type as text/html when rendering *.html.erb" do + get "/content_type/implied/i_am_html_erb" + assert_header "Content-Type", "text/html; charset=utf-8" + end + + test "sets Content-Type as application/xml when rendering *.xml.erb" do + get "/content_type/implied/i_am_xml_erb" + assert_header "Content-Type", "application/xml; charset=utf-8" + end + + test "sets Content-Type as text/html when rendering *.html.builder" do + get "/content_type/implied/i_am_html_builder" + assert_header "Content-Type", "text/html; charset=utf-8" + end + + test "sets Content-Type as application/xml when rendering *.xml.builder" do + get "/content_type/implied/i_am_xml_builder" + assert_header "Content-Type", "application/xml; charset=utf-8" + end + + end +end + +module Charset + class BaseController < ActionController::Base2 + def set_on_response_obj + response.charset = "utf-16" + render :text => "Hello world!" + end + + def set_as_nil_on_response_obj + response.charset = nil + render :text => "Hello world!" + end + end + + class TestSetOnResponseObj < SimpleRouteCase + describe "setting the charset of the response directly on the response object" + + get "/charset/base/set_on_response_obj" + assert_body "Hello world!" + assert_header "Content-Type", "text/html; charset=utf-16" + end + + class TestSetAsNilOnResponseObj < SimpleRouteCase + describe "setting the charset of the response as nil directly on the response object" + + get "/charset/base/set_as_nil_on_response_obj" + assert_body "Hello world!" + assert_header "Content-Type", "text/html; charset=utf-8" + end +end \ No newline at end of file diff --git a/actionpack/test/new_base/test_helper.rb b/actionpack/test/new_base/test_helper.rb index 03af5a66a6..d41c7283b7 100644 --- a/actionpack/test/new_base/test_helper.rb +++ b/actionpack/test/new_base/test_helper.rb @@ -42,6 +42,7 @@ module ActionController use ActionController::UrlFor use ActionController::Renderer use ActionController::Layouts + use ActionController::ContentType def self.inherited(klass) ::ActionController::Base2.subclasses << klass.to_s -- cgit v1.2.3