aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/test')
-rw-r--r--actionpack/test/controller/content_type_test.rb2
-rw-r--r--actionpack/test/controller/mime_responds_test.rb51
-rw-r--r--actionpack/test/controller/render_js_test.rb6
-rw-r--r--actionpack/test/controller/render_test.rb9
-rw-r--r--actionpack/test/controller/routing_test.rb1027
-rw-r--r--actionpack/test/dispatch/mime_type_test.rb2
-rw-r--r--actionpack/test/dispatch/request/json_params_parsing_test.rb28
-rw-r--r--actionpack/test/dispatch/request/xml_params_parsing_test.rb17
-rw-r--r--actionpack/test/dispatch/request_test.rb63
-rw-r--r--actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.xml.erb (renamed from actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml)0
-rw-r--r--actionpack/test/fixtures/test/_customer_with_var.erb2
-rw-r--r--actionpack/test/fixtures/test/_hash_object.erb2
-rw-r--r--actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb1
-rw-r--r--actionpack/test/lib/fixture_template.rb20
-rw-r--r--actionpack/test/new_base/content_type_test.rb4
-rw-r--r--actionpack/test/new_base/render_layout_test.rb2
-rw-r--r--actionpack/test/new_base/render_rjs_test.rb7
-rw-r--r--actionpack/test/new_base/render_template_test.rb2
-rw-r--r--actionpack/test/template/javascript_helper_test.rb4
-rw-r--r--actionpack/test/template/prototype_helper_test.rb4
-rw-r--r--actionpack/test/template/render_test.rb38
21 files changed, 369 insertions, 922 deletions
diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb
index 511788aec8..c249788c67 100644
--- a/actionpack/test/controller/content_type_test.rb
+++ b/actionpack/test/controller/content_type_test.rb
@@ -46,7 +46,7 @@ class ContentTypeController < ActionController::Base
def render_default_content_types_for_respond_to
respond_to do |format|
format.html { render :text => "hello world!" }
- format.xml { render :action => "render_default_content_types_for_respond_to.rhtml" }
+ format.xml { render :action => "render_default_content_types_for_respond_to" }
format.js { render :text => "hello world!" }
format.rss { render :text => "hello world!", :content_type => Mime::XML }
end
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb
index 2e2dba5aae..3f00b9ba2f 100644
--- a/actionpack/test/controller/mime_responds_test.rb
+++ b/actionpack/test/controller/mime_responds_test.rb
@@ -79,29 +79,20 @@ class RespondToController < ActionController::Base
end
end
- def custom_constant_handling
- Mime::Type.register("text/x-mobile", :mobile)
+ Mime::Type.register("text/x-mobile", :mobile)
+ def custom_constant_handling
respond_to do |type|
type.html { render :text => "HTML" }
type.mobile { render :text => "Mobile" }
end
- ensure
- Mime::SET.delete(:mobile)
- Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
end
def custom_constant_handling_without_block
- Mime::Type.register("text/x-mobile", :mobile)
-
respond_to do |type|
type.html { render :text => "HTML" }
type.mobile
end
-
- ensure
- Mime::SET.delete(:mobile)
- Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
end
def handle_any
@@ -125,32 +116,24 @@ class RespondToController < ActionController::Base
end
end
+ Mime::Type.register_alias("text/html", :iphone)
+
def iphone_with_html_response_type
- Mime::Type.register_alias("text/html", :iphone)
request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
respond_to do |type|
type.html { @type = "Firefox" }
type.iphone { @type = "iPhone" }
end
-
- ensure
- Mime::SET.delete(:iphone)
- Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
end
def iphone_with_html_response_type_without_layout
- Mime::Type.register_alias("text/html", :iphone)
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
respond_to do |type|
type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" }
type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" }
end
-
- ensure
- Mime::SET.delete(:iphone)
- Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
end
def rescue_action(e)
@@ -213,18 +196,20 @@ class RespondToControllerTest < ActionController::TestCase
def test_js_or_html
@request.accept = "text/javascript, text/html"
- get :js_or_html
+ xhr :get, :js_or_html
assert_equal 'JS', @response.body
- get :html_or_xml
+ @request.accept = "text/javascript, text/html"
+ xhr :get, :html_or_xml
assert_equal 'HTML', @response.body
- get :just_xml
+ @request.accept = "text/javascript, text/html"
+ xhr :get, :just_xml
assert_response 406
end
def test_json_or_yaml
- get :json_or_yaml
+ xhr :get, :json_or_yaml
assert_equal 'JSON', @response.body
get :json_or_yaml, :format => 'json'
@@ -246,13 +231,13 @@ class RespondToControllerTest < ActionController::TestCase
def test_js_or_anything
@request.accept = "text/javascript, */*"
- get :js_or_html
+ xhr :get, :js_or_html
assert_equal 'JS', @response.body
- get :html_or_xml
+ xhr :get, :html_or_xml
assert_equal 'HTML', @response.body
- get :just_xml
+ xhr :get, :just_xml
assert_equal 'XML', @response.body
end
@@ -291,14 +276,16 @@ class RespondToControllerTest < ActionController::TestCase
end
def test_with_atom_content_type
+ @request.accept = ""
@request.env["CONTENT_TYPE"] = "application/atom+xml"
- get :made_for_content_type
+ xhr :get, :made_for_content_type
assert_equal "ATOM", @response.body
end
def test_with_rss_content_type
+ @request.accept = ""
@request.env["CONTENT_TYPE"] = "application/rss+xml"
- get :made_for_content_type
+ xhr :get, :made_for_content_type
assert_equal "RSS", @response.body
end
@@ -795,12 +782,8 @@ class PostController < AbstractPostController
protected
def with_iphone
- Mime::Type.register_alias("text/html", :iphone)
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
yield
- ensure
- Mime::SET.delete(:iphone)
- Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
end
end
diff --git a/actionpack/test/controller/render_js_test.rb b/actionpack/test/controller/render_js_test.rb
index d02fd3fd4c..bc850de733 100644
--- a/actionpack/test/controller/render_js_test.rb
+++ b/actionpack/test/controller/render_js_test.rb
@@ -13,7 +13,7 @@ class TestController < ActionController::Base
# let's just rely on the template
end
- def partial
+ def show_partial
render :partial => 'partial'
end
end
@@ -33,7 +33,7 @@ class RenderTest < ActionController::TestCase
end
def test_should_render_js_partial
- xhr :get, :partial, :format => 'js'
+ xhr :get, :show_partial, :format => 'js'
assert_equal 'partial js', @response.body
- end
+ end
end \ No newline at end of file
diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb
index 0c0599679c..9c5b560c2c 100644
--- a/actionpack/test/controller/render_test.rb
+++ b/actionpack/test/controller/render_test.rb
@@ -486,10 +486,6 @@ class TestController < ActionController::Base
render :action => "using_layout_around_block"
end
- def render_using_layout_around_block_with_args
- render :action => "using_layout_around_block_with_args"
- end
-
def render_using_layout_around_block_in_main_layout_and_within_content_for_layout
render :action => "using_layout_around_block", :layout => "layouts/block_with_layout"
end
@@ -1161,11 +1157,6 @@ class RenderTest < ActionController::TestCase
assert_equal "Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n", @response.body
end
- def test_using_layout_around_block_with_args
- get :render_using_layout_around_block_with_args
- assert_equal "Before\narg1arg2\nAfter", @response.body
- end
-
def test_partial_only
get :partial_only
assert_equal "only partial", @response.body
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index 2534c232c7..33fb6ac017 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -9,7 +9,6 @@ class MilestonesController < ActionController::Base
def rescue_action(e) raise e end
end
-RunTimeTests = ARGV.include? 'time'
ROUTING = ActionController::Routing
class ROUTING::RouteBuilder
@@ -60,590 +59,6 @@ class UriReservedCharactersRoutingTest < Test::Unit::TestCase
end
end
-class SegmentTest < Test::Unit::TestCase
- def test_first_segment_should_interpolate_for_structure
- s = ROUTING::Segment.new
- def s.interpolation_statement(array) 'hello' end
- assert_equal 'hello', s.continue_string_structure([])
- end
-
- def test_interpolation_statement
- s = ROUTING::StaticSegment.new("Hello")
- assert_equal "Hello", eval(s.interpolation_statement([]))
- assert_equal "HelloHello", eval(s.interpolation_statement([s]))
-
- s2 = ROUTING::StaticSegment.new("-")
- assert_equal "Hello-Hello", eval(s.interpolation_statement([s, s2]))
-
- s3 = ROUTING::StaticSegment.new("World")
- assert_equal "Hello-World", eval(s3.interpolation_statement([s, s2]))
- end
-end
-
-class StaticSegmentTest < Test::Unit::TestCase
- def test_interpolation_chunk_should_respect_raw
- s = ROUTING::StaticSegment.new('Hello World')
- assert !s.raw?
- assert_equal 'Hello%20World', s.interpolation_chunk
-
- s = ROUTING::StaticSegment.new('Hello World', :raw => true)
- assert s.raw?
- assert_equal 'Hello World', s.interpolation_chunk
- end
-
- def test_value_should_not_be_double_unescaped
- s = ROUTING::StaticSegment.new('%D0%9A%D0%B0%D1%80%D1%82%D0%B0') # Карта
- assert_equal '%D0%9A%D0%B0%D1%80%D1%82%D0%B0', s.interpolation_chunk
- end
-
- def test_regexp_chunk_should_escape_specials
- s = ROUTING::StaticSegment.new('Hello*World')
- assert_equal 'Hello\*World', s.regexp_chunk
-
- s = ROUTING::StaticSegment.new('HelloWorld')
- assert_equal 'HelloWorld', s.regexp_chunk
- end
-
- def test_regexp_chunk_should_add_question_mark_for_optionals
- s = ROUTING::StaticSegment.new("/", :optional => true)
- assert_equal "/?", s.regexp_chunk
-
- s = ROUTING::StaticSegment.new("hello", :optional => true)
- assert_equal "(?:hello)?", s.regexp_chunk
- end
-end
-
-class DynamicSegmentTest < Test::Unit::TestCase
- def segment(options = {})
- unless @segment
- @segment = ROUTING::DynamicSegment.new(:a, options)
- end
- @segment
- end
-
- def test_extract_value
- s = ROUTING::DynamicSegment.new(:a)
-
- hash = {:a => '10', :b => '20'}
- assert_equal '10', eval(s.extract_value)
-
- hash = {:b => '20'}
- assert_equal nil, eval(s.extract_value)
-
- s.default = '20'
- assert_equal '20', eval(s.extract_value)
- end
-
- def test_default_local_name
- assert_equal 'a_value', segment.local_name,
- "Unexpected name -- all value_check tests will fail!"
- end
-
- def test_presence_value_check
- a_value = 10
- assert eval(segment.value_check)
- end
-
- def test_regexp_value_check_rejects_nil
- segment = segment(:regexp => /\d+/)
-
- a_value = nil
- assert !eval(segment.value_check)
- end
-
- def test_optional_regexp_value_check_should_accept_nil
- segment = segment(:regexp => /\d+/, :optional => true)
-
- a_value = nil
- assert eval(segment.value_check)
- end
-
- def test_regexp_value_check_rejects_no_match
- segment = segment(:regexp => /\d+/)
-
- a_value = "Hello20World"
- assert !eval(segment.value_check)
-
- a_value = "20Hi"
- assert !eval(segment.value_check)
- end
-
- def test_regexp_value_check_accepts_match
- segment = segment(:regexp => /\d+/)
- a_value = "30"
- assert eval(segment.value_check)
- end
-
- def test_value_check_fails_on_nil
- a_value = nil
- assert ! eval(segment.value_check)
- end
-
- def test_optional_value_needs_no_check
- segment = segment(:optional => true)
-
- a_value = nil
- assert_equal nil, segment.value_check
- end
-
- def test_regexp_value_check_should_accept_match_with_default
- segment = segment(:regexp => /\d+/, :default => '200')
-
- a_value = '100'
- assert eval(segment.value_check)
- end
-
- def test_expiry_should_not_trigger_once_expired
- expired = true
- hash = merged = {:a => 2, :b => 3}
- options = {:b => 3}
- expire_on = Hash.new { raise 'No!!!' }
-
- eval(segment.expiry_statement)
- rescue RuntimeError
- flunk "Expiry check should not have occurred!"
- end
-
- def test_expiry_should_occur_according_to_expire_on
- expired = false
- hash = merged = {:a => 2, :b => 3}
- options = {:b => 3}
-
- expire_on = {:b => true, :a => false}
- eval(segment.expiry_statement)
- assert !expired
- assert_equal({:a => 2, :b => 3}, hash)
-
- expire_on = {:b => true, :a => true}
- eval(segment.expiry_statement)
- assert expired
- assert_equal({:b => 3}, hash)
- end
-
- def test_extraction_code_should_return_on_nil
- hash = merged = {:b => 3}
- options = {:b => 3}
- a_value = nil
-
- # Local jump because of return inside eval.
- assert_raise(LocalJumpError) { eval(segment.extraction_code) }
- end
-
- def test_extraction_code_should_return_on_mismatch
- segment = segment(:regexp => /\d+/)
- hash = merged = {:a => 'Hi', :b => '3'}
- options = {:b => '3'}
- a_value = nil
-
- # Local jump because of return inside eval.
- assert_raise(LocalJumpError) { eval(segment.extraction_code) }
- end
-
- def test_extraction_code_should_accept_value_and_set_local
- hash = merged = {:a => 'Hi', :b => '3'}
- options = {:b => '3'}
- a_value = nil
- expired = true
-
- eval(segment.extraction_code)
- assert_equal 'Hi', a_value
- end
-
- def test_extraction_should_work_without_value_check
- segment.default = 'hi'
- hash = merged = {:b => '3'}
- options = {:b => '3'}
- a_value = nil
- expired = true
-
- eval(segment.extraction_code)
- assert_equal 'hi', a_value
- end
-
- def test_extraction_code_should_perform_expiry
- expired = false
- hash = merged = {:a => 'Hi', :b => '3'}
- options = {:b => '3'}
- expire_on = {:a => true}
- a_value = nil
-
- eval(segment.extraction_code)
- assert_equal 'Hi', a_value
- assert expired
- assert_equal options, hash
- end
-
- def test_interpolation_chunk_should_replace_value
- a_value = 'Hi'
- assert_equal a_value, eval(%("#{segment.interpolation_chunk}"))
- end
-
- def test_interpolation_chunk_should_accept_nil
- a_value = nil
- assert_equal '', eval(%("#{segment.interpolation_chunk('a_value')}"))
- end
-
- def test_value_regexp_should_be_nil_without_regexp
- assert_equal nil, segment.value_regexp
- end
-
- def test_value_regexp_should_match_exacly
- segment = segment(:regexp => /\d+/)
- assert_no_match segment.value_regexp, "Hello 10 World"
- assert_no_match segment.value_regexp, "Hello 10"
- assert_no_match segment.value_regexp, "10 World"
- assert_match segment.value_regexp, "10"
- end
-
- def test_regexp_chunk_should_return_string
- segment = segment(:regexp => /\d+/)
- assert_kind_of String, segment.regexp_chunk
- end
-
- def test_build_pattern_non_optional_with_no_captures
- # Non optional
- a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /\d+/)
- assert_equal "(\\d+)stuff", a_segment.build_pattern('stuff')
- end
-
- def test_build_pattern_non_optional_with_captures
- # Non optional
- a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /(\d+)(.*?)/)
- assert_equal "((\\d+)(.*?))stuff", a_segment.build_pattern('stuff')
- end
-
- def test_optionality_implied
- a_segment = ROUTING::DynamicSegment.new(:id)
- assert a_segment.optionality_implied?
-
- a_segment = ROUTING::DynamicSegment.new(:action)
- assert a_segment.optionality_implied?
- end
-
- def test_modifiers_must_be_handled_sensibly
- a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/i)
- assert_equal "((?i-mx:david|jamis))stuff", a_segment.build_pattern('stuff')
- a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/x)
- assert_equal "((?x-mi:david|jamis))stuff", a_segment.build_pattern('stuff')
- a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/)
- assert_equal "(david|jamis)stuff", a_segment.build_pattern('stuff')
- end
-end
-
-class ControllerSegmentTest < Test::Unit::TestCase
- def test_regexp_should_only_match_possible_controllers
- ActionController::Routing.with_controllers %w(admin/accounts admin/users account pages) do
- cs = ROUTING::ControllerSegment.new :controller
- regexp = %r{\A#{cs.regexp_chunk}\Z}
-
- ActionController::Routing.possible_controllers.each do |name|
- assert_match regexp, name
- assert_no_match regexp, "#{name}_fake"
-
- match = regexp.match name
- assert_equal name, match[1]
- end
- end
- end
-end
-
-class PathSegmentTest < Test::Unit::TestCase
- def segment(options = {})
- unless @segment
- @segment = ROUTING::PathSegment.new(:path, options)
- end
- @segment
- end
-
- def test_regexp_chunk_should_return_string
- segment = segment(:regexp => /[a-z]+/)
- assert_kind_of String, segment.regexp_chunk
- end
-
- def test_regexp_chunk_should_be_wrapped_with_parenthesis
- segment = segment(:regexp => /[a-z]+/)
- assert_equal "([a-z]+)", segment.regexp_chunk
- end
-
- def test_regexp_chunk_should_respect_options
- segment = segment(:regexp => /[a-z]+/i)
- assert_equal "((?i-mx:[a-z]+))", segment.regexp_chunk
- end
-end
-
-class RouteBuilderTest < Test::Unit::TestCase
- def builder
- @builder ||= ROUTING::RouteBuilder.new
- end
-
- def build(path, options)
- builder.build(path, options)
- end
-
- def test_options_should_not_be_modified
- requirements1 = { :id => /\w+/, :controller => /(?:[a-z](?:-?[a-z]+)*)/ }
- requirements2 = requirements1.dup
-
- assert_equal requirements1, requirements2
-
- with_options(:controller => 'folder',
- :requirements => requirements2) do |m|
- m.build 'folders/new', :action => 'new'
- end
-
- assert_equal requirements1, requirements2
- end
-
- def test_segment_for_static
- segment, rest = builder.segment_for 'ulysses'
- assert_equal '', rest
- assert_kind_of ROUTING::StaticSegment, segment
- assert_equal 'ulysses', segment.value
- end
-
- def test_segment_for_action
- segment, rest = builder.segment_for ':action'
- assert_equal '', rest
- assert_kind_of ROUTING::DynamicSegment, segment
- assert_equal :action, segment.key
- assert_equal 'index', segment.default
- end
-
- def test_segment_for_dynamic
- segment, rest = builder.segment_for ':login'
- assert_equal '', rest
- assert_kind_of ROUTING::DynamicSegment, segment
- assert_equal :login, segment.key
- assert_equal nil, segment.default
- assert ! segment.optional?
- end
-
- def test_segment_for_with_rest
- segment, rest = builder.segment_for ':login/:action'
- assert_equal :login, segment.key
- assert_equal '/:action', rest
- segment, rest = builder.segment_for rest
- assert_equal '/', segment.value
- assert_equal ':action', rest
- segment, rest = builder.segment_for rest
- assert_equal :action, segment.key
- assert_equal '', rest
- end
-
- def test_segments_for
- segments = builder.segments_for_route_path '/:controller/:action/:id'
-
- assert_kind_of ROUTING::DividerSegment, segments[0]
- assert_equal '/', segments[2].value
-
- assert_kind_of ROUTING::DynamicSegment, segments[1]
- assert_equal :controller, segments[1].key
-
- assert_kind_of ROUTING::DividerSegment, segments[2]
- assert_equal '/', segments[2].value
-
- assert_kind_of ROUTING::DynamicSegment, segments[3]
- assert_equal :action, segments[3].key
-
- assert_kind_of ROUTING::DividerSegment, segments[4]
- assert_equal '/', segments[4].value
-
- assert_kind_of ROUTING::DynamicSegment, segments[5]
- assert_equal :id, segments[5].key
- end
-
- def test_segment_for_action
- s, r = builder.segment_for(':action/something/else')
- assert_equal '/something/else', r
- assert_equal :action, s.key
- end
-
- def test_action_default_should_not_trigger_on_prefix
- s, r = builder.segment_for ':action_name/something/else'
- assert_equal '/something/else', r
- assert_equal :action_name, s.key
- assert_equal nil, s.default
- end
-
- def test_divide_route_options
- segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
- defaults, requirements = builder.divide_route_options(segments,
- :action => 'buy', :person => /\w+/, :car => /\w+/,
- :defaults => {:person => nil, :car => nil}
- )
-
- assert_equal({:action => 'buy', :person => nil, :car => nil}, defaults)
- assert_equal({:person => /\w+/, :car => /\w+/}, requirements)
- end
-
- def test_assign_route_options
- segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
- defaults = {:action => 'buy', :person => nil, :car => nil}
- requirements = {:person => /\w+/, :car => /\w+/}
-
- route_requirements = builder.assign_route_options(segments, defaults, requirements)
- assert_equal({}, route_requirements)
-
- assert_equal :action, segments[3].key
- assert_equal 'buy', segments[3].default
-
- assert_equal :person, segments[5].key
- assert_equal %r/\w+/, segments[5].regexp
- assert segments[5].optional?
-
- assert_equal :car, segments[7].key
- assert_equal %r/\w+/, segments[7].regexp
- assert segments[7].optional?
- end
-
- def test_assign_route_options_with_anchor_chars
- segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
- defaults = {:action => 'buy', :person => nil, :car => nil}
- requirements = {:person => /\w+/, :car => /^\w+$/}
-
- assert_raise ArgumentError do
- route_requirements = builder.assign_route_options(segments, defaults, requirements)
- end
-
- requirements[:car] = /[^\/]+/
- route_requirements = builder.assign_route_options(segments, defaults, requirements)
- end
-
- def test_optional_segments_preceding_required_segments
- segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
- defaults = {:action => 'buy', :person => nil, :car => "model-t"}
- assert builder.assign_route_options(segments, defaults, {}).empty?
-
- 0.upto(1) { |i| assert !segments[i].optional?, "segment #{i} is optional and it shouldn't be" }
- assert segments[2].optional?
-
- assert_equal nil, builder.warn_output # should only warn on the :person segment
- end
-
- def test_segmentation_of_dot_path
- segments = builder.segments_for_route_path '/books/:action.rss'
- assert builder.assign_route_options(segments, {}, {}).empty?
- assert_equal 6, segments.length # "/", "books", "/", ":action", ".", "rss"
- assert !segments.any? { |seg| seg.optional? }
- end
-
- def test_segmentation_of_dynamic_dot_path
- segments = builder.segments_for_route_path '/books/:action.:format'
- assert builder.assign_route_options(segments, {}, {}).empty?
- assert_equal 6, segments.length # "/", "books", "/", ":action", ".", ":format"
- assert !segments.any? { |seg| seg.optional? }
- assert_kind_of ROUTING::DynamicSegment, segments.last
- end
-
- def test_assignment_of_default_options
- segments = builder.segments_for_route_path '/:controller/:action/:id/'
- action, id = segments[-4], segments[-2]
-
- assert_equal :action, action.key
- assert_equal :id, id.key
- assert ! action.optional?
- assert ! id.optional?
-
- builder.assign_default_route_options(segments)
-
- assert_equal 'index', action.default
- assert action.optional?
- assert id.optional?
- end
-
- def test_assignment_of_default_options_respects_existing_defaults
- segments = builder.segments_for_route_path '/:controller/:action/:id/'
- action, id = segments[-4], segments[-2]
-
- assert_equal :action, action.key
- assert_equal :id, id.key
- action.default = 'show'
- action.is_optional = true
-
- id.default = 'Welcome'
- id.is_optional = true
-
- builder.assign_default_route_options(segments)
-
- assert_equal 'show', action.default
- assert action.optional?
- assert_equal 'Welcome', id.default
- assert id.optional?
- end
-
- def test_assignment_of_default_options_respects_regexps
- segments = builder.segments_for_route_path '/:controller/:action/:id/'
- action = segments[-4]
-
- assert_equal :action, action.key
- segments[-4] = ROUTING::DynamicSegment.new(:action, :regexp => /show|in/)
-
- builder.assign_default_route_options(segments)
-
- assert_equal nil, action.default
- assert ! action.optional?
- end
-
- def test_assignment_of_is_optional_when_default
- segments = builder.segments_for_route_path '/books/:action.rss'
- assert_equal segments[3].key, :action
- segments[3].default = 'changes'
- builder.ensure_required_segments(segments)
- assert ! segments[3].optional?
- end
-
- def test_is_optional_is_assigned_to_default_segments
- segments = builder.segments_for_route_path '/books/:action'
- builder.assign_route_options(segments, {:action => 'index'}, {})
-
- assert_equal segments[3].key, :action
- assert segments[3].optional?
- assert_kind_of ROUTING::DividerSegment, segments[2]
- assert segments[2].optional?
- end
-
- # XXX is optional not being set right?
- # /blah/:defaulted_segment <-- is the second slash optional? it should be.
-
- def test_route_build
- ActionController::Routing.with_controllers %w(users pages) do
- r = builder.build '/:controller/:action/:id/', :action => nil
-
- [0, 2, 4].each do |i|
- assert_kind_of ROUTING::DividerSegment, r.segments[i]
- assert_equal '/', r.segments[i].value
- assert r.segments[i].optional? if i > 1
- end
-
- assert_kind_of ROUTING::DynamicSegment, r.segments[1]
- assert_equal :controller, r.segments[1].key
- assert_equal nil, r.segments[1].default
-
- assert_kind_of ROUTING::DynamicSegment, r.segments[3]
- assert_equal :action, r.segments[3].key
- assert_equal 'index', r.segments[3].default
-
- assert_kind_of ROUTING::DynamicSegment, r.segments[5]
- assert_equal :id, r.segments[5].key
- assert r.segments[5].optional?
- end
- end
-
- def test_slashes_are_implied
- routes = [
- builder.build('/:controller/:action/:id/', :action => nil),
- builder.build('/:controller/:action/:id', :action => nil),
- builder.build(':controller/:action/:id', :action => nil),
- builder.build('/:controller/:action/:id/', :action => nil)
- ]
- expected = routes.first.segments.length
- routes.each_with_index do |route, i|
- found = route.segments.length
- assert_equal expected, found, "Route #{i + 1} has #{found} segments, expected #{expected}"
- end
- end
-end
-
class RoutingTest < Test::Unit::TestCase
def test_possible_controllers
true_controller_paths = ActionController::Routing.controller_paths
@@ -759,7 +174,7 @@ class LegacyRouteSetTests < Test::Unit::TestCase
ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed)
end
-
+
def teardown
@rs.clear!
end
@@ -815,52 +230,6 @@ class LegacyRouteSetTests < Test::Unit::TestCase
map.resources :pages
map.connect ':controller/:action/:id'
}
- n = 1000
- if RunTimeTests
- GC.start
- rectime = Benchmark.realtime do
- n.times do
- rs.recognize_path("/videos/1234567", {:method => :get})
- rs.recognize_path("/videos/1234567/abuse", {:method => :get})
- rs.recognize_path("/users/1234567/settings", {:method => :get})
- rs.recognize_path("/channels/1234567", {:method => :get})
- rs.recognize_path("/session/new", {:method => :get})
- rs.recognize_path("/admin/user/show/10", {:method => :get})
- end
- end
- puts "\n\nRecognition (#{rs.routes.size} routes):"
- per_url = rectime / (n * 6)
- puts "#{per_url * 1000} ms/url"
- puts "#{1 / per_url} url/s\n\n"
- end
- end
-
- def test_time_generation
- n = 5000
- if RunTimeTests
- GC.start
- pairs = [
- [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
- [{:controller => 'content'}, {:controller => 'content', :action => 'index'}],
- [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
- [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
- [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
- [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
- [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
- [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
- ]
- p = nil
- gentime = Benchmark.realtime do
- n.times do
- pairs.each {|(a, b)| rs.generate(a, b)}
- end
- end
-
- puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
- per_url = gentime / (n * 8)
- puts "#{per_url * 1000} ms/url"
- puts "#{1 / per_url} url/s\n\n"
- end
end
def test_route_with_colon_first
@@ -1138,8 +507,7 @@ class LegacyRouteSetTests < Test::Unit::TestCase
map.connect '*path', :controller => 'content', :action => 'show_file'
end
- recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo))
- assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path)
+ assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => %w(pages boo))
end
def test_backwards
@@ -1455,161 +823,6 @@ class LegacyRouteSetTests < Test::Unit::TestCase
end
end
-class RouteTest < Test::Unit::TestCase
- def setup
- @route = ROUTING::Route.new
- end
-
- def slash_segment(is_optional = false)
- ROUTING::DividerSegment.new('/', :optional => is_optional)
- end
-
- def default_route
- unless defined?(@default_route)
- segments = []
- segments << ROUTING::StaticSegment.new('/', :raw => true)
- segments << ROUTING::DynamicSegment.new(:controller)
- segments << slash_segment(:optional)
- segments << ROUTING::DynamicSegment.new(:action, :default => 'index', :optional => true)
- segments << slash_segment(:optional)
- segments << ROUTING::DynamicSegment.new(:id, :optional => true)
- segments << slash_segment(:optional)
- @default_route = ROUTING::Route.new(segments).freeze
- end
- @default_route
- end
-
- def test_default_route_recognition
- expected = {:controller => 'accounts', :action => 'show', :id => '10'}
- assert_equal expected, default_route.recognize('/accounts/show/10')
- assert_equal expected, default_route.recognize('/accounts/show/10/')
-
- expected[:id] = 'jamis'
- assert_equal expected, default_route.recognize('/accounts/show/jamis/')
-
- expected.delete :id
- assert_equal expected, default_route.recognize('/accounts/show')
- assert_equal expected, default_route.recognize('/accounts/show/')
-
- expected[:action] = 'index'
- assert_equal expected, default_route.recognize('/accounts/')
- assert_equal expected, default_route.recognize('/accounts')
-
- assert_equal nil, default_route.recognize('/')
- assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free')
- end
-
- def test_default_route_should_omit_default_action
- o = {:controller => 'accounts', :action => 'index'}
- assert_equal '/accounts', default_route.generate(o, o, {})
- end
-
- def test_default_route_should_include_default_action_when_id_present
- o = {:controller => 'accounts', :action => 'index', :id => '20'}
- assert_equal '/accounts/index/20', default_route.generate(o, o, {})
- end
-
- def test_default_route_should_work_with_action_but_no_id
- o = {:controller => 'accounts', :action => 'list_all'}
- assert_equal '/accounts/list_all', default_route.generate(o, o, {})
- end
-
- def test_default_route_should_uri_escape_pluses
- expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' }
- assert_equal expected, default_route.recognize('/accounts/show/hello world')
- assert_equal expected, default_route.recognize('/accounts/show/hello%20world')
- assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {})
-
- expected[:id] = 'hello+world'
- assert_equal expected, default_route.recognize('/accounts/show/hello+world')
- assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld')
- assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {})
- end
-
- def test_matches_controller_and_action
- # requirement_for should only be called for the action and controller _once_
- @route.expects(:requirement_for).with(:controller).times(1).returns('pages')
- @route.expects(:requirement_for).with(:action).times(1).returns('show')
-
- @route.requirements = {:controller => 'pages', :action => 'show'}
- assert @route.matches_controller_and_action?('pages', 'show')
- assert !@route.matches_controller_and_action?('not_pages', 'show')
- assert !@route.matches_controller_and_action?('pages', 'not_show')
- end
-
- def test_parameter_shell
- page_url = ROUTING::Route.new
- page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/}
- assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell)
- end
-
- def test_defaults
- route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html"
- assert_equal(
- { :controller => "users", :action => "show", :format => "html" },
- route.defaults)
- end
-
- def test_builder_complains_without_controller
- assert_raise(ArgumentError) do
- ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index"
- end
- end
-
- def test_significant_keys_for_default_route
- keys = default_route.significant_keys.sort_by {|k| k.to_s }
- assert_equal [:action, :controller, :id], keys
- end
-
- def test_significant_keys
- segments = []
- segments << ROUTING::StaticSegment.new('/', :raw => true)
- segments << ROUTING::StaticSegment.new('user')
- segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
- segments << ROUTING::DynamicSegment.new(:user)
- segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
-
- requirements = {:controller => 'users', :action => 'show'}
-
- user_url = ROUTING::Route.new(segments, requirements)
- keys = user_url.significant_keys.sort_by { |k| k.to_s }
- assert_equal [:action, :controller, :user], keys
- end
-
- def test_build_empty_query_string
- assert_equal '', @route.build_query_string({})
- end
-
- def test_build_query_string_with_nil_value
- assert_equal '', @route.build_query_string({:x => nil})
- end
-
- def test_simple_build_query_string
- assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2'))
- end
-
- def test_convert_ints_build_query_string
- assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2))
- end
-
- def test_escape_spaces_build_query_string
- assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world'))
- end
-
- def test_expand_array_build_query_string
- assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2]))
- end
-
- def test_escape_spaces_build_query_string_selected_keys
- assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x]))
- end
-
- private
- def order_query_string(qs)
- '?' + qs[1..-1].split('&').sort.join('&')
- end
-end
-
class RouteSetTest < ActiveSupport::TestCase
def set
@set ||= ROUTING::RouteSet.new
@@ -1619,6 +832,19 @@ class RouteSetTest < ActiveSupport::TestCase
@request ||= ActionController::TestRequest.new
end
+ def default_route_set
+ @default_route_set ||= begin
+ set = nil
+ ActionController::Routing.with_controllers(['accounts']) do
+ set = ROUTING::RouteSet.new
+ set.draw do |map|
+ map.connect '/:controller/:action/:id/'
+ end
+ end
+ set
+ end
+ end
+
def test_generate_extras
set.draw { |m| m.connect ':controller/:action/:id' }
path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
@@ -2185,18 +1411,6 @@ class RouteSetTest < ActiveSupport::TestCase
Object.send(:remove_const, :Api)
end
- def test_generate_finds_best_fit
- set.draw do |map|
- map.connect "/people", :controller => "people", :action => "index"
- map.connect "/ws/people", :controller => "people", :action => "index", :ws => true
- end
-
- assert_deprecated {
- url = set.generate(:controller => "people", :action => "index", :ws => true)
- assert_equal "/ws/people", url
- }
- end
-
def test_generate_changes_controller_module
set.draw { |map| map.connect ':controller/:action/:id' }
current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
@@ -2504,6 +1718,213 @@ class RouteSetTest < ActiveSupport::TestCase
assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, set.recognize_path('/named'))
end
+
+ def test_interpolation_chunk_should_respect_raw
+ ActionController::Routing.with_controllers(['hello']) do
+ set.draw do |map|
+ map.connect '/Hello World', :controller => 'hello'
+ end
+
+ assert_equal '/Hello%20World', set.generate(:controller => 'hello')
+ assert_equal({:controller => "hello", :action => "index"}, set.recognize_path('/Hello World'))
+ assert_raise(ActionController::RoutingError) { set.recognize_path('/Hello%20World') }
+ end
+ end
+
+ def test_value_should_not_be_double_unescaped
+ ActionController::Routing.with_controllers(['foo']) do
+ set.draw do |map|
+ map.connect '/Карта', :controller => 'foo'
+ end
+
+ assert_equal '/%D0%9A%D0%B0%D1%80%D1%82%D0%B0', set.generate(:controller => 'foo')
+ assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/Карта'))
+ assert_raise(ActionController::RoutingError) { set.recognize_path('/%D0%9A%D0%B0%D1%80%D1%82%D0%B0') }
+ end
+ end
+
+ def test_regexp_chunk_should_escape_specials
+ ActionController::Routing.with_controllers(['foo', 'bar']) do
+ set.draw do |map|
+ map.connect '/Hello*World', :controller => 'foo'
+ map.connect '/HelloWorld', :controller => 'bar'
+ end
+
+ assert_equal '/Hello*World', set.generate(:controller => 'foo')
+ assert_equal '/HelloWorld', set.generate(:controller => 'bar')
+
+ assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/Hello*World'))
+ assert_equal({:controller => "bar", :action => "index"}, set.recognize_path('/HelloWorld'))
+ end
+ end
+
+ def test_regexp_chunk_should_add_question_mark_for_optionals
+ ActionController::Routing.with_controllers(['foo', 'bar']) do
+ set.draw do |map|
+ map.connect '/', :controller => 'foo'
+ map.connect '/hello', :controller => 'bar'
+ end
+
+ assert_equal '/', set.generate(:controller => 'foo')
+ assert_equal '/hello', set.generate(:controller => 'bar')
+
+ assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/'))
+ assert_equal({:controller => "bar", :action => "index"}, set.recognize_path('/hello'))
+ end
+ end
+
+ def test_assign_route_options_with_anchor_chars
+ ActionController::Routing.with_controllers(['cars']) do
+ set.draw do |map|
+ map.connect '/cars/:action/:person/:car/', :controller => 'cars'
+ end
+
+ assert_equal '/cars/buy/1/2', set.generate(:controller => 'cars', :action => 'buy', :person => '1', :car => '2')
+
+ assert_equal({:controller => "cars", :action => "buy", :person => "1", :car => "2"}, set.recognize_path('/cars/buy/1/2'))
+ end
+ end
+
+ def test_segmentation_of_dot_path
+ ActionController::Routing.with_controllers(['books']) do
+ set.draw do |map|
+ map.connect '/books/:action.rss', :controller => 'books'
+ end
+
+ assert_equal '/books/list.rss', set.generate(:controller => 'books', :action => 'list')
+
+ assert_equal({:controller => "books", :action => "list"}, set.recognize_path('/books/list.rss'))
+ end
+ end
+
+ def test_segmentation_of_dynamic_dot_path
+ ActionController::Routing.with_controllers(['books']) do
+ set.draw do |map|
+ map.connect '/books/:action.:format', :controller => 'books'
+ end
+
+ assert_equal '/books/list.rss', set.generate(:controller => 'books', :action => 'list', :format => 'rss')
+ assert_equal '/books/list.xml', set.generate(:controller => 'books', :action => 'list', :format => 'xml')
+ assert_equal '/books/list', set.generate(:controller => 'books', :action => 'list')
+ assert_equal '/books', set.generate(:controller => 'books', :action => 'index')
+
+ assert_equal({:controller => "books", :action => "list", :format => "rss"}, set.recognize_path('/books/list.rss'))
+ assert_equal({:controller => "books", :action => "list", :format => "xml"}, set.recognize_path('/books/list.xml'))
+ assert_equal({:controller => "books", :action => "list"}, set.recognize_path('/books/list'))
+ assert_equal({:controller => "books", :action => "index"}, set.recognize_path('/books'))
+ end
+ end
+
+ def test_slashes_are_implied
+ ActionController::Routing.with_controllers(['foo']) do
+ ['/:controller/:action/:id/', '/:controller/:action/:id',
+ ':controller/:action/:id', '/:controller/:action/:id/'
+ ].each do |path|
+ @set = nil
+ set.draw { |map| map.connect(path) }
+
+ assert_equal '/foo', set.generate(:controller => 'foo', :action => 'index')
+ assert_equal '/foo/list', set.generate(:controller => 'foo', :action => 'list')
+ assert_equal '/foo/show/1', set.generate(:controller => 'foo', :action => 'show', :id => '1')
+
+ assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/foo'))
+ assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/foo/index'))
+ assert_equal({:controller => "foo", :action => "list"}, set.recognize_path('/foo/list'))
+ assert_equal({:controller => "foo", :action => "show", :id => "1"}, set.recognize_path('/foo/show/1'))
+ end
+ end
+ end
+
+ def test_default_route_recognition
+ expected = {:controller => 'accounts', :action => 'show', :id => '10'}
+ assert_equal expected, default_route_set.recognize_path('/accounts/show/10')
+ assert_equal expected, default_route_set.recognize_path('/accounts/show/10/')
+
+ expected[:id] = 'jamis'
+ assert_equal expected, default_route_set.recognize_path('/accounts/show/jamis/')
+
+ expected.delete :id
+ assert_equal expected, default_route_set.recognize_path('/accounts/show')
+ assert_equal expected, default_route_set.recognize_path('/accounts/show/')
+
+ expected[:action] = 'index'
+ assert_equal expected, default_route_set.recognize_path('/accounts/')
+ assert_equal expected, default_route_set.recognize_path('/accounts')
+
+ assert_raise(ActionController::RoutingError) { default_route_set.recognize_path('/') }
+ assert_raise(ActionController::RoutingError) { default_route_set.recognize_path('/accounts/how/goood/it/is/to/be/free') }
+ end
+
+ def test_default_route_should_omit_default_action
+ assert_equal '/accounts', default_route_set.generate({:controller => 'accounts', :action => 'index'})
+ end
+
+ def test_default_route_should_include_default_action_when_id_present
+ assert_equal '/accounts/index/20', default_route_set.generate({:controller => 'accounts', :action => 'index', :id => '20'})
+ end
+
+ def test_default_route_should_work_with_action_but_no_id
+ assert_equal '/accounts/list_all', default_route_set.generate({:controller => 'accounts', :action => 'list_all'})
+ end
+
+ def test_default_route_should_uri_escape_pluses
+ expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' }
+ assert_equal expected, default_route_set.recognize_path('/accounts/show/hello world')
+ assert_equal expected, default_route_set.recognize_path('/accounts/show/hello%20world')
+ assert_equal '/accounts/show/hello%20world', default_route_set.generate(expected, expected)
+
+ expected[:id] = 'hello+world'
+ assert_equal expected, default_route_set.recognize_path('/accounts/show/hello+world')
+ assert_equal expected, default_route_set.recognize_path('/accounts/show/hello%2Bworld')
+ assert_equal '/accounts/show/hello+world', default_route_set.generate(expected, expected)
+ end
+
+ def test_parameter_shell
+ page_url = ROUTING::Route.new
+ page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/}
+ assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell)
+ end
+
+ def test_defaults
+ route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html"
+ assert_equal(
+ { :controller => "users", :action => "show", :format => "html" },
+ route.defaults)
+ end
+
+ def test_builder_complains_without_controller
+ assert_raise(ArgumentError) do
+ ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index"
+ end
+ end
+
+ def test_build_empty_query_string
+ assert_equal '/foo', default_route_set.generate({:controller => 'foo'})
+ end
+
+ def test_build_query_string_with_nil_value
+ assert_equal '/foo', default_route_set.generate({:controller => 'foo', :x => nil})
+ end
+
+ def test_simple_build_query_string
+ assert_equal '/foo?x=1&y=2', default_route_set.generate({:controller => 'foo', :x => '1', :y => '2'})
+ end
+
+ def test_convert_ints_build_query_string
+ assert_equal '/foo?x=1&y=2', default_route_set.generate({:controller => 'foo', :x => 1, :y => 2})
+ end
+
+ def test_escape_spaces_build_query_string
+ assert_equal '/foo?x=hello+world&y=goodbye+world', default_route_set.generate({:controller => 'foo', :x => 'hello world', :y => 'goodbye world'})
+ end
+
+ def test_expand_array_build_query_string
+ assert_equal '/foo?x%5B%5D=1&x%5B%5D=2', default_route_set.generate({:controller => 'foo', :x => [1, 2]})
+ end
+
+ def test_escape_spaces_build_query_string_selected_keys
+ assert_equal '/foo?x=hello+world', default_route_set.generate({:controller => 'foo', :x => 'hello world'})
+ end
end
class RouteLoadingTest < Test::Unit::TestCase
@@ -2567,10 +1988,10 @@ class RouteLoadingTest < Test::Unit::TestCase
routes.reload
end
-
+
def test_load_multiple_configurations
routes.add_configuration_file("engines.rb")
-
+
File.expects(:stat).at_least_once.returns(@stat)
routes.expects(:load).with('./config/routes.rb')
diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb
index 4ea0fedb8f..0832943d4c 100644
--- a/actionpack/test/dispatch/mime_type_test.rb
+++ b/actionpack/test/dispatch/mime_type_test.rb
@@ -56,7 +56,7 @@ class MimeTypeTest < ActiveSupport::TestCase
test "type convenience methods" do
# Don't test Mime::ALL, since it Mime::ALL#html? == true
- types = Mime::SET.symbols.uniq - [:all]
+ types = Mime::SET.symbols.uniq - [:all, :iphone]
# Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE
types.delete_if { |type| !Mime.const_defined?(type.to_s.upcase) }
diff --git a/actionpack/test/dispatch/request/json_params_parsing_test.rb b/actionpack/test/dispatch/request/json_params_parsing_test.rb
index a3dde72c4e..db6cf7b330 100644
--- a/actionpack/test/dispatch/request/json_params_parsing_test.rb
+++ b/actionpack/test/dispatch/request/json_params_parsing_test.rb
@@ -30,16 +30,36 @@ class JsonParamsParsingTest < ActionController::IntegrationTest
)
end
+ test "logs error if parsing unsuccessful" do
+ with_test_routing do
+ begin
+ $stderr = StringIO.new
+ json = "[\"person]\": {\"name\": \"David\"}}"
+ post "/parse", json, {'CONTENT_TYPE' => 'application/json'}
+ assert_response :error
+ $stderr.rewind && err = $stderr.read
+ assert err =~ /Error occurred while parsing request parameters/
+ ensure
+ $stderr = STDERR
+ end
+ end
+ end
+
private
def assert_parses(expected, actual, headers = {})
+ with_test_routing do
+ post "/parse", actual, headers
+ assert_response :ok
+ assert_equal(expected, TestController.last_request_parameters)
+ end
+ end
+
+ def with_test_routing
with_routing do |set|
set.draw do |map|
map.connect ':action', :controller => "json_params_parsing_test/test"
end
-
- post "/parse", actual, headers
- assert_response :ok
- assert_equal(expected, TestController.last_request_parameters)
+ yield
end
end
end
diff --git a/actionpack/test/dispatch/request/xml_params_parsing_test.rb b/actionpack/test/dispatch/request/xml_params_parsing_test.rb
index ee764e726e..521002b519 100644
--- a/actionpack/test/dispatch/request/xml_params_parsing_test.rb
+++ b/actionpack/test/dispatch/request/xml_params_parsing_test.rb
@@ -38,6 +38,21 @@ class XmlParamsParsingTest < ActionController::IntegrationTest
end
end
+ test "logs error if parsing unsuccessful" do
+ with_test_routing do
+ begin
+ $stderr = StringIO.new
+ xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></pineapple>"
+ post "/parse", xml, default_headers
+ assert_response :error
+ $stderr.rewind && err = $stderr.read
+ assert err =~ /Error occurred while parsing request parameters/
+ ensure
+ $stderr = STDERR
+ end
+ end
+ end
+
test "parses multiple files" do
xml = <<-end_body
<person>
@@ -85,4 +100,4 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest
def default_headers
{'HTTP_X_POST_DATA_FORMAT' => 'xml'}
end
-end
+end \ No newline at end of file
diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb
index b626063df4..239fda98e0 100644
--- a/actionpack/test/dispatch/request_test.rb
+++ b/actionpack/test/dispatch/request_test.rb
@@ -366,12 +366,12 @@ class RequestTest < ActiveSupport::TestCase
end
test "XMLHttpRequest" do
- with_accept_header false do
- request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
- request.expects(:parameters).at_least_once.returns({})
- assert request.xhr?
- assert_equal Mime::JS, request.format
- end
+ request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
+ 'HTTP_ACCEPT' =>
+ [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(",")
+ request.expects(:parameters).at_least_once.returns({})
+ assert request.xhr?
+ assert_equal Mime::JS, request.format
end
test "content type" do
@@ -420,37 +420,34 @@ class RequestTest < ActiveSupport::TestCase
end
test "formats with accept header" do
- with_accept_header true do
- request = stub_request 'HTTP_ACCEPT' => 'text/html'
- request.expects(:parameters).at_least_once.returns({})
- assert_equal [ Mime::HTML ], request.formats
-
- request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
- request.expects(:parameters).at_least_once.returns({})
- assert_equal with_set(Mime::XML, Mime::HTML, Mime::ALL), request.formats
- end
+ request = stub_request 'HTTP_ACCEPT' => 'text/html'
+ request.expects(:parameters).at_least_once.returns({})
+ assert_equal [ Mime::HTML ], request.formats
- with_accept_header false do
- request = stub_request
- request.expects(:parameters).at_least_once.returns({ :format => :txt })
- assert_equal with_set(Mime::TEXT), request.formats
- end
+ request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8',
+ 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
+ request.expects(:parameters).at_least_once.returns({})
+ assert_equal with_set(Mime::XML), request.formats
+
+ request = stub_request
+ request.expects(:parameters).at_least_once.returns({ :format => :txt })
+ assert_equal with_set(Mime::TEXT), request.formats
end
test "negotiate_mime" do
- with_accept_header true do
- request = stub_request 'HTTP_ACCEPT' => 'text/html'
- request.expects(:parameters).at_least_once.returns({})
-
- assert_equal nil, request.negotiate_mime([Mime::XML, Mime::JSON])
- assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::HTML])
- assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::ALL])
-
- request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
- request.expects(:parameters).at_least_once.returns({})
- assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV])
- assert_equal Mime::CSV, request.negotiate_mime([Mime::CSV, Mime::YAML])
- end
+ request = stub_request 'HTTP_ACCEPT' => 'text/html',
+ 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
+
+ request.expects(:parameters).at_least_once.returns({})
+
+ assert_equal nil, request.negotiate_mime([Mime::XML, Mime::JSON])
+ assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::HTML])
+ assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::ALL])
+
+ request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8',
+ 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
+ request.expects(:parameters).at_least_once.returns({})
+ assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV])
end
protected
diff --git a/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml b/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.xml.erb
index 25dc746886..25dc746886 100644
--- a/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml
+++ b/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.xml.erb
diff --git a/actionpack/test/fixtures/test/_customer_with_var.erb b/actionpack/test/fixtures/test/_customer_with_var.erb
index c28824936b..00047dd20e 100644
--- a/actionpack/test/fixtures/test/_customer_with_var.erb
+++ b/actionpack/test/fixtures/test/_customer_with_var.erb
@@ -1 +1 @@
-<%= customer.name %> <%= customer.name %> <%= customer_with_var.name %> \ No newline at end of file
+<%= customer.name %> <%= customer.name %> <%= customer.name %> \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/_hash_object.erb b/actionpack/test/fixtures/test/_hash_object.erb
index 55c03afb27..34a92c6a56 100644
--- a/actionpack/test/fixtures/test/_hash_object.erb
+++ b/actionpack/test/fixtures/test/_hash_object.erb
@@ -1,2 +1,2 @@
<%= hash_object[:first_name] %>
-<%= object[:first_name].reverse %>
+<%= hash_object[:first_name].reverse %>
diff --git a/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb b/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb
deleted file mode 100644
index 71b1f30ad0..0000000000
--- a/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<% render(:layout => "layout_for_block_with_args") do |*args| %><%= args.join %><% end %> \ No newline at end of file
diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb
index ee526b5de5..8da92180d1 100644
--- a/actionpack/test/lib/fixture_template.rb
+++ b/actionpack/test/lib/fixture_template.rb
@@ -4,7 +4,7 @@ module ActionView #:nodoc:
super(options)
@hash = hash
end
-
+
def find_templates(name, details, prefix, partial)
if regexp = details_to_regexp(name, details, prefix, partial)
cached(regexp) do
@@ -16,26 +16,26 @@ module ActionView #:nodoc:
end
end
end
-
+
private
-
+
def formats_regexp
@formats_regexp ||= begin
formats = Mime::SET.symbols
'(?:' + formats.map { |l| "\\.#{Regexp.escape(l.to_s)}" }.join('|') + ')?'
end
end
-
+
def handler_regexp
e = TemplateHandlers.extensions.map{|h| "\\.#{Regexp.escape(h.to_s)}"}.join("|")
- "(?:#{e})?"
+ "(?:#{e})"
end
-
+
def details_to_regexp(name, details, prefix, partial)
path = ""
path << "#{prefix}/" unless prefix.empty?
path << (partial ? "_#{name}" : name)
-
+
extensions = ""
[:locales, :formats].each do |k|
extensions << if exts = details[k]
@@ -47,7 +47,7 @@ module ActionView #:nodoc:
%r'^#{Regexp.escape(path)}#{extensions}#{handler_regexp}$'
end
-
+
# TODO: fix me
# :api: plugin
def path_to_details(path)
@@ -56,10 +56,10 @@ module ActionView #:nodoc:
partial = m[1] == '_'
details = (m[2]||"").split('.').reject { |e| e.empty? }
handler = Template.handler_class_for_extension(m[3])
-
+
format = Mime[details.last] && details.pop.to_sym
locale = details.last && details.pop.to_sym
-
+
return handler, :format => format, :locale => locale, :partial => partial
end
end
diff --git a/actionpack/test/new_base/content_type_test.rb b/actionpack/test/new_base/content_type_test.rb
index cfc03a3024..ceee508224 100644
--- a/actionpack/test/new_base/content_type_test.rb
+++ b/actionpack/test/new_base/content_type_test.rb
@@ -75,7 +75,7 @@ module ContentType
end
test "sets Content-Type as application/xml when rendering *.xml.erb" do
- get "/content_type/implied/i_am_xml_erb"
+ get "/content_type/implied/i_am_xml_erb", "format" => "xml"
assert_header "Content-Type", "application/xml; charset=utf-8"
end
@@ -87,7 +87,7 @@ module ContentType
end
test "sets Content-Type as application/xml when rendering *.xml.builder" do
- get "/content_type/implied/i_am_xml_builder"
+ get "/content_type/implied/i_am_xml_builder", "format" => "xml"
assert_header "Content-Type", "application/xml; charset=utf-8"
end
diff --git a/actionpack/test/new_base/render_layout_test.rb b/actionpack/test/new_base/render_layout_test.rb
index 279b807a5f..933eef58e7 100644
--- a/actionpack/test/new_base/render_layout_test.rb
+++ b/actionpack/test/new_base/render_layout_test.rb
@@ -83,7 +83,7 @@ module ControllerLayouts
testing ControllerLayouts::MismatchFormatController
test "if JS is selected, an HTML template is not also selected" do
- get :index
+ get :index, "format" => "js"
assert_response "$(\"test\").omg();"
end
diff --git a/actionpack/test/new_base/render_rjs_test.rb b/actionpack/test/new_base/render_rjs_test.rb
index bd4c87b3bf..3d3e516905 100644
--- a/actionpack/test/new_base/render_rjs_test.rb
+++ b/actionpack/test/new_base/render_rjs_test.rb
@@ -21,24 +21,23 @@ module RenderRjs
def index_locale
old_locale, I18n.locale = I18n.locale, :da
end
-
end
class TestBasic < SimpleRouteCase
testing BasicController
test "rendering a partial in an RJS template should pick the JS template over the HTML one" do
- get :index
+ get :index, "format" => "js"
assert_response("$(\"customer\").update(\"JS Partial\");")
end
test "replacing an element with a partial in an RJS template should pick the HTML template over the JS one" do
- get :index_html
+ get :index_html, "format" => "js"
assert_response("$(\"customer\").update(\"HTML Partial\");")
end
test "replacing an element with a partial in an RJS template with a locale should pick the localed HTML template" do
- get :index_locale, :format => :js
+ get :index_locale, "format" => "js"
assert_response("$(\"customer\").update(\"Danish HTML Partial\");")
end
diff --git a/actionpack/test/new_base/render_template_test.rb b/actionpack/test/new_base/render_template_test.rb
index 94ea38fc7b..967cbd07b0 100644
--- a/actionpack/test/new_base/render_template_test.rb
+++ b/actionpack/test/new_base/render_template_test.rb
@@ -73,7 +73,7 @@ module RenderTemplate
end
test "rendering a builder template" do
- get :builder_template
+ get :builder_template, "format" => "xml"
assert_response "<html>\n <p>Hello</p>\n</html>\n"
end
end
diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb
index 8caabfc3e1..f0f686f6e2 100644
--- a/actionpack/test/template/javascript_helper_test.rb
+++ b/actionpack/test/template/javascript_helper_test.rb
@@ -7,6 +7,10 @@ class JavaScriptHelperTest < ActionView::TestCase
attr_accessor :formats, :output_buffer
+ def reset_formats(format)
+ @format = format
+ end
+
def setup
super
@template = self
diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb
index acbf311212..313a769088 100644
--- a/actionpack/test/template/prototype_helper_test.rb
+++ b/actionpack/test/template/prototype_helper_test.rb
@@ -36,6 +36,10 @@ class Author::Nested < Author; end
class PrototypeHelperBaseTest < ActionView::TestCase
attr_accessor :formats, :output_buffer
+ def reset_formats(format)
+ @format = format
+ end
+
def setup
super
@template = self
diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb
index 7f30ae88a1..c86d5215cd 100644
--- a/actionpack/test/template/render_test.rb
+++ b/actionpack/test/template/render_test.rb
@@ -2,10 +2,14 @@
require 'abstract_unit'
require 'controller/fake_models'
+class TestController < ActionController::Base
+end
+
module RenderTestCases
def setup_view(paths)
@assigns = { :secret => 'in the sauce' }
@view = ActionView::Base.new(paths, @assigns)
+ @controller_view = ActionView::Base.for_controller(TestController.new)
# Reload and register danish language for testing
I18n.reload!
@@ -138,7 +142,7 @@ module RenderTestCases
end
def test_render_partial_collection_without_as
- assert_equal "local_inspector,local_inspector_counter,object",
+ assert_equal "local_inspector,local_inspector_counter",
@view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ])
end
@@ -158,6 +162,25 @@ module RenderTestCases
assert_nil @view.render(:partial => [])
end
+ def test_render_partial_using_string
+ assert_equal "Hello: Anonymous", @controller_view.render('customer')
+ end
+
+ def test_render_partial_with_locals_using_string
+ assert_equal "Hola: david", @controller_view.render('customer_greeting', :greeting => 'Hola', :customer_greeting => Customer.new("david"))
+ end
+
+ def test_render_partial_using_object
+ assert_equal "Hello: lifo",
+ @controller_view.render(Customer.new("lifo"), :greeting => "Hello")
+ end
+
+ def test_render_partial_using_collection
+ customers = [ Customer.new("Amazon"), Customer.new("Yahoo") ]
+ assert_equal "Hello: AmazonHello: Yahoo",
+ @controller_view.render(customers, :greeting => "Hello")
+ end
+
# TODO: The reason for this test is unclear, improve documentation
def test_render_partial_and_fallback_to_layout
assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
@@ -167,6 +190,8 @@ module RenderTestCases
def test_render_missing_xml_partial_and_raise_missing_template
@view.formats = [:xml]
assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") }
+ ensure
+ @view.formats = nil
end
def test_render_inline
@@ -196,17 +221,6 @@ module RenderTestCases
assert_equal 'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
end
- class LegacyHandler < ActionView::TemplateHandler
- def render(template, local_assigns)
- "source: #{template.source}; locals: #{local_assigns.inspect}"
- end
- end
-
- def test_render_legacy_handler_with_custom_type
- ActionView::Template.register_template_handler :foo, LegacyHandler
- assert_equal 'source: Hello, <%= name %>!; locals: {:name=>"Josh"}', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
- end
-
def test_render_ignores_templates_with_malformed_template_handlers
%w(malformed malformed.erb malformed.html.erb malformed.en.html.erb).each do |name|
assert_raise(ActionView::MissingTemplate) { @view.render(:file => "test/malformed/#{name}") }