2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'controller/fake_controllers'
|
2005-03-26 16:43:25 -05:00
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class TestTest < ActionController::TestCase
|
2005-03-26 16:43:25 -05:00
|
|
|
class TestController < ActionController::Base
|
2008-02-02 00:32:44 -05:00
|
|
|
def no_op
|
|
|
|
render :text => 'dummy'
|
|
|
|
end
|
|
|
|
|
2005-03-26 16:43:25 -05:00
|
|
|
def set_flash
|
|
|
|
flash["test"] = ">#{flash["test"]}<"
|
2005-07-21 03:14:35 -04:00
|
|
|
render :text => 'ignore me'
|
2005-03-26 16:43:25 -05:00
|
|
|
end
|
2006-03-20 13:16:48 -05:00
|
|
|
|
2008-05-10 11:54:09 -04:00
|
|
|
def set_flash_now
|
|
|
|
flash.now["test_now"] = ">#{flash["test_now"]}<"
|
|
|
|
render :text => 'ignore me'
|
|
|
|
end
|
|
|
|
|
2008-02-02 00:32:44 -05:00
|
|
|
def set_session
|
|
|
|
session['string'] = 'A wonder'
|
|
|
|
session[:symbol] = 'it works'
|
|
|
|
render :text => 'Success'
|
|
|
|
end
|
|
|
|
|
2009-01-29 20:39:48 -05:00
|
|
|
def reset_the_session
|
|
|
|
reset_session
|
|
|
|
render :text => 'ignore me'
|
|
|
|
end
|
|
|
|
|
2006-03-15 22:15:12 -05:00
|
|
|
def render_raw_post
|
2008-11-07 15:42:34 -05:00
|
|
|
raise ActiveSupport::TestCase::Assertion, "#raw_post is blank" if request.raw_post.blank?
|
2006-03-15 22:15:12 -05:00
|
|
|
render :text => request.raw_post
|
|
|
|
end
|
|
|
|
|
2007-05-15 17:36:21 -04:00
|
|
|
def render_body
|
|
|
|
render :text => request.body.read
|
|
|
|
end
|
|
|
|
|
2005-06-29 05:44:55 -04:00
|
|
|
def test_params
|
|
|
|
render :text => params.inspect
|
2006-09-04 00:03:57 -04:00
|
|
|
end
|
|
|
|
|
2005-04-07 02:22:19 -04:00
|
|
|
def test_uri
|
2005-06-28 22:41:00 -04:00
|
|
|
render :text => request.request_uri
|
2005-04-07 02:22:19 -04:00
|
|
|
end
|
2005-04-17 12:43:48 -04:00
|
|
|
|
2007-10-07 23:33:30 -04:00
|
|
|
def test_query_string
|
|
|
|
render :text => request.query_string
|
|
|
|
end
|
|
|
|
|
2005-04-17 12:43:48 -04:00
|
|
|
def test_html_output
|
2005-06-28 22:41:00 -04:00
|
|
|
render :text => <<HTML
|
2005-04-17 12:43:48 -04:00
|
|
|
<html>
|
|
|
|
<body>
|
2005-10-17 20:50:52 -04:00
|
|
|
<a href="/"><img src="/images/button.png" /></a>
|
2005-04-17 12:43:48 -04:00
|
|
|
<div id="foo">
|
|
|
|
<ul>
|
|
|
|
<li class="item">hello</li>
|
|
|
|
<li class="item">goodbye</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div id="bar">
|
|
|
|
<form action="/somewhere">
|
|
|
|
Name: <input type="text" name="person[name]" id="person_name" />
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML
|
|
|
|
end
|
2008-07-28 14:38:20 -04:00
|
|
|
|
2007-09-07 21:32:16 -04:00
|
|
|
def test_xml_output
|
|
|
|
response.content_type = "application/xml"
|
|
|
|
render :text => <<XML
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<root>
|
|
|
|
<area>area is an empty tag in HTML, raising an error if not in xml mode</area>
|
|
|
|
</root>
|
|
|
|
XML
|
|
|
|
end
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2005-06-30 10:49:36 -04:00
|
|
|
def test_only_one_param
|
2005-07-31 06:31:47 -04:00
|
|
|
render :text => (params[:left] && params[:right]) ? "EEP, Both here!" : "OK"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_remote_addr
|
|
|
|
render :text => (request.remote_addr || "not specified")
|
2005-06-30 10:49:36 -04:00
|
|
|
end
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
def test_file_upload
|
|
|
|
render :text => params[:file].size
|
|
|
|
end
|
2006-03-15 22:15:12 -05:00
|
|
|
|
2007-12-05 18:23:04 -05:00
|
|
|
def test_send_file
|
|
|
|
send_file(File.expand_path(__FILE__))
|
|
|
|
end
|
|
|
|
|
2006-05-22 18:59:56 -04:00
|
|
|
def redirect_to_same_controller
|
|
|
|
redirect_to :controller => 'test', :action => 'test_uri', :id => 5
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_different_controller
|
|
|
|
redirect_to :controller => 'fail', :id => 5
|
|
|
|
end
|
|
|
|
|
2006-11-28 18:25:00 -05:00
|
|
|
def create
|
2007-05-18 17:23:07 -04:00
|
|
|
head :created, :location => 'created resource'
|
2006-11-28 18:25:00 -05:00
|
|
|
end
|
|
|
|
|
2009-06-05 09:58:38 -04:00
|
|
|
def delete_cookie
|
|
|
|
cookies.delete("foo")
|
|
|
|
render :nothing => true
|
|
|
|
end
|
|
|
|
|
2006-03-20 13:16:48 -05:00
|
|
|
private
|
|
|
|
def rescue_action(e)
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_url(opts)
|
|
|
|
url_for(opts.merge(:action => "test_uri"))
|
|
|
|
end
|
2005-03-26 16:43:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2005-03-26 16:43:25 -05:00
|
|
|
@controller = TestController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2005-04-07 02:22:19 -04:00
|
|
|
end
|
|
|
|
|
2006-03-15 22:15:12 -05:00
|
|
|
def test_raw_post_handling
|
|
|
|
params = {:page => {:name => 'page name'}, 'some key' => 123}
|
2007-10-22 17:10:56 -04:00
|
|
|
post :render_raw_post, params.dup
|
2006-03-15 22:15:12 -05:00
|
|
|
|
2007-05-15 17:36:21 -04:00
|
|
|
assert_equal params.to_query, @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_body_stream
|
|
|
|
params = { :page => { :name => 'page name' }, 'some key' => 123 }
|
|
|
|
|
2007-10-22 17:10:56 -04:00
|
|
|
post :render_body, params.dup
|
2007-05-15 17:36:21 -04:00
|
|
|
|
|
|
|
assert_equal params.to_query, @response.body
|
2006-03-15 22:15:12 -05:00
|
|
|
end
|
|
|
|
|
2005-03-26 16:43:25 -05:00
|
|
|
def test_process_without_flash
|
|
|
|
process :set_flash
|
2005-07-15 11:00:39 -04:00
|
|
|
assert_equal '><', flash['test']
|
2006-09-04 00:03:57 -04:00
|
|
|
end
|
|
|
|
|
2005-03-26 16:43:25 -05:00
|
|
|
def test_process_with_flash
|
|
|
|
process :set_flash, nil, nil, { "test" => "value" }
|
2005-07-15 11:00:39 -04:00
|
|
|
assert_equal '>value<', flash['test']
|
2005-03-26 16:43:25 -05:00
|
|
|
end
|
2005-04-07 02:22:19 -04:00
|
|
|
|
2008-05-10 11:54:09 -04:00
|
|
|
def test_process_with_flash_now
|
|
|
|
process :set_flash_now, nil, nil, { "test_now" => "value_now" }
|
|
|
|
assert_equal '>value_now<', flash['test_now']
|
|
|
|
end
|
|
|
|
|
2008-02-02 00:32:44 -05:00
|
|
|
def test_process_with_session
|
|
|
|
process :set_session
|
|
|
|
assert_equal 'A wonder', session['string'], "A value stored in the session should be available by string key"
|
|
|
|
assert_equal 'A wonder', session[:string], "Test session hash should allow indifferent access"
|
|
|
|
assert_equal 'it works', session['symbol'], "Test session hash should allow indifferent access"
|
|
|
|
assert_equal 'it works', session[:symbol], "Test session hash should allow indifferent access"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_with_session_arg
|
|
|
|
process :no_op, nil, { 'string' => 'value1', :symbol => 'value2' }
|
|
|
|
assert_equal 'value1', session['string']
|
|
|
|
assert_equal 'value1', session[:string]
|
|
|
|
assert_equal 'value2', session['symbol']
|
|
|
|
assert_equal 'value2', session[:symbol]
|
|
|
|
end
|
|
|
|
|
2009-01-29 20:39:48 -05:00
|
|
|
def test_session_is_cleared_from_controller_after_reset_session
|
|
|
|
process :set_session
|
|
|
|
process :reset_the_session
|
|
|
|
assert_equal Hash.new, @controller.session.to_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_session_is_cleared_from_request_after_reset_session
|
|
|
|
process :set_session
|
|
|
|
process :reset_the_session
|
|
|
|
assert_equal Hash.new, @request.session.to_hash
|
|
|
|
end
|
|
|
|
|
2005-04-07 02:22:19 -04:00
|
|
|
def test_process_with_request_uri_with_no_params
|
|
|
|
process :test_uri
|
2005-06-28 22:41:00 -04:00
|
|
|
assert_equal "/test_test/test/test_uri", @response.body
|
2005-04-07 02:22:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_with_request_uri_with_params
|
|
|
|
process :test_uri, :id => 7
|
2005-06-28 22:41:00 -04:00
|
|
|
assert_equal "/test_test/test/test_uri/7", @response.body
|
2005-04-07 02:22:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_with_request_uri_with_params_with_explicit_uri
|
2009-04-30 18:26:03 -04:00
|
|
|
@request.request_uri = "/explicit/uri"
|
2005-04-07 02:22:19 -04:00
|
|
|
process :test_uri, :id => 7
|
2005-06-28 22:41:00 -04:00
|
|
|
assert_equal "/explicit/uri", @response.body
|
2006-09-04 00:03:57 -04:00
|
|
|
end
|
|
|
|
|
2007-10-07 23:33:30 -04:00
|
|
|
def test_process_with_query_string
|
|
|
|
process :test_query_string, :q => 'test'
|
|
|
|
assert_equal "q=test", @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_with_query_string_with_explicit_uri
|
2009-04-30 18:26:03 -04:00
|
|
|
@request.request_uri = "/explicit/uri?q=test?extra=question"
|
2007-10-07 23:33:30 -04:00
|
|
|
process :test_query_string
|
|
|
|
assert_equal "q=test?extra=question", @response.body
|
|
|
|
end
|
|
|
|
|
2006-09-04 00:03:57 -04:00
|
|
|
def test_multiple_calls
|
2005-06-30 10:49:36 -04:00
|
|
|
process :test_only_one_param, :left => true
|
|
|
|
assert_equal "OK", @response.body
|
2006-09-04 00:03:57 -04:00
|
|
|
process :test_only_one_param, :right => true
|
2005-06-30 10:49:36 -04:00
|
|
|
assert_equal "OK", @response.body
|
2005-04-07 02:22:19 -04:00
|
|
|
end
|
2005-04-17 12:43:48 -04:00
|
|
|
|
2005-10-08 21:45:21 -04:00
|
|
|
def test_assert_tag_tag
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a 'form' tag
|
|
|
|
assert_tag :tag => 'form'
|
|
|
|
# there is not an 'hr' tag
|
|
|
|
assert_no_tag :tag => 'hr'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_attributes
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a tag with an 'id' of 'bar'
|
|
|
|
assert_tag :attributes => { :id => "bar" }
|
|
|
|
# there is no tag with a 'name' of 'baz'
|
|
|
|
assert_no_tag :attributes => { :name => "baz" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_parent
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a tag with a parent 'form' tag
|
|
|
|
assert_tag :parent => { :tag => "form" }
|
|
|
|
# there is no tag with a parent of 'input'
|
|
|
|
assert_no_tag :parent => { :tag => "input" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_child
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a tag with a child 'input' tag
|
|
|
|
assert_tag :child => { :tag => "input" }
|
2005-10-17 20:50:52 -04:00
|
|
|
# there is no tag with a child 'strong' tag
|
|
|
|
assert_no_tag :child => { :tag => "strong" }
|
2005-10-08 21:45:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_ancestor
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a 'li' tag with an ancestor having an id of 'foo'
|
|
|
|
assert_tag :ancestor => { :attributes => { :id => "foo" } }, :tag => "li"
|
|
|
|
# there is no tag of any kind with an ancestor having an href matching 'foo'
|
|
|
|
assert_no_tag :ancestor => { :attributes => { :href => /foo/ } }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_descendant
|
|
|
|
process :test_html_output
|
|
|
|
|
2007-12-05 13:54:41 -05:00
|
|
|
# there is a tag with a descendant 'li' tag
|
2005-10-08 21:45:21 -04:00
|
|
|
assert_tag :descendant => { :tag => "li" }
|
|
|
|
# there is no tag with a descendant 'html' tag
|
|
|
|
assert_no_tag :descendant => { :tag => "html" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_sibling
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a tag with a sibling of class 'item'
|
|
|
|
assert_tag :sibling => { :attributes => { :class => "item" } }
|
|
|
|
# there is no tag with a sibling 'ul' tag
|
|
|
|
assert_no_tag :sibling => { :tag => "ul" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_after
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a tag following a sibling 'div' tag
|
|
|
|
assert_tag :after => { :tag => "div" }
|
|
|
|
# there is no tag following a sibling tag with id 'bar'
|
|
|
|
assert_no_tag :after => { :attributes => { :id => "bar" } }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_before
|
|
|
|
process :test_html_output
|
|
|
|
|
2007-09-28 10:18:47 -04:00
|
|
|
# there is a tag preceding a tag with id 'bar'
|
2005-10-08 21:45:21 -04:00
|
|
|
assert_tag :before => { :attributes => { :id => "bar" } }
|
2007-09-28 10:18:47 -04:00
|
|
|
# there is no tag preceding a 'form' tag
|
2005-10-08 21:45:21 -04:00
|
|
|
assert_no_tag :before => { :tag => "form" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_children_count
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a tag with 2 children
|
|
|
|
assert_tag :children => { :count => 2 }
|
2006-09-03 12:40:01 -04:00
|
|
|
# in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
|
|
|
|
assert_tag :tag => 'ul', :children => { :count => 2 }
|
2005-10-08 21:45:21 -04:00
|
|
|
# there is no tag with 4 children
|
|
|
|
assert_no_tag :children => { :count => 4 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_children_less_than
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a tag with less than 5 children
|
|
|
|
assert_tag :children => { :less_than => 5 }
|
|
|
|
# there is no 'ul' tag with less than 2 children
|
|
|
|
assert_no_tag :children => { :less_than => 2 }, :tag => "ul"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_children_greater_than
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a 'body' tag with more than 1 children
|
|
|
|
assert_tag :children => { :greater_than => 1 }, :tag => "body"
|
|
|
|
# there is no tag with more than 10 children
|
|
|
|
assert_no_tag :children => { :greater_than => 10 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_children_only
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a tag containing only one child with an id of 'foo'
|
2006-09-04 00:03:57 -04:00
|
|
|
assert_tag :children => { :count => 1,
|
2005-10-08 21:45:21 -04:00
|
|
|
:only => { :attributes => { :id => "foo" } } }
|
|
|
|
# there is no tag containing only one 'li' child
|
|
|
|
assert_no_tag :children => { :count => 1, :only => { :tag => "li" } }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_tag_content
|
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# the output contains the string "Name"
|
2006-09-09 22:51:38 -04:00
|
|
|
assert_tag :content => /Name/
|
2005-10-08 21:45:21 -04:00
|
|
|
# the output does not contain the string "test"
|
2006-09-09 22:51:38 -04:00
|
|
|
assert_no_tag :content => /test/
|
2005-10-08 21:45:21 -04:00
|
|
|
end
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2005-10-08 21:45:21 -04:00
|
|
|
def test_assert_tag_multiple
|
2005-04-17 12:43:48 -04:00
|
|
|
process :test_html_output
|
|
|
|
|
|
|
|
# there is a 'div', id='bar', with an immediate child whose 'action'
|
|
|
|
# attribute matches the regexp /somewhere/.
|
|
|
|
assert_tag :tag => "div", :attributes => { :id => "bar" },
|
|
|
|
:child => { :attributes => { :action => /somewhere/ } }
|
|
|
|
|
|
|
|
# there is no 'div', id='foo', with a 'ul' child with more than
|
|
|
|
# 2 "li" children.
|
|
|
|
assert_no_tag :tag => "div", :attributes => { :id => "foo" },
|
|
|
|
:child => {
|
|
|
|
:tag => "ul",
|
|
|
|
:children => { :greater_than => 2,
|
|
|
|
:only => { :tag => "li" } } }
|
|
|
|
end
|
2005-06-25 06:58:44 -04:00
|
|
|
|
2005-10-17 20:50:52 -04:00
|
|
|
def test_assert_tag_children_without_content
|
|
|
|
process :test_html_output
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2005-10-17 20:50:52 -04:00
|
|
|
# there is a form tag with an 'input' child which is a self closing tag
|
|
|
|
assert_tag :tag => "form",
|
|
|
|
:children => { :count => 1,
|
|
|
|
:only => { :tag => "input" } }
|
|
|
|
|
|
|
|
# the body tag has an 'a' child which in turn has an 'img' child
|
|
|
|
assert_tag :tag => "body",
|
|
|
|
:children => { :count => 1,
|
|
|
|
:only => { :tag => "a",
|
|
|
|
:children => { :count => 1,
|
|
|
|
:only => { :tag => "img" } } } }
|
|
|
|
end
|
2008-07-28 14:38:20 -04:00
|
|
|
|
2007-09-07 21:32:16 -04:00
|
|
|
def test_should_not_impose_childless_html_tags_in_xml
|
|
|
|
process :test_xml_output
|
|
|
|
|
|
|
|
begin
|
|
|
|
$stderr = StringIO.new
|
|
|
|
assert_select 'area' #This will cause a warning if content is processed as HTML
|
|
|
|
$stderr.rewind && err = $stderr.read
|
|
|
|
ensure
|
|
|
|
$stderr = STDERR
|
|
|
|
end
|
|
|
|
|
|
|
|
assert err.empty?
|
|
|
|
end
|
2005-10-17 20:50:52 -04:00
|
|
|
|
2006-09-09 18:05:21 -04:00
|
|
|
def test_assert_tag_attribute_matching
|
|
|
|
@response.body = '<input type="text" name="my_name">'
|
|
|
|
assert_tag :tag => 'input',
|
|
|
|
:attributes => { :name => /my/, :type => 'text' }
|
|
|
|
assert_no_tag :tag => 'input',
|
|
|
|
:attributes => { :name => 'my', :type => 'text' }
|
|
|
|
assert_no_tag :tag => 'input',
|
|
|
|
:attributes => { :name => /^my$/, :type => 'text' }
|
|
|
|
end
|
|
|
|
|
2006-09-09 22:51:38 -04:00
|
|
|
def test_assert_tag_content_matching
|
|
|
|
@response.body = "<p>hello world</p>"
|
|
|
|
assert_tag :tag => "p", :content => "hello world"
|
|
|
|
assert_tag :tag => "p", :content => /hello/
|
|
|
|
assert_no_tag :tag => "p", :content => "hello"
|
|
|
|
end
|
|
|
|
|
2005-07-15 11:00:39 -04:00
|
|
|
def test_assert_generates
|
2005-06-25 06:58:44 -04:00
|
|
|
assert_generates 'controller/action/5', :controller => 'controller', :action => 'action', :id => '5'
|
2006-10-16 10:32:11 -04:00
|
|
|
assert_generates 'controller/action/7', {:id => "7"}, {:controller => "controller", :action => "action"}
|
|
|
|
assert_generates 'controller/action/5', {:controller => "controller", :action => "action", :id => "5", :name => "bob"}, {}, {:name => "bob"}
|
|
|
|
assert_generates 'controller/action/7', {:id => "7", :name => "bob"}, {:controller => "controller", :action => "action"}, {:name => "bob"}
|
|
|
|
assert_generates 'controller/action/7', {:id => "7"}, {:controller => "controller", :action => "action", :name => "bob"}, {}
|
2005-06-25 06:58:44 -04:00
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2005-07-15 11:00:39 -04:00
|
|
|
def test_assert_routing
|
|
|
|
assert_routing 'content', :controller => 'content', :action => 'index'
|
|
|
|
end
|
|
|
|
|
2008-01-29 20:25:44 -05:00
|
|
|
def test_assert_routing_with_method
|
|
|
|
with_routing do |set|
|
2008-07-28 14:41:42 -04:00
|
|
|
set.draw { |map| map.resources(:content) }
|
2008-01-29 20:25:44 -05:00
|
|
|
assert_routing({ :method => 'post', :path => 'content' }, { :controller => 'content', :action => 'create' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-15 11:00:39 -04:00
|
|
|
def test_assert_routing_in_module
|
|
|
|
assert_routing 'admin/user', :controller => 'admin/user', :action => 'index'
|
|
|
|
end
|
|
|
|
|
2005-06-29 05:44:55 -04:00
|
|
|
def test_params_passing
|
|
|
|
get :test_params, :page => {:name => "Page name", :month => '4', :year => '2004', :day => '6'}
|
|
|
|
parsed_params = eval(@response.body)
|
|
|
|
assert_equal(
|
|
|
|
{'controller' => 'test_test/test', 'action' => 'test_params',
|
|
|
|
'page' => {'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6'}},
|
|
|
|
parsed_params
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2005-07-07 15:23:35 -04:00
|
|
|
def test_id_converted_to_string
|
2005-06-29 05:44:55 -04:00
|
|
|
get :test_params, :id => 20, :foo => Object.new
|
2005-07-07 15:23:35 -04:00
|
|
|
assert_kind_of String, @request.path_parameters['id']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_array_path_parameter_handled_properly
|
|
|
|
with_routing do |set|
|
2006-06-01 11:42:08 -04:00
|
|
|
set.draw do |map|
|
|
|
|
map.connect 'file/*path', :controller => 'test_test/test', :action => 'test_params'
|
|
|
|
map.connect ':controller/:action/:id'
|
2005-07-07 15:23:35 -04:00
|
|
|
end
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2005-07-07 15:23:35 -04:00
|
|
|
get :test_params, :path => ['hello', 'world']
|
|
|
|
assert_equal ['hello', 'world'], @request.path_parameters['path']
|
|
|
|
assert_equal 'hello/world', @request.path_parameters['path'].to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_realistic_path_parameters
|
|
|
|
get :test_params, :id => 20, :foo => Object.new
|
|
|
|
|
|
|
|
# All elements of path_parameters should use string keys
|
|
|
|
@request.path_parameters.keys.each do |key|
|
|
|
|
assert_kind_of String, key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_routing_places_routes_back
|
|
|
|
assert ActionController::Routing::Routes
|
|
|
|
routes_id = ActionController::Routing::Routes.object_id
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2005-07-07 15:23:35 -04:00
|
|
|
begin
|
|
|
|
with_routing { raise 'fail' }
|
|
|
|
fail 'Should not be here.'
|
|
|
|
rescue RuntimeError
|
2005-06-29 05:44:55 -04:00
|
|
|
end
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2005-07-07 15:23:35 -04:00
|
|
|
assert ActionController::Routing::Routes
|
|
|
|
assert_equal routes_id, ActionController::Routing::Routes.object_id
|
2005-06-29 05:44:55 -04:00
|
|
|
end
|
2005-07-31 06:31:47 -04:00
|
|
|
|
|
|
|
def test_remote_addr
|
|
|
|
get :test_remote_addr
|
|
|
|
assert_equal "0.0.0.0", @response.body
|
|
|
|
|
|
|
|
@request.remote_addr = "192.0.0.1"
|
|
|
|
get :test_remote_addr
|
|
|
|
assert_equal "192.0.0.1", @response.body
|
|
|
|
end
|
2005-08-01 17:28:17 -04:00
|
|
|
|
2005-10-13 14:09:27 -04:00
|
|
|
def test_header_properly_reset_after_remote_http_request
|
|
|
|
xhr :get, :test_params
|
|
|
|
assert_nil @request.env['HTTP_X_REQUESTED_WITH']
|
|
|
|
end
|
|
|
|
|
2008-07-28 14:38:20 -04:00
|
|
|
def test_header_properly_reset_after_get_request
|
2006-02-20 18:49:43 -05:00
|
|
|
get :test_params
|
|
|
|
@request.recycle!
|
|
|
|
assert_nil @request.instance_variable_get("@request_method")
|
|
|
|
end
|
|
|
|
|
2009-03-24 11:37:57 -04:00
|
|
|
def test_params_reset_after_post_request
|
|
|
|
post :no_op, :foo => "bar"
|
|
|
|
assert_equal "bar", @request.params[:foo]
|
|
|
|
@request.recycle!
|
|
|
|
post :no_op
|
|
|
|
assert @request.params[:foo].blank?
|
|
|
|
end
|
|
|
|
|
2009-06-05 09:58:38 -04:00
|
|
|
def test_should_have_knowledge_of_client_side_cookie_state_even_if_they_are_not_set
|
|
|
|
@request.cookies['foo'] = 'bar'
|
|
|
|
get :no_op
|
|
|
|
assert_equal 'bar', cookies['foo']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_detect_if_cookie_is_deleted
|
|
|
|
@request.cookies['foo'] = 'bar'
|
|
|
|
get :delete_cookie
|
|
|
|
assert_nil cookies['foo']
|
|
|
|
end
|
|
|
|
|
2005-08-01 17:28:17 -04:00
|
|
|
%w(controller response request).each do |variable|
|
|
|
|
%w(get post put delete head process).each do |method|
|
|
|
|
define_method("test_#{variable}_missing_for_#{method}_raises_error") do
|
|
|
|
remove_instance_variable "@#{variable}"
|
|
|
|
begin
|
|
|
|
send(method, :test_remote_addr)
|
|
|
|
assert false, "expected RuntimeError, got nothing"
|
|
|
|
rescue RuntimeError => error
|
|
|
|
assert true
|
|
|
|
assert_match %r{@#{variable} is nil}, error.message
|
|
|
|
rescue => error
|
|
|
|
assert false, "expected RuntimeError, got #{error.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart'
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2008-05-19 19:01:42 -04:00
|
|
|
if RUBY_VERSION < '1.9'
|
|
|
|
READ_BINARY = 'rb'
|
|
|
|
READ_PLAIN = 'r'
|
|
|
|
else
|
|
|
|
READ_BINARY = 'rb:binary'
|
|
|
|
READ_PLAIN = 'r:binary'
|
|
|
|
end
|
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
def test_test_uploaded_file
|
|
|
|
filename = 'mona_lisa.jpg'
|
|
|
|
path = "#{FILES_DIR}/#{filename}"
|
|
|
|
content_type = 'image/png'
|
2008-05-19 19:01:42 -04:00
|
|
|
expected = File.read(path)
|
|
|
|
expected.force_encoding(Encoding::BINARY) if expected.respond_to?(:force_encoding)
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
file = ActionController::TestUploadedFile.new(path, content_type)
|
|
|
|
assert_equal filename, file.original_filename
|
|
|
|
assert_equal content_type, file.content_type
|
|
|
|
assert_equal file.path, file.local_path
|
2008-05-19 19:01:42 -04:00
|
|
|
assert_equal expected, file.read
|
2008-08-27 20:57:25 -04:00
|
|
|
|
|
|
|
new_content_type = "new content_type"
|
|
|
|
file.content_type = new_content_type
|
|
|
|
assert_equal new_content_type, file.content_type
|
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
end
|
2008-07-28 14:38:20 -04:00
|
|
|
|
2007-09-14 03:34:45 -04:00
|
|
|
def test_test_uploaded_file_with_binary
|
|
|
|
filename = 'mona_lisa.jpg'
|
|
|
|
path = "#{FILES_DIR}/#{filename}"
|
|
|
|
content_type = 'image/png'
|
2008-07-28 14:38:20 -04:00
|
|
|
|
2007-09-14 03:34:45 -04:00
|
|
|
binary_uploaded_file = ActionController::TestUploadedFile.new(path, content_type, :binary)
|
2008-05-19 19:01:42 -04:00
|
|
|
assert_equal File.open(path, READ_BINARY).read, binary_uploaded_file.read
|
2008-07-28 14:38:20 -04:00
|
|
|
|
2007-09-14 03:34:45 -04:00
|
|
|
plain_uploaded_file = ActionController::TestUploadedFile.new(path, content_type)
|
2008-05-19 19:01:42 -04:00
|
|
|
assert_equal File.open(path, READ_PLAIN).read, plain_uploaded_file.read
|
2007-09-14 03:34:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fixture_file_upload_with_binary
|
|
|
|
filename = 'mona_lisa.jpg'
|
|
|
|
path = "#{FILES_DIR}/#{filename}"
|
|
|
|
content_type = 'image/jpg'
|
2008-07-28 14:38:20 -04:00
|
|
|
|
2007-09-14 03:34:45 -04:00
|
|
|
binary_file_upload = fixture_file_upload(path, content_type, :binary)
|
2008-05-19 19:01:42 -04:00
|
|
|
assert_equal File.open(path, READ_BINARY).read, binary_file_upload.read
|
2008-07-28 14:38:20 -04:00
|
|
|
|
2007-09-14 03:34:45 -04:00
|
|
|
plain_file_upload = fixture_file_upload(path, content_type)
|
2008-05-19 19:01:42 -04:00
|
|
|
assert_equal File.open(path, READ_PLAIN).read, plain_file_upload.read
|
2007-09-14 03:34:45 -04:00
|
|
|
end
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
def test_fixture_file_upload
|
|
|
|
post :test_file_upload, :file => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")
|
2007-04-24 00:12:11 -04:00
|
|
|
assert_equal '159528', @response.body
|
2006-03-18 15:10:52 -05:00
|
|
|
end
|
2006-09-04 00:03:57 -04:00
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
def test_test_uploaded_file_exception_when_file_doesnt_exist
|
|
|
|
assert_raise(RuntimeError) { ActionController::TestUploadedFile.new('non_existent_file') }
|
|
|
|
end
|
2006-03-20 13:16:48 -05:00
|
|
|
|
2006-11-28 18:25:00 -05:00
|
|
|
def test_redirect_url_only_cares_about_location_header
|
|
|
|
get :create
|
|
|
|
assert_response :created
|
|
|
|
|
|
|
|
# Redirect url doesn't care that it wasn't a :redirect response.
|
|
|
|
assert_equal 'created resource', @response.redirect_url
|
|
|
|
assert_equal @response.redirect_url, redirect_to_url
|
|
|
|
|
|
|
|
# Must be a :redirect response.
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
2006-11-28 18:25:00 -05:00
|
|
|
assert_redirected_to 'created resource'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-05 18:23:04 -05:00
|
|
|
def test_binary_content_works_with_send_file
|
|
|
|
get :test_send_file
|
2009-05-02 15:57:40 -04:00
|
|
|
assert_deprecated do
|
|
|
|
assert_nothing_raised(NoMethodError) { @response.binary_content }
|
|
|
|
end
|
2007-12-05 18:23:04 -05:00
|
|
|
end
|
2008-07-28 14:38:20 -04:00
|
|
|
|
2006-05-22 18:59:56 -04:00
|
|
|
protected
|
|
|
|
def with_foo_routing
|
|
|
|
with_routing do |set|
|
2006-06-01 11:42:08 -04:00
|
|
|
set.draw do |map|
|
|
|
|
map.generate_url 'foo', :controller => 'test'
|
|
|
|
map.connect ':controller/:action/:id'
|
2006-05-22 18:59:56 -04:00
|
|
|
end
|
|
|
|
yield set
|
|
|
|
end
|
|
|
|
end
|
2005-03-26 16:43:25 -05:00
|
|
|
end
|
2007-01-28 02:10:35 -05:00
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class InferringClassNameTest < ActionController::TestCase
|
2007-10-25 22:21:21 -04:00
|
|
|
def test_determine_controller_class
|
|
|
|
assert_equal ContentController, determine_class("ContentControllerTest")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_determine_controller_class_with_nonsense_name
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nil determine_class("HelloGoodBye")
|
2007-10-25 22:21:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_determine_controller_class_with_sensible_name_where_no_controller_exists
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nil determine_class("NoControllerWithThisNameTest")
|
2007-10-25 22:21:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def determine_class(name)
|
|
|
|
ActionController::TestCase.determine_default_controller_class(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class CrazyNameTest < ActionController::TestCase
|
|
|
|
tests ContentController
|
2008-01-05 08:34:15 -05:00
|
|
|
|
2007-10-25 22:21:21 -04:00
|
|
|
def test_controller_class_can_be_set_manually_not_just_inferred
|
|
|
|
assert_equal ContentController, self.class.controller_class
|
|
|
|
end
|
|
|
|
end
|
2008-03-08 07:56:41 -05:00
|
|
|
|
|
|
|
class NamedRoutesControllerTest < ActionController::TestCase
|
|
|
|
tests ContentController
|
2008-07-28 14:38:20 -04:00
|
|
|
|
2008-03-08 07:56:41 -05:00
|
|
|
def test_should_be_able_to_use_named_routes_before_a_request_is_done
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw { |map| map.resources :contents }
|
|
|
|
assert_equal 'http://test.host/contents/new', new_content_url
|
2008-10-13 10:08:41 -04:00
|
|
|
assert_equal 'http://test.host/contents/1', content_url(:id => 1)
|
2008-03-08 07:56:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|