From a2ca04bb3ac178bbe1503ac65dc88f5f3f8cb37f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Strza=C5=82kowski?= Date: Sun, 11 Aug 2013 23:19:22 +0200 Subject: Extend basic rendering, test it in railties --- .../lib/action_controller/metal/rendering.rb | 19 +++++-- actionpack/test/controller/basic_rendering_test.rb | 19 ------- railties/test/application/basic_rendering_test.rb | 62 ++++++++++++++++++++++ railties/test/isolation/abstract_unit.rb | 6 +++ 4 files changed, 84 insertions(+), 22 deletions(-) delete mode 100644 actionpack/test/controller/basic_rendering_test.rb create mode 100644 railties/test/application/basic_rendering_test.rb diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb index 2d58e1440c..d965cb8c16 100644 --- a/actionpack/lib/action_controller/metal/rendering.rb +++ b/actionpack/lib/action_controller/metal/rendering.rb @@ -6,15 +6,28 @@ module ActionController # :api: public def render(*args, &block) super(*args, &block) - text = args.first[:text] - if text.present? - self.response_body = text + opts = args.first + if (opts.keys & [:text, :nothing]).present? + self.response_body = if opts.has_key?(:text) && opts[:text].present? + opts[:text] + elsif opts.has_key?(:nothing) && opts[:nothing] + " " + end + else + raise UnsupportedOperationError end end def rendered_format Mime::TEXT end + + class UnsupportedOperationError < StandardError + def initialize + super "Unsupported render operation. BasicRendering supports only :text + and :nothing options. For more, you need to include ActionView." + end + end end module Rendering diff --git a/actionpack/test/controller/basic_rendering_test.rb b/actionpack/test/controller/basic_rendering_test.rb deleted file mode 100644 index 2db3cff395..0000000000 --- a/actionpack/test/controller/basic_rendering_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'abstract_unit' - -class BasicRenderingController < ActionController::Base - def render_hello_world - render text: "Hello World!" - end -end - -class BasicRenderingTest < ActionController::TestCase - tests BasicRenderingController - - def test_render_hello_world - get :render_hello_world - - assert_equal "Hello World!", @response.body - assert_equal "text/plain", @response.content_type - end -end - \ No newline at end of file diff --git a/railties/test/application/basic_rendering_test.rb b/railties/test/application/basic_rendering_test.rb new file mode 100644 index 0000000000..00ba433a05 --- /dev/null +++ b/railties/test/application/basic_rendering_test.rb @@ -0,0 +1,62 @@ +require 'isolation/abstract_unit' +require 'rack/test' + +module ApplicationTests + class BasicRenderingTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + include Rack::Test::Methods + + def setup + build_app + end + + def teardown + teardown_app + end + + test "Rendering without ActionView" do + gsub_app_file 'config/application.rb', "require 'rails/all'", <<-RUBY + require "active_model/railtie" + require "action_controller/railtie" + RUBY + + # Turn off ActionView and jquery-rails (it depends on AV) + $:.reject! {|path| path =~ /(actionview|jquery\-rails)/ } + boot_rails + + app_file 'app/controllers/pages_controller.rb', <<-RUBY + class PagesController < ApplicationController + def render_hello_world + render text: "Hello World!" + end + + def render_nothing + render nothing: true + end + + def no_render; end + + def raise_error + render foo: "bar" + end + end + RUBY + + get '/pages/render_hello_world' + assert_equal 200, last_response.status + assert_equal "Hello World!", last_response.body + assert_equal "text/plain; charset=utf-8", last_response.content_type + + get '/pages/render_nothing' + assert_equal 200, last_response.status + assert_equal " ", last_response.body + assert_equal "text/plain; charset=utf-8", last_response.content_type + + get '/pages/no_render' + assert_equal 500, last_response.status + + get '/pages/raise_error' + assert_equal 500, last_response.status + end + end +end diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index ab3ca106dc..913e2b5e29 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -243,6 +243,12 @@ module TestHelpers end end + def gsub_app_file(path, regexp, *args, &block) + path = "#{app_path}/#{path}" + content = File.read(path).gsub(regexp, *args, &block) + File.open(path, 'wb') { |f| f.write(content) } + end + def remove_file(path) FileUtils.rm_rf "#{app_path}/#{path}" end -- cgit v1.2.3