2004-11-24 01:04:44 +00:00
|
|
|
require File.dirname(__FILE__) + '/../abstract_unit'
|
|
|
|
|
2005-05-30 09:00:46 +00:00
|
|
|
unless defined?(Customer)
|
|
|
|
Customer = Struct.new("Customer", :name)
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
module Fun
|
|
|
|
class GamesController < ActionController::Base
|
2004-11-24 01:04:44 +00:00
|
|
|
def hello_world
|
|
|
|
end
|
2005-02-15 01:45:35 +00:00
|
|
|
end
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
layout :determine_layout
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def hello_world
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def render_hello_world
|
|
|
|
render "test/hello_world"
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def render_hello_world_from_variable
|
|
|
|
@person = "david"
|
|
|
|
render_text "hello #{@person}"
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def render_action_hello_world
|
|
|
|
render_action "hello_world"
|
|
|
|
end
|
2006-02-19 00:46:34 +00:00
|
|
|
|
|
|
|
def render_action_hello_world_with_symbol
|
|
|
|
render_action :hello_world
|
|
|
|
end
|
2006-09-04 04:05:38 +00:00
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def render_text_hello_world
|
|
|
|
render_text "hello world"
|
|
|
|
end
|
2006-12-06 23:06:38 +00:00
|
|
|
|
2006-12-06 22:27:08 +00:00
|
|
|
def render_json_hello_world
|
|
|
|
render_json({:hello => 'world'}.to_json)
|
|
|
|
end
|
2006-12-06 23:06:38 +00:00
|
|
|
|
2006-12-06 22:27:08 +00:00
|
|
|
def render_json_hello_world_with_callback
|
|
|
|
render_json({:hello => 'world'}.to_json, 'alert')
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2006-12-19 04:41:57 +00:00
|
|
|
def render_symbol_json
|
|
|
|
render :json => {:hello => 'world'}.to_json
|
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def render_custom_code
|
|
|
|
render_text "hello world", "404 Moved"
|
|
|
|
end
|
2006-09-04 04:05:38 +00:00
|
|
|
|
2006-10-09 01:26:35 +00:00
|
|
|
def render_text_appendix
|
|
|
|
render_text "hello world"
|
|
|
|
render_text ", goodbye!", "404 Not Found", true
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_nothing_with_appendix
|
|
|
|
render_text "appended", nil, true
|
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def render_xml_hello
|
|
|
|
@name = "David"
|
|
|
|
render "test/hello"
|
|
|
|
end
|
2005-02-15 00:51:02 +00:00
|
|
|
|
2007-02-17 18:16:44 +00:00
|
|
|
def heading
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def greeting
|
|
|
|
# let's just rely on the template
|
2005-02-15 00:51:02 +00:00
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def layout_test
|
|
|
|
render_action "hello_world"
|
|
|
|
end
|
2006-09-04 04:05:38 +00:00
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def builder_layout_test
|
|
|
|
render_action "hello"
|
|
|
|
end
|
|
|
|
|
2006-09-06 03:51:45 +00:00
|
|
|
def builder_partial_test
|
|
|
|
render_action "hello_world_container"
|
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def partials_list
|
2005-07-05 18:31:51 +00:00
|
|
|
@test_unchanged = 'hello'
|
2005-02-15 01:45:35 +00:00
|
|
|
@customers = [ Customer.new("david"), Customer.new("mary") ]
|
|
|
|
render_action "list"
|
|
|
|
end
|
|
|
|
|
2005-04-17 09:25:54 +00:00
|
|
|
def partial_only
|
|
|
|
render_partial
|
|
|
|
end
|
|
|
|
|
2005-02-23 14:57:08 +00:00
|
|
|
def hello_in_a_string
|
|
|
|
@customers = [ Customer.new("david"), Customer.new("mary") ]
|
|
|
|
render_text "How's there? #{render_to_string("test/list")}"
|
|
|
|
end
|
2006-09-04 04:05:38 +00:00
|
|
|
|
2005-05-19 16:00:04 +00:00
|
|
|
def accessing_params_in_template
|
|
|
|
render_template "Hello: <%= params[:name] %>"
|
|
|
|
end
|
2005-02-23 14:57:08 +00:00
|
|
|
|
2005-10-13 20:26:39 +00:00
|
|
|
def accessing_local_assigns_in_inline_template
|
|
|
|
name = params[:local_name]
|
2005-10-17 15:51:07 +00:00
|
|
|
render :inline => "<%= 'Goodbye, ' + local_name %>",
|
|
|
|
:locals => { :local_name => name }
|
2005-10-13 20:26:39 +00:00
|
|
|
end
|
2006-09-04 04:05:38 +00:00
|
|
|
|
2005-10-16 00:04:33 +00:00
|
|
|
def accessing_local_assigns_in_inline_template_with_string_keys
|
|
|
|
name = params[:local_name]
|
2005-10-17 15:51:07 +00:00
|
|
|
ActionView::Base.local_assigns_support_string_keys = true
|
|
|
|
render :inline => "<%= 'Goodbye, ' + local_name %>",
|
|
|
|
:locals => { "local_name" => name }
|
|
|
|
ActionView::Base.local_assigns_support_string_keys = false
|
2005-10-16 00:04:33 +00:00
|
|
|
end
|
2005-10-13 20:26:39 +00:00
|
|
|
|
2007-04-02 00:02:11 +00:00
|
|
|
def formatted_html_erb
|
|
|
|
end
|
|
|
|
|
|
|
|
def formatted_xml_erb
|
|
|
|
end
|
|
|
|
|
2005-10-25 02:15:08 +00:00
|
|
|
def render_to_string_test
|
|
|
|
@foo = render_to_string :inline => "this is a test"
|
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def rescue_action(e) raise end
|
2006-09-04 04:05:38 +00:00
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
private
|
|
|
|
def determine_layout
|
2006-09-04 04:05:38 +00:00
|
|
|
case action_name
|
2005-02-15 01:45:35 +00:00
|
|
|
when "layout_test": "layouts/standard"
|
|
|
|
when "builder_layout_test": "layouts/builder"
|
2006-12-19 04:41:57 +00:00
|
|
|
when "render_symbol_json": "layouts/standard" # to make sure layouts don't interfere
|
2005-02-15 01:45:35 +00:00
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2005-02-15 01:45:35 +00:00
|
|
|
end
|
2005-02-15 00:51:02 +00:00
|
|
|
|
2007-02-04 20:47:05 +00:00
|
|
|
TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
|
|
|
Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
2005-02-15 01:45:35 +00:00
|
|
|
|
|
|
|
class RenderTest < Test::Unit::TestCase
|
2004-11-24 01:04:44 +00:00
|
|
|
def setup
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2006-03-12 01:56:55 +00:00
|
|
|
@controller = TestController.new
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
@request.host = "www.nextangle.com"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_simple_show
|
2006-09-04 05:24:08 +00:00
|
|
|
get :hello_world
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_response 200
|
|
|
|
assert_template "test/hello_world"
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_with_render
|
2006-09-04 05:51:48 +00:00
|
|
|
assert_deprecated_render { get :render_hello_world }
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_template "test/hello_world"
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_with_render_from_variable
|
2006-09-04 05:24:08 +00:00
|
|
|
get :render_hello_world_from_variable
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "hello david", @response.body
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_with_render_action
|
2006-09-04 05:24:08 +00:00
|
|
|
get :render_action_hello_world
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_template "test/hello_world"
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2006-02-19 00:46:34 +00:00
|
|
|
def test_do_with_render_action_with_symbol
|
2006-09-04 05:24:08 +00:00
|
|
|
get :render_action_hello_world_with_symbol
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_template "test/hello_world"
|
2006-02-19 00:46:34 +00:00
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_do_with_render_text
|
2006-09-04 05:24:08 +00:00
|
|
|
get :render_text_hello_world
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "hello world", @response.body
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2006-12-06 23:06:38 +00:00
|
|
|
|
2006-12-06 22:27:08 +00:00
|
|
|
def test_do_with_render_json
|
|
|
|
get :render_json_hello_world
|
|
|
|
assert_equal '{hello: "world"}', @response.body
|
2006-12-06 23:06:38 +00:00
|
|
|
assert_equal 'application/json', @response.content_type
|
2006-12-06 22:27:08 +00:00
|
|
|
end
|
2006-12-06 23:06:38 +00:00
|
|
|
|
2006-12-06 22:27:08 +00:00
|
|
|
def test_do_with_render_json_with_callback
|
|
|
|
get :render_json_hello_world_with_callback
|
|
|
|
assert_equal 'alert({hello: "world"})', @response.body
|
2006-12-06 23:06:38 +00:00
|
|
|
assert_equal 'application/json', @response.content_type
|
2006-12-06 22:27:08 +00:00
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2006-12-19 04:41:57 +00:00
|
|
|
def test_do_with_render_symbol_json
|
|
|
|
get :render_symbol_json
|
|
|
|
assert_equal '{hello: "world"}', @response.body
|
|
|
|
assert_equal 'application/json', @response.content_type
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_do_with_render_custom_code
|
2006-09-04 05:24:08 +00:00
|
|
|
get :render_custom_code
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_response 404
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2006-10-09 01:26:35 +00:00
|
|
|
def test_do_with_render_text_appendix
|
|
|
|
get :render_text_appendix
|
|
|
|
assert_response 404
|
|
|
|
assert_equal 'hello world, goodbye!', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_with_render_nothing_with_appendix
|
|
|
|
get :render_nothing_with_appendix
|
|
|
|
assert_response 200
|
|
|
|
assert_equal 'appended', @response.body
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_attempt_to_access_object_method
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2005-01-09 15:20:00 +00:00
|
|
|
def test_private_methods
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
|
2005-01-09 15:20:00 +00:00
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_render_xml
|
2006-09-04 05:51:48 +00:00
|
|
|
assert_deprecated_render { get :render_xml_hello }
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_xml_with_default
|
2006-09-04 05:24:08 +00:00
|
|
|
get :greeting
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "<p>This is grand!</p>\n", @response.body
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2006-09-06 03:51:45 +00:00
|
|
|
def test_render_xml_with_partial
|
|
|
|
get :builder_partial_test
|
|
|
|
assert_equal "<test>\n <hello/>\n</test>\n", @response.body
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_layout_rendering
|
2006-09-04 05:24:08 +00:00
|
|
|
get :layout_test
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "<html>Hello world!</html>", @response.body
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_xml_with_layouts
|
2006-09-04 05:24:08 +00:00
|
|
|
get :builder_layout_test
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2005-07-06 08:27:11 +00:00
|
|
|
# def test_partials_list
|
2006-03-12 01:56:55 +00:00
|
|
|
# get :partials_list
|
2005-07-06 08:27:11 +00:00
|
|
|
# assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
|
|
|
|
# end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-04-17 09:25:54 +00:00
|
|
|
def test_partial_only
|
2006-09-04 05:24:08 +00:00
|
|
|
get :partial_only
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "only partial", @response.body
|
2005-04-17 09:25:54 +00:00
|
|
|
end
|
|
|
|
|
2005-02-23 14:57:08 +00:00
|
|
|
def test_render_to_string
|
2006-09-04 05:24:08 +00:00
|
|
|
get :hello_in_a_string
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
|
2005-10-25 02:15:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_to_string_resets_assigns
|
2006-09-04 05:24:08 +00:00
|
|
|
get :render_to_string_test
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "The value of foo is: ::this is a test::\n", @response.body
|
2005-02-23 14:57:08 +00:00
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def test_nested_rendering
|
2006-03-12 01:56:55 +00:00
|
|
|
@controller = Fun::GamesController.new
|
|
|
|
get :hello_world
|
|
|
|
assert_equal "Living in a nested world", @response.body
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2005-05-19 16:00:04 +00:00
|
|
|
def test_accessing_params_in_template
|
2006-09-04 05:24:08 +00:00
|
|
|
get :accessing_params_in_template, :name => "David"
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "Hello: David", @response.body
|
2005-05-19 16:00:04 +00:00
|
|
|
end
|
|
|
|
|
2005-10-13 20:26:39 +00:00
|
|
|
def test_accessing_local_assigns_in_inline_template
|
2006-09-04 05:24:08 +00:00
|
|
|
get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "Goodbye, Local David", @response.body
|
2005-10-13 20:26:39 +00:00
|
|
|
end
|
2006-09-04 04:05:38 +00:00
|
|
|
|
2005-10-16 00:04:33 +00:00
|
|
|
def test_accessing_local_assigns_in_inline_template_with_string_keys
|
2006-09-04 05:24:08 +00:00
|
|
|
get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
|
2006-03-12 01:56:55 +00:00
|
|
|
assert_equal "Goodbye, Local David", @response.body
|
2005-10-16 00:04:33 +00:00
|
|
|
end
|
2006-09-04 05:51:48 +00:00
|
|
|
|
2007-02-17 18:16:44 +00:00
|
|
|
def test_render_200_should_set_etag
|
|
|
|
get :render_hello_world_from_variable
|
2007-02-19 02:10:19 +00:00
|
|
|
assert_equal etag_for("hello david"), @response.headers['ETag']
|
2007-02-17 18:16:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_against_etag_request_should_304_when_match
|
|
|
|
@request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello david")
|
|
|
|
get :render_hello_world_from_variable
|
|
|
|
assert_equal "304 Not Modified", @response.headers['Status']
|
|
|
|
assert @response.body.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_against_etag_request_should_200_when_no_match
|
|
|
|
@request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello somewhere else")
|
|
|
|
get :render_hello_world_from_variable
|
|
|
|
assert_equal "200 OK", @response.headers['Status']
|
|
|
|
assert !@response.body.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_with_etag
|
|
|
|
get :render_hello_world_from_variable
|
2007-02-18 00:47:18 +00:00
|
|
|
expected_etag = etag_for('hello david')
|
2007-02-19 02:10:19 +00:00
|
|
|
assert_equal expected_etag, @response.headers['ETag']
|
2007-02-18 00:47:18 +00:00
|
|
|
|
2007-02-17 18:16:44 +00:00
|
|
|
@request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
|
|
|
|
get :render_hello_world_from_variable
|
|
|
|
assert_equal "304 Not Modified", @response.headers['Status']
|
2007-02-18 00:47:18 +00:00
|
|
|
|
2007-02-17 18:16:44 +00:00
|
|
|
@request.headers["HTTP_IF_NONE_MATCH"] = "\"diftag\""
|
|
|
|
get :render_hello_world_from_variable
|
|
|
|
assert_equal "200 OK", @response.headers['Status']
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_with_404_shouldnt_have_etag
|
|
|
|
get :render_custom_code
|
2007-02-19 02:10:19 +00:00
|
|
|
assert_nil @response.headers['ETag']
|
2007-02-17 18:16:44 +00:00
|
|
|
end
|
|
|
|
|
2007-02-18 16:32:05 +00:00
|
|
|
def test_etag_should_not_be_changed_when_already_set
|
|
|
|
expected_etag = etag_for("hello somewhere else")
|
2007-02-19 02:10:19 +00:00
|
|
|
@response.headers["ETag"] = expected_etag
|
2007-02-18 16:32:05 +00:00
|
|
|
get :render_hello_world_from_variable
|
2007-02-19 02:10:19 +00:00
|
|
|
assert_equal expected_etag, @response.headers['ETag']
|
2007-02-18 16:32:05 +00:00
|
|
|
end
|
|
|
|
|
2007-02-18 23:54:20 +00:00
|
|
|
def test_etag_should_govern_renders_with_layouts_too
|
|
|
|
get :builder_layout_test
|
2007-02-19 00:27:19 +00:00
|
|
|
assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
|
2007-02-19 02:10:19 +00:00
|
|
|
assert_equal etag_for("<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n"), @response.headers['ETag']
|
2007-02-18 23:54:20 +00:00
|
|
|
end
|
|
|
|
|
2007-04-02 00:02:11 +00:00
|
|
|
def test_should_render_formatted_template
|
|
|
|
get :formatted_html_erb
|
|
|
|
assert_equal 'formatted html erb', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_render_formatted_xml_erb_template
|
|
|
|
get :formatted_xml_erb, :format => :xml
|
|
|
|
assert_equal '<test>passed formatted xml erb</test>', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_render_formatted_html_erb_template
|
|
|
|
get :formatted_xml_erb
|
|
|
|
assert_equal '<test>passed formatted html erb</test>', @response.body
|
|
|
|
end
|
2007-02-18 23:54:20 +00:00
|
|
|
|
2006-09-04 05:51:48 +00:00
|
|
|
protected
|
|
|
|
def assert_deprecated_render(&block)
|
|
|
|
assert_deprecated(/render/, &block)
|
|
|
|
end
|
2007-02-18 00:47:18 +00:00
|
|
|
|
2007-02-17 18:16:44 +00:00
|
|
|
def etag_for(text)
|
2007-02-18 00:47:18 +00:00
|
|
|
%("#{Digest::MD5.hexdigest(text)}")
|
2007-02-17 18:16:44 +00:00
|
|
|
end
|
2005-05-30 09:00:46 +00:00
|
|
|
end
|