aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/test')
-rw-r--r--actionpack/test/abstract_unit.rb8
-rw-r--r--actionpack/test/activerecord/active_record_store_test.rb12
-rw-r--r--actionpack/test/activerecord/render_partial_with_record_identification_test.rb6
-rw-r--r--actionpack/test/controller/caching_test.rb16
-rw-r--r--actionpack/test/controller/mime_responds_test.rb76
-rw-r--r--actionpack/test/controller/new_base/render_template_test.rb20
-rw-r--r--actionpack/test/controller/routing_test.rb581
-rw-r--r--actionpack/test/controller/runner_test.rb22
-rw-r--r--actionpack/test/controller/url_for_test.rb23
-rw-r--r--actionpack/test/dispatch/callbacks_test.rb72
-rw-r--r--actionpack/test/dispatch/cookies_test.rb21
-rw-r--r--actionpack/test/dispatch/mime_type_test.rb51
-rw-r--r--actionpack/test/dispatch/mount_test.rb18
-rw-r--r--actionpack/test/dispatch/reloader_test.rb124
-rw-r--r--actionpack/test/dispatch/request_test.rb39
-rw-r--r--actionpack/test/dispatch/routing_test.rb76
-rw-r--r--actionpack/test/fixtures/star_star_mime/index.js.erb1
-rw-r--r--actionpack/test/fixtures/test/scoped_translation.erb1
-rw-r--r--actionpack/test/fixtures/test/translation.erb1
-rw-r--r--actionpack/test/fixtures/translations/templates/array.erb1
-rw-r--r--actionpack/test/fixtures/translations/templates/found.erb1
-rw-r--r--actionpack/test/fixtures/translations/templates/missing.erb1
-rw-r--r--actionpack/test/lib/controller/fake_models.rb6
-rw-r--r--actionpack/test/template/lookup_context_test.rb29
-rw-r--r--actionpack/test/template/number_helper_test.rb5
-rw-r--r--actionpack/test/template/render_test.rb5
-rw-r--r--actionpack/test/template/translation_helper_test.rb72
27 files changed, 885 insertions, 403 deletions
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb
index 92597e40ff..60534a9746 100644
--- a/actionpack/test/abstract_unit.rb
+++ b/actionpack/test/abstract_unit.rb
@@ -36,14 +36,6 @@ require 'active_record'
require 'action_controller/caching'
require 'action_controller/caching/sweeping'
-begin
- require 'ruby-debug'
- Debugger.settings[:autoeval] = true
- Debugger.start
-rescue LoadError
- # Debugging disabled. `gem install ruby-debug` to enable.
-end
-
require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
module Rails
diff --git a/actionpack/test/activerecord/active_record_store_test.rb b/actionpack/test/activerecord/active_record_store_test.rb
index 7c595d1b89..f0fb113860 100644
--- a/actionpack/test/activerecord/active_record_store_test.rb
+++ b/actionpack/test/activerecord/active_record_store_test.rb
@@ -23,6 +23,7 @@ class ActiveRecordStoreTest < ActionDispatch::IntegrationTest
def call_reset_session
session[:foo]
reset_session
+ reset_session if params[:twice]
session[:foo] = "baz"
head :ok
end
@@ -94,6 +95,17 @@ class ActiveRecordStoreTest < ActionDispatch::IntegrationTest
end
end
+ def test_calling_reset_session_twice_does_not_raise_errors
+ with_test_route_set do
+ get '/call_reset_session', :twice => "true"
+ assert_response :success
+
+ get '/get_session_value'
+ assert_response :success
+ assert_equal 'foo: "baz"', response.body
+ end
+ end
+
def test_setting_session_value_after_session_reset
with_test_route_set do
get '/set_session_value'
diff --git a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb
index 43c534c111..99f09286ff 100644
--- a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb
+++ b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb
@@ -11,7 +11,7 @@ class RenderPartialWithRecordIdentificationController < ActionController::Base
render :partial => @topic.replies
end
- def render_with_named_scope
+ def render_with_scope
render :partial => Reply.base
end
@@ -62,8 +62,8 @@ class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
assert_equal 'Birdman is better!', @response.body
end
- def test_rendering_partial_with_named_scope
- get :render_with_named_scope
+ def test_rendering_partial_with_scope
+ get :render_with_scope
assert_template 'replies/_reply'
assert_equal 'Birdman is better!Nuh uh!', @response.body
end
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index 914ae56032..c7b54eb0ba 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -16,6 +16,7 @@ end
class PageCachingTestController < CachingController
caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? }
caches_page :found, :not_found
+ caches_page :about_me
def ok
@@ -47,6 +48,14 @@ class PageCachingTestController < CachingController
def trailing_slash
render :text => "Sneak attack"
end
+
+ def about_me
+ respond_to do |format|
+ format.html {render :text => 'I am html'}
+ format.xml {render :text => 'I am xml'}
+ end
+ end
+
end
class PageCachingTest < ActionController::TestCase
@@ -111,6 +120,13 @@ class PageCachingTest < ActionController::TestCase
assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html")
end
+ def test_should_obey_http_accept_attribute
+ @request.env['HTTP_ACCEPT'] = 'text/xml'
+ get :about_me
+ assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/about_me.xml")
+ assert_equal 'I am xml', @response.body
+ end
+
def test_should_cache_with_trailing_slash_on_url
@controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash/'
assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html")
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb
index b6ce9f7d34..98c9d43b93 100644
--- a/actionpack/test/controller/mime_responds_test.rb
+++ b/actionpack/test/controller/mime_responds_test.rb
@@ -2,6 +2,14 @@ require 'abstract_unit'
require 'controller/fake_models'
require 'active_support/core_ext/hash/conversions'
+class StarStarMimeController < ActionController::Base
+ layout nil
+
+ def index
+ render
+ end
+end
+
class RespondToController < ActionController::Base
layout :set_layout
@@ -89,7 +97,6 @@ class RespondToController < ActionController::Base
end
end
- Mime::Type.register("text/x-mobile", :mobile)
def custom_constant_handling
respond_to do |type|
@@ -126,7 +133,6 @@ class RespondToController < ActionController::Base
end
end
- Mime::Type.register_alias("text/html", :iphone)
def iphone_with_html_response_type
request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
@@ -160,16 +166,43 @@ class RespondToController < ActionController::Base
end
end
+class StarStarMimeControllerTest < ActionController::TestCase
+ tests StarStarMimeController
+
+ def test_javascript_with_format
+ @request.accept = "text/javascript"
+ get :index, :format => 'js'
+ assert_match "function addition(a,b){ return a+b; }", @response.body
+ end
+
+ def test_javascript_with_no_format
+ @request.accept = "text/javascript"
+ get :index
+ assert_match "function addition(a,b){ return a+b; }", @response.body
+ end
+
+ def test_javascript_with_no_format_only_star_star
+ @request.accept = "*/*"
+ get :index
+ assert_match "function addition(a,b){ return a+b; }", @response.body
+ end
+
+end
+
class RespondToControllerTest < ActionController::TestCase
tests RespondToController
def setup
super
@request.host = "www.example.com"
+ Mime::Type.register_alias("text/html", :iphone)
+ Mime::Type.register("text/x-mobile", :mobile)
end
def teardown
super
+ Mime::Type.unregister(:iphone)
+ Mime::Type.unregister(:mobile)
end
def test_html
@@ -216,6 +249,16 @@ class RespondToControllerTest < ActionController::TestCase
assert_response 406
end
+ def test_json_or_yaml_with_leading_star_star
+ @request.accept = "*/*, application/json"
+ get :json_xml_or_html
+ assert_equal 'HTML', @response.body
+
+ @request.accept = "*/* , application/json"
+ get :json_xml_or_html
+ assert_equal 'HTML', @response.body
+ end
+
def test_json_or_yaml
xhr :get, :json_or_yaml
assert_equal 'JSON', @response.body
@@ -556,6 +599,17 @@ class InheritedRespondWithController < RespondWithController
end
end
+class RenderJsonRespondWithController < RespondWithController
+ clear_respond_to
+ respond_to :json
+
+ def index
+ respond_with(resource) do |format|
+ format.json { render :json => RenderJsonTestException.new('boom') }
+ end
+ end
+end
+
class EmptyRespondWithController < ActionController::Base
def index
respond_with(Customer.new("david", 13))
@@ -568,10 +622,14 @@ class RespondWithControllerTest < ActionController::TestCase
def setup
super
@request.host = "www.example.com"
+ Mime::Type.register_alias('text/html', :iphone)
+ Mime::Type.register('text/x-mobile', :mobile)
end
def teardown
super
+ Mime::Type.unregister(:iphone)
+ Mime::Type.unregister(:mobile)
end
def test_using_resource
@@ -869,6 +927,14 @@ class RespondWithControllerTest < ActionController::TestCase
assert_equal "JSON", @response.body
end
+ def test_render_json_object_responds_to_str_still_produce_json
+ @controller = RenderJsonRespondWithController.new
+ @request.accept = "application/json"
+ get :index, :format => :json
+ assert_match(/"message":"boom"/, @response.body)
+ assert_match(/"error":"RenderJsonTestException"/, @response.body)
+ end
+
def test_no_double_render_is_raised
@request.accept = "text/html"
assert_raise ActionView::MissingTemplate do
@@ -952,6 +1018,12 @@ class MimeControllerLayoutsTest < ActionController::TestCase
def setup
super
@request.host = "www.example.com"
+ Mime::Type.register_alias("text/html", :iphone)
+ end
+
+ def teardown
+ super
+ Mime::Type.unregister(:iphone)
end
def test_missing_layout_renders_properly
diff --git a/actionpack/test/controller/new_base/render_template_test.rb b/actionpack/test/controller/new_base/render_template_test.rb
index 9899036fe8..584f2d772c 100644
--- a/actionpack/test/controller/new_base/render_template_test.rb
+++ b/actionpack/test/controller/new_base/render_template_test.rb
@@ -4,16 +4,16 @@ module RenderTemplate
class WithoutLayoutController < ActionController::Base
self.view_paths = [ActionView::FixtureResolver.new(
- "test/basic.html.erb" => "Hello from basic.html.erb",
- "shared.html.erb" => "Elastica",
- "locals.html.erb" => "The secret is <%= secret %>",
- "xml_template.xml.builder" => "xml.html do\n xml.p 'Hello'\nend",
- "with_raw.html.erb" => "Hello <%=raw '<strong>this is raw</strong>' %>",
- "with_implicit_raw.html.erb"=> "Hello <%== '<strong>this is also raw</strong>' %>",
- "test/with_json.html.erb" => "<%= render :template => 'test/with_json.json' %>",
- "test/with_json.json.erb" => "<%= render :template => 'test/final' %>",
- "test/final.json.erb" => "{ final: json }",
- "test/with_error.html.erb" => "<%= idontexist %>"
+ "test/basic.html.erb" => "Hello from basic.html.erb",
+ "shared.html.erb" => "Elastica",
+ "locals.html.erb" => "The secret is <%= secret %>",
+ "xml_template.xml.builder" => "xml.html do\n xml.p 'Hello'\nend",
+ "with_raw.html.erb" => "Hello <%=raw '<strong>this is raw</strong>' %>",
+ "with_implicit_raw.html.erb" => "Hello <%== '<strong>this is also raw</strong>' %>",
+ "test/with_json.html.erb" => "<%= render :template => 'test/with_json.json' %>",
+ "test/with_json.json.erb" => "<%= render :template => 'test/final' %>",
+ "test/final.json.erb" => "{ final: json }",
+ "test/with_error.html.erb" => "<%= idontexist %>"
)]
def index
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index cd067b7d18..89f0d03c56 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -12,8 +12,16 @@ end
ROUTING = ActionDispatch::Routing
+module RoutingTestHelpers
+ def url_for(set, options, recall = nil)
+ set.send(:url_for, options.merge(:only_path => true, :_path_segments => recall))
+ end
+end
+
# See RFC 3986, section 3.3 for allowed path characters.
class UriReservedCharactersRoutingTest < Test::Unit::TestCase
+ include RoutingTestHelpers
+
def setup
@set = ActionDispatch::Routing::RouteSet.new
@set.draw do
@@ -28,12 +36,13 @@ class UriReservedCharactersRoutingTest < Test::Unit::TestCase
end
def test_route_generation_escapes_unsafe_path_characters
- @set.generate(:controller => "content", :action => "act#{@segment}ion", :variable => "variable", :additional => "foo")
assert_equal "/content/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2",
- @set.generate(:controller => "content",
- :action => "act#{@segment}ion",
- :variable => "var#{@segment}iable",
- :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"])
+ url_for(@set, {
+ :controller => "content",
+ :action => "act#{@segment}ion",
+ :variable => "var#{@segment}iable",
+ :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"]
+ })
end
def test_route_recognition_unescapes_path_components
@@ -45,10 +54,13 @@ class UriReservedCharactersRoutingTest < Test::Unit::TestCase
end
def test_route_generation_allows_passing_non_string_values_to_generated_helper
- assert_equal "/content/action/variable/1/2", @set.generate(:controller => "content",
- :action => "action",
- :variable => "variable",
- :additional => [1, 2])
+ assert_equal "/content/action/variable/1/2",
+ url_for(@set, {
+ :controller => "content",
+ :action => "action",
+ :variable => "variable",
+ :additional => [1, 2]
+ })
end
end
@@ -68,6 +80,8 @@ class MockController
end
class LegacyRouteSetTests < Test::Unit::TestCase
+ include RoutingTestHelpers
+
attr_reader :rs
def setup
@@ -81,18 +95,18 @@ class LegacyRouteSetTests < Test::Unit::TestCase
def test_default_setup
@rs.draw { match '/:controller(/:action(/:id))' }
assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
- assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
+ assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
- assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10)
+ assert_equal '/admin/user/show/10', url_for(rs, { :controller => 'admin/user', :action => 'show', :id => 10 })
- assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
- assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
+ assert_equal '/admin/user/show', url_for(rs, { :action => 'show' }, { :controller => 'admin/user', :action => 'list', :id => '10' })
+ assert_equal '/admin/user/list/10', url_for(rs, {}, { :controller => 'admin/user', :action => 'list', :id => '10' })
- assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
- assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
+ assert_equal '/admin/stuff', url_for(rs, { :controller => 'stuff' }, { :controller => 'admin/user', :action => 'list', :id => '10' })
+ assert_equal '/stuff', url_for(rs, { :controller => '/stuff' }, { :controller => 'admin/user', :action => 'list', :id => '10' })
end
def test_ignores_leading_slash
@@ -143,11 +157,14 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match ':controller/:admintoken(/:action(/:id))', :controller => /admin\/.+/
match '/:controller(/:action(/:id))'
end
+
assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
rs.recognize_path("/admin/user/foo"))
- assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo"))
- assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
- assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo")
+ assert_equal({:controller => "content", :action => "foo"},
+ rs.recognize_path("/content/foo"))
+
+ assert_equal '/admin/user/foo', url_for(rs, { :controller => "admin/user", :admintoken => "foo", :action => "index" })
+ assert_equal '/content/foo', url_for(rs, { :controller => "content", :action => "foo" })
end
def test_route_with_regexp_and_captures_for_controller
@@ -169,17 +186,16 @@ class LegacyRouteSetTests < Test::Unit::TestCase
end
# Without a file extension
assert_equal '/user/download/file',
- rs.generate(:controller => "user", :action => "download", :file => "file")
- assert_equal(
- {:controller => "user", :action => "download", :file => "file"},
+ url_for(rs, { :controller => "user", :action => "download", :file => "file" })
+
+ assert_equal({:controller => "user", :action => "download", :file => "file"},
rs.recognize_path("/user/download/file"))
# Now, let's try a file with an extension, really a dot (.)
assert_equal '/user/download/file.jpg',
- rs.generate(
- :controller => "user", :action => "download", :file => "file.jpg")
- assert_equal(
- {:controller => "user", :action => "download", :file => "file.jpg"},
+ url_for(rs, { :controller => "user", :action => "download", :file => "file.jpg" })
+
+ assert_equal({:controller => "user", :action => "download", :file => "file.jpg"},
rs.recognize_path("/user/download/file.jpg"))
end
@@ -187,28 +203,25 @@ class LegacyRouteSetTests < Test::Unit::TestCase
rs.draw do
root :to => 'content#list', :as => 'home'
end
- x = setup_for_named_route
- assert_equal("http://test.host/",
- x.send(:home_url))
+ assert_equal("http://test.host/", setup_for_named_route.send(:home_url))
end
def test_named_route_with_option
rs.draw do
match 'page/:title' => 'content#show_page', :as => 'page'
end
- x = setup_for_named_route
+
assert_equal("http://test.host/page/new%20stuff",
- x.send(:page_url, :title => 'new stuff'))
+ setup_for_named_route.send(:page_url, :title => 'new stuff'))
end
def test_named_route_with_default
rs.draw do
match 'page/:title' => 'content#show_page', :title => 'AboutPage', :as => 'page'
end
- x = setup_for_named_route
- assert_equal("http://test.host/page/AboutRails",
- x.send(:page_url, :title => "AboutRails"))
+ assert_equal("http://test.host/page/AboutRails",
+ setup_for_named_route.send(:page_url, :title => "AboutRails"))
end
def test_named_route_with_path_prefix
@@ -217,9 +230,9 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match 'page' => 'content#show_page', :as => 'page'
end
end
- x = setup_for_named_route
+
assert_equal("http://test.host/my/page",
- x.send(:page_url))
+ setup_for_named_route.send(:page_url))
end
def test_named_route_with_blank_path_prefix
@@ -228,27 +241,33 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match 'page' => 'content#show_page', :as => 'page'
end
end
- x = setup_for_named_route
+
assert_equal("http://test.host/page",
- x.send(:page_url))
+ setup_for_named_route.send(:page_url))
end
def test_named_route_with_nested_controller
rs.draw do
match 'admin/user' => 'admin/user#index', :as => "users"
end
- x = setup_for_named_route
+
assert_equal("http://test.host/admin/user",
- x.send(:users_url))
+ setup_for_named_route.send(:users_url))
end
def test_optimised_named_route_with_host
rs.draw do
match 'page' => 'content#show_page', :as => 'pages', :host => 'foo.com'
end
- x = setup_for_named_route
- x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => 'pages').once
- x.send(:pages_url)
+ routes = setup_for_named_route
+ routes.expects(:url_for).with({
+ :host => 'foo.com',
+ :only_path => false,
+ :controller => 'content',
+ :action => 'show_page',
+ :use_route => 'pages'
+ }).once
+ routes.send(:pages_url)
end
def setup_for_named_route
@@ -265,9 +284,9 @@ class LegacyRouteSetTests < Test::Unit::TestCase
rs.draw do
root :to => "hello#index"
end
- x = setup_for_named_route
- assert_equal("http://test.host/", x.send(:root_url))
- assert_equal("/", x.send(:root_path))
+ routes = setup_for_named_route
+ assert_equal("http://test.host/", routes.send(:root_url))
+ assert_equal("/", routes.send(:root_path))
end
def test_named_route_with_regexps
@@ -276,24 +295,19 @@ class LegacyRouteSetTests < Test::Unit::TestCase
:year => /\d+/, :month => /\d+/, :day => /\d+/
match ':controller/:action/:id'
end
- x = setup_for_named_route
- # assert_equal(
- # {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
- # x.send(:article_url, :title => 'hi')
- # )
- assert_equal(
- "http://test.host/page/2005/6/10/hi",
- x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
- )
+
+ routes = setup_for_named_route
+
+ assert_equal "http://test.host/page/2005/6/10/hi",
+ routes.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
end
def test_changing_controller
@rs.draw { match ':controller/:action/:id' }
- assert_equal '/admin/stuff/show/10', rs.generate(
- {:controller => 'stuff', :action => 'show', :id => 10},
- {:controller => 'admin/user', :action => 'index'}
- )
+ assert_equal '/admin/stuff/show/10',
+ url_for(rs, {:controller => 'stuff', :action => 'show', :id => 10},
+ {:controller => 'admin/user', :action => 'index'})
end
def test_paths_escaped
@@ -319,8 +333,7 @@ class LegacyRouteSetTests < Test::Unit::TestCase
end
# No / to %2F in URI, only for query params.
- x = setup_for_named_route
- assert_equal("/file/hello/world", x.send(:path_path, ['hello', 'world']))
+ assert_equal("/file/hello/world", setup_for_named_route.send(:path_path, ['hello', 'world']))
end
def test_non_controllers_cannot_be_matched
@@ -334,7 +347,9 @@ class LegacyRouteSetTests < Test::Unit::TestCase
rs.draw do
match 'post/:id' => 'post#show', :constraints => { :id => /\d+/ }, :as => 'post'
end
- assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
+ assert_raise(ActionController::RoutingError) do
+ url_for(rs, { :controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post" })
+ end
end
def test_dynamic_path_allowed
@@ -342,7 +357,8 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match '*path' => 'content#show_file'
end
- assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
+ assert_equal '/pages/boo',
+ url_for(rs, { :controller => 'content', :action => 'show_file', :path => %w(pages boo) })
end
def test_dynamic_recall_paths_allowed
@@ -350,7 +366,8 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match '*path' => 'content#show_file'
end
- assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => %w(pages boo))
+ assert_equal '/pages/boo',
+ url_for(rs, {}, { :controller => 'content', :action => 'show_file', :path => %w(pages boo) })
end
def test_backwards
@@ -359,9 +376,9 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match ':controller(/:action(/:id))'
end
- assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'})
- assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show')
- assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo')
+ assert_equal '/page/20', url_for(rs, { :id => 20 }, { :controller => 'pages', :action => 'show' })
+ assert_equal '/page/20', url_for(rs, { :controller => 'pages', :id => 20, :action => 'show' })
+ assert_equal '/pages/boo', url_for(rs, { :controller => 'pages', :action => 'boo' })
end
def test_route_with_fixnum_default
@@ -370,10 +387,10 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match ':controller/:action/:id'
end
- assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page')
- assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
- assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
- assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10)
+ assert_equal '/page', url_for(rs, { :controller => 'content', :action => 'show_page' })
+ assert_equal '/page', url_for(rs, { :controller => 'content', :action => 'show_page', :id => 1 })
+ assert_equal '/page', url_for(rs, { :controller => 'content', :action => 'show_page', :id => '1' })
+ assert_equal '/page/10', url_for(rs, { :controller => 'content', :action => 'show_page', :id => 10 })
assert_equal({:controller => "content", :action => 'show_page', :id => 1 }, rs.recognize_path("/page"))
assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
@@ -387,20 +404,20 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match ':controller/:action/:id'
end
- assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo')
- assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
+ assert_equal '/page/foo', url_for(rs, { :controller => 'content', :action => 'show_page', :id => 'foo' })
+ assert_equal({ :controller => "content", :action => 'show_page', :id => 'foo' }, rs.recognize_path("/page/foo"))
token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
token.force_encoding(Encoding::BINARY) if token.respond_to?(:force_encoding)
escaped_token = CGI::escape(token)
- assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)
- assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}"))
+ assert_equal '/page/' + escaped_token, url_for(rs, { :controller => 'content', :action => 'show_page', :id => token })
+ assert_equal({ :controller => "content", :action => 'show_page', :id => token }, rs.recognize_path("/page/#{escaped_token}"))
end
def test_action_expiry
@rs.draw { match ':controller(/:action(/:id))' }
- assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
+ assert_equal '/content', url_for(rs, { :controller => 'content' }, { :controller => 'content', :action => 'show' })
end
def test_requirement_should_prevent_optional_id
@@ -408,10 +425,10 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match 'post/:id' => 'post#show', :constraints => {:id => /\d+/}, :as => 'post'
end
- assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10)
+ assert_equal '/post/10', url_for(rs, { :controller => 'post', :action => 'show', :id => 10 })
assert_raise ActionController::RoutingError do
- rs.generate(:controller => 'post', :action => 'show')
+ url_for(rs, { :controller => 'post', :action => 'show' })
end
end
@@ -424,12 +441,10 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match ':controller/:action/:id'
end
- assert_equal '/test', rs.generate(:controller => 'post', :action => 'show')
- assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
+ assert_equal '/test', url_for(rs, { :controller => 'post', :action => 'show' })
+ assert_equal '/test', url_for(rs, { :controller => 'post', :action => 'show', :year => nil })
- x = setup_for_named_route
- assert_equal("http://test.host/test",
- x.send(:blog_url))
+ assert_equal("http://test.host/test", setup_for_named_route.send(:blog_url))
end
def test_set_to_nil_forgets
@@ -439,20 +454,20 @@ class LegacyRouteSetTests < Test::Unit::TestCase
end
assert_equal '/pages/2005',
- rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
+ url_for(rs, { :controller => 'content', :action => 'list_pages', :year => 2005 })
assert_equal '/pages/2005/6',
- rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
+ url_for(rs, { :controller => 'content', :action => 'list_pages', :year => 2005, :month => 6 })
assert_equal '/pages/2005/6/12',
- rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
+ url_for(rs, { :controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12 })
assert_equal '/pages/2005/6/4',
- rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ url_for(rs, { :day => 4 }, { :controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12' })
assert_equal '/pages/2005/6',
- rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ url_for(rs, { :day => nil }, { :controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12' })
assert_equal '/pages/2005',
- rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ url_for(rs, { :day => nil, :month => nil }, { :controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12' })
end
def test_root_url_generation_with_controller_and_action
@@ -460,8 +475,8 @@ class LegacyRouteSetTests < Test::Unit::TestCase
root :to => "content#index"
end
- assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
- assert_equal '/', rs.generate(:controller => 'content')
+ assert_equal '/', url_for(rs, { :controller => 'content', :action => 'index' })
+ assert_equal '/', url_for(rs, { :controller => 'content' })
end
def test_named_root_url_generation_with_controller_and_action
@@ -469,12 +484,10 @@ class LegacyRouteSetTests < Test::Unit::TestCase
root :to => "content#index", :as => 'home'
end
- assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
- assert_equal '/', rs.generate(:controller => 'content')
+ assert_equal '/', url_for(rs, { :controller => 'content', :action => 'index' })
+ assert_equal '/', url_for(rs, { :controller => 'content' })
- x = setup_for_named_route
- assert_equal("http://test.host/",
- x.send(:home_url))
+ assert_equal("http://test.host/", setup_for_named_route.send(:home_url))
end
def test_named_route_method
@@ -483,8 +496,8 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match ':controller(/:action(/:id))'
end
- assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories')
- assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
+ assert_equal '/categories', url_for(rs, { :controller => 'content', :action => 'categories' })
+ assert_equal '/content/hi', url_for(rs, { :controller => 'content', :action => 'hi' })
end
def test_named_routes_array
@@ -499,7 +512,12 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match ':controller/:action/:id'
end
- assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
+ assert_equal '/journal', url_for(rs, {
+ :controller => 'content',
+ :action => 'list_journal',
+ :date => nil,
+ :user_id => nil
+ })
end
def setup_request_method_routes_for(method)
@@ -564,9 +582,9 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match '/posts/new/:action' => 'subpath_books'
end
- assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit")
- assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete")
- assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview")
+ assert_equal "/books/7/edit", url_for(rs, { :controller => "subpath_books", :id => 7, :action => "edit" })
+ assert_equal "/items/15/complete", url_for(rs, { :controller => "subpath_books", :id => 15, :action => "complete" })
+ assert_equal "/posts/new/preview", url_for(rs, { :controller => "subpath_books", :action => "preview" })
end
def test_failed_constraints_raises_exception_with_violated_constraints
@@ -574,9 +592,8 @@ class LegacyRouteSetTests < Test::Unit::TestCase
match 'foos/:id' => 'foos#show', :as => 'foo_with_requirement', :constraints => { :id => /\d+/ }
end
- x = setup_for_named_route
assert_raise(ActionController::RoutingError) do
- x.send(:foo_with_requirement_url, "I am Against the constraints")
+ setup_for_named_route.send(:foo_with_requirement_url, "I am Against the constraints")
end
end
@@ -606,11 +623,12 @@ class LegacyRouteSetTests < Test::Unit::TestCase
assert_not_nil hash
assert_equal %w(cc ac), [hash[:controller], hash[:action]]
-
end
end
class RouteSetTest < ActiveSupport::TestCase
+ include RoutingTestHelpers
+
def set
@set ||= ROUTING::RouteSet.new
end
@@ -657,7 +675,8 @@ class RouteSetTest < ActiveSupport::TestCase
match ':controller/:action/:id.:format'
match ':controller/:action/:id'
end
- assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
+ assert_equal "/foo/bar/15?this=hello",
+ url_for(set, { :controller => "foo", :action => "bar", :id => 15, :this => "hello" })
end
def test_extra_keys_not_first
@@ -742,7 +761,7 @@ class RouteSetTest < ActiveSupport::TestCase
assert_equal "http://test.host/admin/users", controller.send(:users_url)
assert_equal '/admin/users', controller.send(:users_path)
- assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
+ assert_equal '/admin/users', url_for(set, controller.send(:hash_for_users_url), { :controller => 'users', :action => 'index' })
end
def test_named_route_url_method_with_anchor
@@ -813,8 +832,8 @@ class RouteSetTest < ActiveSupport::TestCase
assert_equal 1, set.routes.size
- assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10)
- assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10)
+ assert_equal '/users/show/10', url_for(set, { :controller => 'users', :action => 'show', :id => 10 })
+ assert_equal '/users/index/10', url_for(set, { :controller => 'users', :id => 10 })
assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
@@ -992,7 +1011,7 @@ class RouteSetTest < ActiveSupport::TestCase
match "/people/list", :controller => "people", :action => "list"
end
- url = set.generate(:controller => "people", :action => "list")
+ url = url_for(set, { :controller => "people", :action => "list" })
assert_equal "/people/list", url
end
@@ -1057,8 +1076,9 @@ class RouteSetTest < ActiveSupport::TestCase
def test_generate_changes_controller_module
set.draw { match ':controller/:action/:id' }
current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
- url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current)
- assert_equal "/foo/bar/baz/7", url
+
+ assert_equal "/foo/bar/baz/7",
+ url_for(set, { :controller => "foo/bar", :action => "baz", :id => 7 }, current)
end
def test_id_is_sticky_when_it_ought_to_be
@@ -1066,7 +1086,7 @@ class RouteSetTest < ActiveSupport::TestCase
match ':controller/:id/:action'
end
- url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
+ url = url_for(set, { :action => "destroy" }, { :controller => "people", :action => "show", :id => "7" })
assert_equal "/people/7/destroy", url
end
@@ -1076,8 +1096,9 @@ class RouteSetTest < ActiveSupport::TestCase
match ':controller/:action/:id'
end
- url = set.generate({:controller => "welcome", :action => "about"},
- {:controller => "welcome", :action => "get", :id => "7"})
+ url = url_for(set, { :controller => "welcome", :action => "about" },
+ { :controller => "welcome", :action => "get", :id => "7" })
+
assert_equal "/about", url
end
@@ -1085,7 +1106,7 @@ class RouteSetTest < ActiveSupport::TestCase
set.draw { match ':controller/:action/:id' }
args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
- assert_equal "/foo/bar/7?x=y", set.generate(args)
+ assert_equal "/foo/bar/7?x=y", url_for(set, args)
assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
assert_equal [:x], set.extra_keys(args)
end
@@ -1098,7 +1119,7 @@ class RouteSetTest < ActiveSupport::TestCase
end
args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
- assert_equal "/my/foo/bar/7?x=y", set.generate(args)
+ assert_equal "/my/foo/bar/7?x=y", url_for(set, args)
end
def test_generate_with_blank_path_prefix
@@ -1109,7 +1130,7 @@ class RouteSetTest < ActiveSupport::TestCase
end
args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
- assert_equal "/foo/bar/7?x=y", set.generate(args)
+ assert_equal "/foo/bar/7?x=y", url_for(set, args)
end
def test_named_routes_are_never_relative_to_modules
@@ -1119,10 +1140,10 @@ class RouteSetTest < ActiveSupport::TestCase
match '/connection' => 'connection#index', :as => 'family_connection'
end
- url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'})
+ url = url_for(set, { :controller => "connection" }, { :controller => 'connection/manage' })
assert_equal "/connection/connection", url
- url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
+ url = url_for(set, { :use_route => :family_connection, :controller => "connection" }, { :controller => 'connection/manage' })
assert_equal "/connection", url
end
@@ -1130,7 +1151,7 @@ class RouteSetTest < ActiveSupport::TestCase
set.draw do
match ':controller(/:action(/:id))'
end
- assert_equal '/books', set.generate(
+ assert_equal '/books', url_for(set,
{:controller => 'books', :action => 'index'},
{:controller => 'books', :action => 'show', :id => '10'}
)
@@ -1141,7 +1162,7 @@ class RouteSetTest < ActiveSupport::TestCase
match 'show_weblog/:parameter' => 'weblog#show'
match ':controller(/:action(/:id))'
end
- assert_equal '/weblog/edit?parameter=1', set.generate(
+ assert_equal '/weblog/edit?parameter=1', url_for(set,
{:action => 'edit', :parameter => 1},
{:controller => 'weblog', :action => 'show', :parameter => 1}
)
@@ -1152,12 +1173,12 @@ class RouteSetTest < ActiveSupport::TestCase
match '/posts(.:format)' => 'posts#index'
end
- assert_equal '/posts', set.generate(
+ assert_equal '/posts', url_for(set,
{:controller => 'posts'},
{:controller => 'posts', :action => 'index', :format => 'xml'}
)
- assert_equal '/posts.xml', set.generate(
+ assert_equal '/posts.xml', url_for(set,
{:controller => 'posts', :format => 'xml'},
{:controller => 'posts', :action => 'index', :format => 'xml'}
)
@@ -1165,9 +1186,9 @@ class RouteSetTest < ActiveSupport::TestCase
def test_expiry_determination_should_consider_values_with_to_param
set.draw { match 'projects/:project_id/:controller/:action' }
- assert_equal '/projects/1/weblog/show', set.generate(
- {:action => 'show', :project_id => 1},
- {:controller => 'weblog', :action => 'show', :project_id => '1'})
+ assert_equal '/projects/1/weblog/show', url_for(set,
+ { :action => 'show', :project_id => 1 },
+ { :controller => 'weblog', :action => 'show', :project_id => '1' })
end
def test_named_route_in_nested_resource
@@ -1208,7 +1229,7 @@ class RouteSetTest < ActiveSupport::TestCase
assert_raise ArgumentError do
set.draw do
match 'page/:name' => 'pages#show',
- :constraints => {:name => /(david|jamis)/m}
+ :constraints => { :name => /(david|jamis)/m }
end
end
end
@@ -1217,18 +1238,18 @@ class RouteSetTest < ActiveSupport::TestCase
assert_nothing_raised do
set.draw do
match 'page/:name' => 'pages#show',
- :constraints => {:name => /(david|jamis)/i}
+ :constraints => { :name => /(david|jamis)/i }
end
end
assert_nothing_raised do
set.draw do
match 'page/:name' => 'pages#show',
- :constraints => {:name => / # Desperately overcommented regexp
+ :constraints => { :name => / # Desperately overcommented regexp
( #Either
david #The Creator
| #Or
jamis #The Deployer
- )/x}
+ )/x }
end
end
end
@@ -1251,12 +1272,12 @@ class RouteSetTest < ActiveSupport::TestCase
:constraints => {:name => /(david|jamis)/i}
end
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
+ url = url_for(set, { :controller => 'pages', :action => 'show', :name => 'david' })
assert_equal "/page/david", url
assert_raise ActionController::RoutingError do
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
+ url_for(set, { :controller => 'pages', :action => 'show', :name => 'davidjamis' })
end
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
+ url = url_for(set, { :controller => 'pages', :action => 'show', :name => 'JAMIS' })
assert_equal "/page/JAMIS", url
end
@@ -1280,7 +1301,7 @@ class RouteSetTest < ActiveSupport::TestCase
end
end
- def test_route_requirement_generate_with_xi_modifiers
+ def test_route_requirement_with_xi_modifiers
set.draw do
match 'page/:name' => 'pages#show',
:constraints => {:name => / # Desperately overcommented regexp
@@ -1291,21 +1312,11 @@ class RouteSetTest < ActiveSupport::TestCase
)/xi}
end
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
- assert_equal "/page/JAMIS", url
- end
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'},
+ set.recognize_path('/page/JAMIS'))
- def test_route_requirement_recognize_with_xi_modifiers
- set.draw do
- match 'page/:name' => 'pages#show',
- :constraints => {:name => / # Desperately overcommented regexp
- ( #Either
- david #The Creator
- | #Or
- jamis #The Deployer
- )/xi}
- end
- assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS'))
+ assert_equal "/page/JAMIS",
+ url_for(set, { :controller => 'pages', :action => 'show', :name => 'JAMIS' })
end
def test_routes_with_symbols
@@ -1323,8 +1334,8 @@ class RouteSetTest < ActiveSupport::TestCase
match '/hello' => 'bar#index'
end
- assert_equal '/', set.generate(:controller => 'foo')
- assert_equal '/hello', set.generate(:controller => 'bar')
+ assert_equal '/', url_for(set, { :controller => 'foo' })
+ assert_equal '/hello', url_for(set, { :controller => 'bar' })
assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/'))
assert_equal({:controller => "bar", :action => "index"}, set.recognize_path('/hello'))
@@ -1335,7 +1346,7 @@ class RouteSetTest < ActiveSupport::TestCase
match '/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 '/cars/buy/1/2', url_for(set, { :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
@@ -1345,7 +1356,7 @@ class RouteSetTest < ActiveSupport::TestCase
match '/books/:action.rss', :controller => 'books'
end
- assert_equal '/books/list.rss', set.generate(:controller => 'books', :action => 'list')
+ assert_equal '/books/list.rss', url_for(set, { :controller => 'books', :action => 'list' })
assert_equal({:controller => "books", :action => "list"}, set.recognize_path('/books/list.rss'))
end
@@ -1355,14 +1366,14 @@ class RouteSetTest < ActiveSupport::TestCase
match '/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 '/books/list.rss', url_for(set, { :controller => 'books', :action => 'list', :format => 'rss' })
+ assert_equal '/books/list.xml', url_for(set, { :controller => 'books', :action => 'list', :format => 'xml' })
+ assert_equal '/books/list', url_for(set, { :controller => 'books', :action => 'list' })
+ assert_equal '/books', url_for(set, { :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 => "list"}, set.recognize_path('/books/list'))
assert_equal({:controller => "books", :action => "index"}, set.recognize_path('/books'))
end
@@ -1370,13 +1381,13 @@ class RouteSetTest < ActiveSupport::TestCase
@set = nil
set.draw { match("/:controller(/:action(/:id))") }
- assert_equal '/content', set.generate(:controller => 'content', :action => 'index')
- assert_equal '/content/list', set.generate(:controller => 'content', :action => 'list')
- assert_equal '/content/show/1', set.generate(:controller => 'content', :action => 'show', :id => '1')
+ assert_equal '/content', url_for(set, { :controller => 'content', :action => 'index' })
+ assert_equal '/content/list', url_for(set, { :controller => 'content', :action => 'list' })
+ assert_equal '/content/show/1', url_for(set, { :controller => 'content', :action => 'show', :id => '1' })
assert_equal({:controller => "content", :action => "index"}, set.recognize_path('/content'))
assert_equal({:controller => "content", :action => "index"}, set.recognize_path('/content/index'))
- assert_equal({:controller => "content", :action => "list"}, set.recognize_path('/content/list'))
+ assert_equal({:controller => "content", :action => "list"}, set.recognize_path('/content/list'))
assert_equal({:controller => "content", :action => "show", :id => "1"}, set.recognize_path('/content/show/1'))
end
@@ -1401,54 +1412,54 @@ class RouteSetTest < ActiveSupport::TestCase
end
def test_default_route_should_omit_default_action
- assert_equal '/accounts', default_route_set.generate({:controller => 'accounts', :action => 'index'})
+ assert_equal '/accounts', url_for(default_route_set, { :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'})
+ assert_equal '/accounts/index/20', url_for(default_route_set, { :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'})
+ assert_equal '/accounts/list_all', url_for(default_route_set, { :controller => 'accounts', :action => 'list_all' })
end
def test_default_route_should_uri_escape_pluses
expected = { :controller => 'pages', :action => 'show', :id => 'hello world' }
assert_equal expected, default_route_set.recognize_path('/pages/show/hello%20world')
- assert_equal '/pages/show/hello%20world', default_route_set.generate(expected, expected)
+ assert_equal '/pages/show/hello%20world', url_for(default_route_set, expected)
expected[:id] = 'hello+world'
assert_equal expected, default_route_set.recognize_path('/pages/show/hello+world')
assert_equal expected, default_route_set.recognize_path('/pages/show/hello%2Bworld')
- assert_equal '/pages/show/hello+world', default_route_set.generate(expected, expected)
+ assert_equal '/pages/show/hello+world', url_for(default_route_set, expected)
end
def test_build_empty_query_string
- assert_uri_equal '/foo', default_route_set.generate({:controller => 'foo'})
+ assert_uri_equal '/foo', url_for(default_route_set, { :controller => 'foo' })
end
def test_build_query_string_with_nil_value
- assert_uri_equal '/foo', default_route_set.generate({:controller => 'foo', :x => nil})
+ assert_uri_equal '/foo', url_for(default_route_set, { :controller => 'foo', :x => nil })
end
def test_simple_build_query_string
- assert_uri_equal '/foo?x=1&y=2', default_route_set.generate({:controller => 'foo', :x => '1', :y => '2'})
+ assert_uri_equal '/foo?x=1&y=2', url_for(default_route_set, { :controller => 'foo', :x => '1', :y => '2' })
end
def test_convert_ints_build_query_string
- assert_uri_equal '/foo?x=1&y=2', default_route_set.generate({:controller => 'foo', :x => 1, :y => 2})
+ assert_uri_equal '/foo?x=1&y=2', url_for(default_route_set, { :controller => 'foo', :x => 1, :y => 2 })
end
def test_escape_spaces_build_query_string
- assert_uri_equal '/foo?x=hello+world&y=goodbye+world', default_route_set.generate({:controller => 'foo', :x => 'hello world', :y => 'goodbye world'})
+ assert_uri_equal '/foo?x=hello+world&y=goodbye+world', url_for(default_route_set, { :controller => 'foo', :x => 'hello world', :y => 'goodbye world' })
end
def test_expand_array_build_query_string
- assert_uri_equal '/foo?x%5B%5D=1&x%5B%5D=2', default_route_set.generate({:controller => 'foo', :x => [1, 2]})
+ assert_uri_equal '/foo?x%5B%5D=1&x%5B%5D=2', url_for(default_route_set, { :controller => 'foo', :x => [1, 2] })
end
def test_escape_spaces_build_query_string_selected_keys
- assert_uri_equal '/foo?x=hello+world', default_route_set.generate({:controller => 'foo', :x => 'hello world'})
+ assert_uri_equal '/foo?x=hello+world', url_for(default_route_set, { :controller => 'foo', :x => 'hello world' })
end
def test_generate_with_default_params
@@ -1462,7 +1473,7 @@ class RouteSetTest < ActiveSupport::TestCase
match ':controller/:action/:id'
end
- assert_equal '/ibocorp', set.generate({:controller => 'ibocorp', :action => "show", :page => 1})
+ assert_equal '/ibocorp', url_for(set, { :controller => 'ibocorp', :action => "show", :page => 1 })
end
def test_generate_with_optional_params_recalls_last_request
@@ -1492,11 +1503,11 @@ class RouteSetTest < ActiveSupport::TestCase
last_request = set.recognize_path("/blog/2006/07/28").freeze
assert_equal({:controller => "blog", :action => "show_date", :year => "2006", :month => "07", :day => "28"}, last_request)
- assert_equal("/blog/2006/07/25", set.generate({:day => 25}, last_request))
- assert_equal("/blog/2005", set.generate({:year => 2005}, last_request))
- assert_equal("/blog/show/123", set.generate({:action => "show" , :id => 123}, last_request))
- assert_equal("/blog/2006", set.generate({:year => 2006}, last_request))
- assert_equal("/blog/2006", set.generate({:year => 2006, :month => nil}, last_request))
+ assert_equal("/blog/2006/07/25", url_for(set, { :day => 25 }, last_request))
+ assert_equal("/blog/2005", url_for(set, { :year => 2005 }, last_request))
+ assert_equal("/blog/show/123", url_for(set, { :action => "show" , :id => 123 }, last_request))
+ assert_equal("/blog/2006", url_for(set, { :year => 2006 }, last_request))
+ assert_equal("/blog/2006", url_for(set, { :year => 2006, :month => nil }, last_request))
end
private
@@ -1512,6 +1523,8 @@ class RouteSetTest < ActiveSupport::TestCase
end
class RackMountIntegrationTests < ActiveSupport::TestCase
+ include RoutingTestHelpers
+
Model = Struct.new(:to_param)
Mapping = lambda {
@@ -1647,111 +1660,111 @@ class RackMountIntegrationTests < ActiveSupport::TestCase
end
def test_generate
- assert_equal '/admin/users', @routes.generate(:use_route => 'admin_users')
- assert_equal '/admin/users', @routes.generate(:controller => 'admin/users')
- assert_equal '/admin/users', @routes.generate(:controller => 'admin/users', :action => 'index')
- assert_equal '/admin/users', @routes.generate({:action => 'index'}, {:controller => 'admin/users'})
- assert_equal '/admin/users', @routes.generate({:controller => 'users', :action => 'index'}, {:controller => 'admin/accounts'})
- assert_equal '/people', @routes.generate({:controller => '/people', :action => 'index'}, {:controller => 'admin/accounts'})
-
- assert_equal '/admin/posts', @routes.generate({:controller => 'admin/posts'})
- assert_equal '/admin/posts/new', @routes.generate({:controller => 'admin/posts', :action => 'new'})
-
- assert_equal '/blog/2009', @routes.generate(:controller => 'posts', :action => 'show_date', :year => 2009)
- assert_equal '/blog/2009/1', @routes.generate(:controller => 'posts', :action => 'show_date', :year => 2009, :month => 1)
- assert_equal '/blog/2009/1/1', @routes.generate(:controller => 'posts', :action => 'show_date', :year => 2009, :month => 1, :day => 1)
-
- assert_equal '/archive/2010', @routes.generate(:controller => 'archive', :action => 'index', :year => '2010')
- assert_equal '/archive', @routes.generate(:controller => 'archive', :action => 'index')
- assert_equal '/archive?year=january', @routes.generate(:controller => 'archive', :action => 'index', :year => 'january')
-
- assert_equal '/people', @routes.generate(:controller => 'people', :action => 'index')
- assert_equal '/people', @routes.generate({:action => 'index'}, {:controller => 'people'})
- assert_equal '/people', @routes.generate({:action => 'index'}, {:controller => 'people', :action => 'show', :id => '1'})
- assert_equal '/people', @routes.generate({:controller => 'people', :action => 'index'}, {:controller => 'people', :action => 'show', :id => '1'})
- assert_equal '/people', @routes.generate({}, {:controller => 'people', :action => 'index'})
- assert_equal '/people/1', @routes.generate({:controller => 'people', :action => 'show'}, {:controller => 'people', :action => 'show', :id => '1'})
- assert_equal '/people/new', @routes.generate(:use_route => 'new_person')
- assert_equal '/people/new', @routes.generate(:controller => 'people', :action => 'new')
- assert_equal '/people/1', @routes.generate(:use_route => 'person', :id => '1')
- assert_equal '/people/1', @routes.generate(:controller => 'people', :action => 'show', :id => '1')
- assert_equal '/people/1.xml', @routes.generate(:controller => 'people', :action => 'show', :id => '1', :format => 'xml')
- assert_equal '/people/1', @routes.generate(:controller => 'people', :action => 'show', :id => 1)
- assert_equal '/people/1', @routes.generate(:controller => 'people', :action => 'show', :id => Model.new('1'))
- assert_equal '/people/1', @routes.generate({:action => 'show', :id => '1'}, {:controller => 'people', :action => 'index'})
- assert_equal '/people/1', @routes.generate({:action => 'show', :id => 1}, {:controller => 'people', :action => 'show', :id => '1'})
- assert_equal '/people', @routes.generate({:controller => 'people', :action => 'index'}, {:controller => 'people', :action => 'show', :id => '1'})
- assert_equal '/people/1', @routes.generate({}, {:controller => 'people', :action => 'show', :id => '1'})
- assert_equal '/people/1', @routes.generate({:controller => 'people', :action => 'show'}, {:controller => 'people', :action => 'index', :id => '1'})
- assert_equal '/people/1/edit', @routes.generate(:controller => 'people', :action => 'edit', :id => '1')
- assert_equal '/people/1/edit.xml', @routes.generate(:controller => 'people', :action => 'edit', :id => '1', :format => 'xml')
- assert_equal '/people/1/edit', @routes.generate(:use_route => 'edit_person', :id => '1')
- assert_equal '/people/1?legacy=true', @routes.generate(:controller => 'people', :action => 'show', :id => '1', :legacy => 'true')
- assert_equal '/people?legacy=true', @routes.generate(:controller => 'people', :action => 'index', :legacy => 'true')
-
- assert_equal '/id_default/2', @routes.generate(:controller => 'foo', :action => 'id_default', :id => '2')
- assert_equal '/id_default', @routes.generate(:controller => 'foo', :action => 'id_default', :id => '1')
- assert_equal '/id_default', @routes.generate(:controller => 'foo', :action => 'id_default', :id => 1)
- assert_equal '/id_default', @routes.generate(:controller => 'foo', :action => 'id_default')
- assert_equal '/optional/bar', @routes.generate(:controller => 'posts', :action => 'index', :optional => 'bar')
- assert_equal '/posts', @routes.generate(:controller => 'posts', :action => 'index')
-
- assert_equal '/project', @routes.generate({:controller => 'project', :action => 'index'})
- assert_equal '/projects/1', @routes.generate({:controller => 'project', :action => 'index', :project_id => '1'})
- assert_equal '/projects/1', @routes.generate({:controller => 'project', :action => 'index'}, {:project_id => '1'})
- assert_raise(ActionController::RoutingError) { @routes.generate({:use_route => 'project', :controller => 'project', :action => 'index'}) }
- assert_equal '/projects/1', @routes.generate({:use_route => 'project', :controller => 'project', :action => 'index', :project_id => '1'})
- assert_equal '/projects/1', @routes.generate({:use_route => 'project', :controller => 'project', :action => 'index'}, {:project_id => '1'})
-
- assert_equal '/clients', @routes.generate(:controller => 'projects', :action => 'index')
- assert_equal '/clients?project_id=1', @routes.generate(:controller => 'projects', :action => 'index', :project_id => '1')
- assert_equal '/clients', @routes.generate({:controller => 'projects', :action => 'index'}, {:project_id => '1'})
- assert_equal '/clients', @routes.generate({:action => 'index'}, {:controller => 'projects', :action => 'index', :project_id => '1'})
-
- assert_equal '/comment/20', @routes.generate({:id => 20}, {:controller => 'comments', :action => 'show'})
- assert_equal '/comment/20', @routes.generate(:controller => 'comments', :id => 20, :action => 'show')
- assert_equal '/comments/boo', @routes.generate(:controller => 'comments', :action => 'boo')
-
- assert_equal '/ws/posts/show/1', @routes.generate(:controller => 'posts', :action => 'show', :id => '1', :ws => true)
- assert_equal '/ws/posts', @routes.generate(:controller => 'posts', :action => 'index', :ws => true)
-
- assert_equal '/account', @routes.generate(:controller => 'account', :action => 'subscription')
- assert_equal '/account/billing', @routes.generate(:controller => 'account', :action => 'billing')
-
- assert_equal '/pages/1/notes/show/1', @routes.generate(:page_id => '1', :controller => 'notes', :action => 'show', :id => '1')
- assert_equal '/pages/1/notes/list', @routes.generate(:page_id => '1', :controller => 'notes', :action => 'list')
- assert_equal '/pages/1/notes', @routes.generate(:page_id => '1', :controller => 'notes', :action => 'index')
- assert_equal '/pages/1/notes', @routes.generate(:page_id => '1', :controller => 'notes')
- assert_equal '/notes', @routes.generate(:page_id => nil, :controller => 'notes')
- assert_equal '/notes', @routes.generate(:controller => 'notes')
- assert_equal '/notes/print', @routes.generate(:controller => 'notes', :action => 'print')
- assert_equal '/notes/print', @routes.generate({}, {:controller => 'notes', :action => 'print'})
-
- assert_equal '/notes/index/1', @routes.generate({:controller => 'notes'}, {:controller => 'notes', :id => '1'})
- assert_equal '/notes/index/1', @routes.generate({:controller => 'notes'}, {:controller => 'notes', :id => '1', :foo => 'bar'})
- assert_equal '/notes/index/1', @routes.generate({:controller => 'notes'}, {:controller => 'notes', :id => '1'})
- assert_equal '/notes/index/1', @routes.generate({:action => 'index'}, {:controller => 'notes', :id => '1'})
- assert_equal '/notes/index/1', @routes.generate({}, {:controller => 'notes', :id => '1'})
- assert_equal '/notes/show/1', @routes.generate({}, {:controller => 'notes', :action => 'show', :id => '1'})
- assert_equal '/notes/index/1', @routes.generate({:controller => 'notes', :id => '1'}, {:foo => 'bar'})
- assert_equal '/posts', @routes.generate({:controller => 'posts'}, {:controller => 'notes', :action => 'show', :id => '1'})
- assert_equal '/notes/list', @routes.generate({:action => 'list'}, {:controller => 'notes', :action => 'show', :id => '1'})
-
- assert_equal '/posts/ping', @routes.generate(:controller => 'posts', :action => 'ping')
- assert_equal '/posts/show/1', @routes.generate(:controller => 'posts', :action => 'show', :id => '1')
- assert_equal '/posts', @routes.generate(:controller => 'posts')
- assert_equal '/posts', @routes.generate(:controller => 'posts', :action => 'index')
- assert_equal '/posts', @routes.generate({:controller => 'posts'}, {:controller => 'posts', :action => 'index'})
- assert_equal '/posts/create', @routes.generate({:action => 'create'}, {:controller => 'posts'})
- assert_equal '/posts?foo=bar', @routes.generate(:controller => 'posts', :foo => 'bar')
- assert_equal '/posts?foo%5B%5D=bar&foo%5B%5D=baz', @routes.generate(:controller => 'posts', :foo => ['bar', 'baz'])
- assert_equal '/posts?page=2', @routes.generate(:controller => 'posts', :page => 2)
- assert_equal '/posts?q%5Bfoo%5D%5Ba%5D=b', @routes.generate(:controller => 'posts', :q => { :foo => { :a => 'b'}})
-
- assert_equal '/news.rss', @routes.generate(:controller => 'news', :action => 'index', :format => 'rss')
-
-
- assert_raise(ActionController::RoutingError) { @routes.generate({:action => 'index'}) }
+ assert_equal '/admin/users', url_for(@routes, { :use_route => 'admin_users' })
+ assert_equal '/admin/users', url_for(@routes, { :controller => 'admin/users' })
+ assert_equal '/admin/users', url_for(@routes, { :controller => 'admin/users', :action => 'index' })
+ assert_equal '/admin/users', url_for(@routes, { :action => 'index' }, { :controller => 'admin/users' })
+ assert_equal '/admin/users', url_for(@routes, { :controller => 'users', :action => 'index' }, { :controller => 'admin/accounts' })
+ assert_equal '/people', url_for(@routes, { :controller => '/people', :action => 'index' }, { :controller => 'admin/accounts' })
+
+ assert_equal '/admin/posts', url_for(@routes, { :controller => 'admin/posts' })
+ assert_equal '/admin/posts/new', url_for(@routes, { :controller => 'admin/posts', :action => 'new' })
+
+ assert_equal '/blog/2009', url_for(@routes, { :controller => 'posts', :action => 'show_date', :year => 2009 })
+ assert_equal '/blog/2009/1', url_for(@routes, { :controller => 'posts', :action => 'show_date', :year => 2009, :month => 1 })
+ assert_equal '/blog/2009/1/1', url_for(@routes, { :controller => 'posts', :action => 'show_date', :year => 2009, :month => 1, :day => 1 })
+
+ assert_equal '/archive/2010', url_for(@routes, { :controller => 'archive', :action => 'index', :year => '2010' })
+ assert_equal '/archive', url_for(@routes, { :controller => 'archive', :action => 'index' })
+ assert_equal '/archive?year=january', url_for(@routes, { :controller => 'archive', :action => 'index', :year => 'january' })
+
+ assert_equal '/people', url_for(@routes, { :controller => 'people', :action => 'index' })
+ assert_equal '/people', url_for(@routes, { :action => 'index' }, { :controller => 'people' })
+ assert_equal '/people', url_for(@routes, { :action => 'index' }, { :controller => 'people', :action => 'show', :id => '1' })
+ assert_equal '/people', url_for(@routes, { :controller => 'people', :action => 'index' }, { :controller => 'people', :action => 'show', :id => '1' })
+ assert_equal '/people', url_for(@routes, {}, { :controller => 'people', :action => 'index' })
+ assert_equal '/people/1', url_for(@routes, { :controller => 'people', :action => 'show' }, { :controller => 'people', :action => 'show', :id => '1' })
+ assert_equal '/people/new', url_for(@routes, { :use_route => 'new_person' })
+ assert_equal '/people/new', url_for(@routes, { :controller => 'people', :action => 'new' })
+ assert_equal '/people/1', url_for(@routes, { :use_route => 'person', :id => '1' })
+ assert_equal '/people/1', url_for(@routes, { :controller => 'people', :action => 'show', :id => '1' })
+ assert_equal '/people/1.xml', url_for(@routes, { :controller => 'people', :action => 'show', :id => '1', :format => 'xml' })
+ assert_equal '/people/1', url_for(@routes, { :controller => 'people', :action => 'show', :id => 1 })
+ assert_equal '/people/1', url_for(@routes, { :controller => 'people', :action => 'show', :id => Model.new('1') })
+ assert_equal '/people/1', url_for(@routes, { :action => 'show', :id => '1' }, { :controller => 'people', :action => 'index' })
+ assert_equal '/people/1', url_for(@routes, { :action => 'show', :id => 1 }, { :controller => 'people', :action => 'show', :id => '1' })
+ assert_equal '/people', url_for(@routes, { :controller => 'people', :action => 'index' }, { :controller => 'people', :action => 'show', :id => '1' })
+ assert_equal '/people/1', url_for(@routes, {}, { :controller => 'people', :action => 'show', :id => '1' })
+ assert_equal '/people/1', url_for(@routes, { :controller => 'people', :action => 'show' }, { :controller => 'people', :action => 'index', :id => '1' })
+ assert_equal '/people/1/edit', url_for(@routes, { :controller => 'people', :action => 'edit', :id => '1' })
+ assert_equal '/people/1/edit.xml', url_for(@routes, { :controller => 'people', :action => 'edit', :id => '1', :format => 'xml' })
+ assert_equal '/people/1/edit', url_for(@routes, { :use_route => 'edit_person', :id => '1' })
+ assert_equal '/people/1?legacy=true', url_for(@routes, { :controller => 'people', :action => 'show', :id => '1', :legacy => 'true' })
+ assert_equal '/people?legacy=true', url_for(@routes, { :controller => 'people', :action => 'index', :legacy => 'true' })
+
+ assert_equal '/id_default/2', url_for(@routes, { :controller => 'foo', :action => 'id_default', :id => '2' })
+ assert_equal '/id_default', url_for(@routes, { :controller => 'foo', :action => 'id_default', :id => '1' })
+ assert_equal '/id_default', url_for(@routes, { :controller => 'foo', :action => 'id_default', :id => 1 })
+ assert_equal '/id_default', url_for(@routes, { :controller => 'foo', :action => 'id_default' })
+ assert_equal '/optional/bar', url_for(@routes, { :controller => 'posts', :action => 'index', :optional => 'bar' })
+ assert_equal '/posts', url_for(@routes, { :controller => 'posts', :action => 'index' })
+
+ assert_equal '/project', url_for(@routes, { :controller => 'project', :action => 'index' })
+ assert_equal '/projects/1', url_for(@routes, { :controller => 'project', :action => 'index', :project_id => '1' })
+ assert_equal '/projects/1', url_for(@routes, { :controller => 'project', :action => 'index'}, {:project_id => '1' })
+ assert_raise(ActionController::RoutingError) { url_for(@routes, { :use_route => 'project', :controller => 'project', :action => 'index' }) }
+ assert_equal '/projects/1', url_for(@routes, { :use_route => 'project', :controller => 'project', :action => 'index', :project_id => '1' })
+ assert_equal '/projects/1', url_for(@routes, { :use_route => 'project', :controller => 'project', :action => 'index' }, { :project_id => '1' })
+
+ assert_equal '/clients', url_for(@routes, { :controller => 'projects', :action => 'index' })
+ assert_equal '/clients?project_id=1', url_for(@routes, { :controller => 'projects', :action => 'index', :project_id => '1' })
+ assert_equal '/clients', url_for(@routes, { :controller => 'projects', :action => 'index' }, { :project_id => '1' })
+ assert_equal '/clients', url_for(@routes, { :action => 'index' }, { :controller => 'projects', :action => 'index', :project_id => '1' })
+
+ assert_equal '/comment/20', url_for(@routes, { :id => 20 }, { :controller => 'comments', :action => 'show' })
+ assert_equal '/comment/20', url_for(@routes, { :controller => 'comments', :id => 20, :action => 'show' })
+ assert_equal '/comments/boo', url_for(@routes, { :controller => 'comments', :action => 'boo' })
+
+ assert_equal '/ws/posts/show/1', url_for(@routes, { :controller => 'posts', :action => 'show', :id => '1', :ws => true })
+ assert_equal '/ws/posts', url_for(@routes, { :controller => 'posts', :action => 'index', :ws => true })
+
+ assert_equal '/account', url_for(@routes, { :controller => 'account', :action => 'subscription' })
+ assert_equal '/account/billing', url_for(@routes, { :controller => 'account', :action => 'billing' })
+
+ assert_equal '/pages/1/notes/show/1', url_for(@routes, { :page_id => '1', :controller => 'notes', :action => 'show', :id => '1' })
+ assert_equal '/pages/1/notes/list', url_for(@routes, { :page_id => '1', :controller => 'notes', :action => 'list' })
+ assert_equal '/pages/1/notes', url_for(@routes, { :page_id => '1', :controller => 'notes', :action => 'index' })
+ assert_equal '/pages/1/notes', url_for(@routes, { :page_id => '1', :controller => 'notes' })
+ assert_equal '/notes', url_for(@routes, { :page_id => nil, :controller => 'notes' })
+ assert_equal '/notes', url_for(@routes, { :controller => 'notes' })
+ assert_equal '/notes/print', url_for(@routes, { :controller => 'notes', :action => 'print' })
+ assert_equal '/notes/print', url_for(@routes, {}, { :controller => 'notes', :action => 'print' })
+
+ assert_equal '/notes/index/1', url_for(@routes, { :controller => 'notes' }, { :controller => 'notes', :id => '1' })
+ assert_equal '/notes/index/1', url_for(@routes, { :controller => 'notes' }, { :controller => 'notes', :id => '1', :foo => 'bar' })
+ assert_equal '/notes/index/1', url_for(@routes, { :controller => 'notes' }, { :controller => 'notes', :id => '1' })
+ assert_equal '/notes/index/1', url_for(@routes, { :action => 'index' }, { :controller => 'notes', :id => '1' })
+ assert_equal '/notes/index/1', url_for(@routes, {}, { :controller => 'notes', :id => '1' })
+ assert_equal '/notes/show/1', url_for(@routes, {}, { :controller => 'notes', :action => 'show', :id => '1' })
+ assert_equal '/notes/index/1', url_for(@routes, { :controller => 'notes', :id => '1' }, { :foo => 'bar' })
+ assert_equal '/posts', url_for(@routes, { :controller => 'posts' }, { :controller => 'notes', :action => 'show', :id => '1' })
+ assert_equal '/notes/list', url_for(@routes, { :action => 'list' }, { :controller => 'notes', :action => 'show', :id => '1' })
+
+ assert_equal '/posts/ping', url_for(@routes, { :controller => 'posts', :action => 'ping' })
+ assert_equal '/posts/show/1', url_for(@routes, { :controller => 'posts', :action => 'show', :id => '1' })
+ assert_equal '/posts', url_for(@routes, { :controller => 'posts' })
+ assert_equal '/posts', url_for(@routes, { :controller => 'posts', :action => 'index' })
+ assert_equal '/posts', url_for(@routes, { :controller => 'posts' }, { :controller => 'posts', :action => 'index' })
+ assert_equal '/posts/create', url_for(@routes, { :action => 'create' }, { :controller => 'posts' })
+ assert_equal '/posts?foo=bar', url_for(@routes, { :controller => 'posts', :foo => 'bar' })
+ assert_equal '/posts?foo%5B%5D=bar&foo%5B%5D=baz', url_for(@routes, { :controller => 'posts', :foo => ['bar', 'baz'] })
+ assert_equal '/posts?page=2', url_for(@routes, { :controller => 'posts', :page => 2 })
+ assert_equal '/posts?q%5Bfoo%5D%5Ba%5D=b', url_for(@routes, { :controller => 'posts', :q => { :foo => { :a => 'b'}} })
+
+ assert_equal '/news.rss', url_for(@routes, { :controller => 'news', :action => 'index', :format => 'rss' })
+
+
+ assert_raise(ActionController::RoutingError) { url_for(@routes, { :action => 'index' }) }
end
def test_generate_extras
diff --git a/actionpack/test/controller/runner_test.rb b/actionpack/test/controller/runner_test.rb
new file mode 100644
index 0000000000..24c220dcd5
--- /dev/null
+++ b/actionpack/test/controller/runner_test.rb
@@ -0,0 +1,22 @@
+require 'abstract_unit'
+require 'action_dispatch/testing/integration'
+
+module ActionDispatch
+ class RunnerTest < Test::Unit::TestCase
+ class MyRunner
+ include Integration::Runner
+
+ def initialize(session)
+ @integration_session = session
+ end
+
+ def hi; end
+ end
+
+ def test_respond_to?
+ runner = MyRunner.new(Class.new { def x; end }.new)
+ assert runner.respond_to?(:hi)
+ assert runner.respond_to?(:x)
+ end
+ end
+end
diff --git a/actionpack/test/controller/url_for_test.rb b/actionpack/test/controller/url_for_test.rb
index 4c07ca4cc3..1f62d29e80 100644
--- a/actionpack/test/controller/url_for_test.rb
+++ b/actionpack/test/controller/url_for_test.rb
@@ -17,7 +17,7 @@ module AbstractController
end
def test_exception_is_thrown_without_host
- assert_raise RuntimeError do
+ assert_raise ArgumentError do
W.new.url_for :controller => 'c', :action => 'a', :id => 'i'
end
end
@@ -60,6 +60,27 @@ module AbstractController
)
end
+ def test_subdomain_may_be_changed
+ add_host!
+ assert_equal('http://api.basecamphq.com/c/a/i',
+ W.new.url_for(:subdomain => 'api', :controller => 'c', :action => 'a', :id => 'i')
+ )
+ end
+
+ def test_domain_may_be_changed
+ add_host!
+ assert_equal('http://www.37signals.com/c/a/i',
+ W.new.url_for(:domain => '37signals.com', :controller => 'c', :action => 'a', :id => 'i')
+ )
+ end
+
+ def test_tld_length_may_be_changed
+ add_host!
+ assert_equal('http://mobile.www.basecamphq.com/c/a/i',
+ W.new.url_for(:subdomain => 'mobile', :tld_length => 2, :controller => 'c', :action => 'a', :id => 'i')
+ )
+ end
+
def test_port
add_host!
assert_equal('http://www.basecamphq.com:3000/c/a/i',
diff --git a/actionpack/test/dispatch/callbacks_test.rb b/actionpack/test/dispatch/callbacks_test.rb
index d3aa55a1ba..5becb621de 100644
--- a/actionpack/test/dispatch/callbacks_test.rb
+++ b/actionpack/test/dispatch/callbacks_test.rb
@@ -1,6 +1,6 @@
require 'abstract_unit'
-class DispatcherTest < Test::Unit::TestCase
+class DispatcherTest < ActiveSupport::TestCase
class Foo
cattr_accessor :a, :b
end
@@ -13,65 +13,9 @@ class DispatcherTest < Test::Unit::TestCase
def setup
Foo.a, Foo.b = 0, 0
- ActionDispatch::Callbacks.reset_callbacks(:prepare)
ActionDispatch::Callbacks.reset_callbacks(:call)
end
- def test_prepare_callbacks_with_cache_classes
- a = b = c = nil
- ActionDispatch::Callbacks.to_prepare { |*args| a = b = c = 1 }
- ActionDispatch::Callbacks.to_prepare { |*args| b = c = 2 }
- ActionDispatch::Callbacks.to_prepare { |*args| c = 3 }
-
- # Ensure to_prepare callbacks are not run when defined
- assert_nil a || b || c
-
- # Run callbacks
- dispatch
-
- assert_equal 1, a
- assert_equal 2, b
- assert_equal 3, c
-
- # Make sure they are only run once
- a = b = c = nil
- dispatch
- assert_nil a || b || c
- end
-
- def test_prepare_callbacks_without_cache_classes
- a = b = c = nil
- ActionDispatch::Callbacks.to_prepare { |*args| a = b = c = 1 }
- ActionDispatch::Callbacks.to_prepare { |*args| b = c = 2 }
- ActionDispatch::Callbacks.to_prepare { |*args| c = 3 }
-
- # Ensure to_prepare callbacks are not run when defined
- assert_nil a || b || c
-
- # Run callbacks
- dispatch(false)
-
- assert_equal 1, a
- assert_equal 2, b
- assert_equal 3, c
-
- # Make sure they are run again
- a = b = c = nil
- dispatch(false)
- assert_equal 1, a
- assert_equal 2, b
- assert_equal 3, c
- end
-
- def test_to_prepare_with_identifier_replaces
- ActionDispatch::Callbacks.to_prepare(:unique_id) { |*args| Foo.a, Foo.b = 1, 1 }
- ActionDispatch::Callbacks.to_prepare(:unique_id) { |*args| Foo.a = 2 }
-
- dispatch
- assert_equal 2, Foo.a
- assert_equal 0, Foo.b
- end
-
def test_before_and_after_callbacks
ActionDispatch::Callbacks.before { |*args| Foo.a += 1; Foo.b += 1 }
ActionDispatch::Callbacks.after { |*args| Foo.a += 1; Foo.b += 1 }
@@ -85,10 +29,20 @@ class DispatcherTest < Test::Unit::TestCase
assert_equal 4, Foo.b
end
+ def test_to_prepare_deprecation
+ prepared = false
+ assert_deprecated do
+ ActionDispatch::Callbacks.to_prepare { prepared = true }
+ end
+
+ ActionDispatch::Reloader.prepare!
+ assert prepared
+ end
+
private
- def dispatch(cache_classes = true, &block)
- @dispatcher ||= ActionDispatch::Callbacks.new(block || DummyApp.new, !cache_classes)
+ def dispatch(&block)
+ @dispatcher ||= ActionDispatch::Callbacks.new(block || DummyApp.new)
@dispatcher.call({'rack.input' => StringIO.new('')})
end
diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb
index 5ec7f12cc1..e2040401c7 100644
--- a/actionpack/test/dispatch/cookies_test.rb
+++ b/actionpack/test/dispatch/cookies_test.rb
@@ -295,6 +295,27 @@ class CookiesTest < ActionController::TestCase
assert_cookie_header "user_name=rizwanreza; domain=.nextangle.local; path=/"
end
+ def test_cookie_with_all_domain_option_using_localhost
+ @request.host = "localhost"
+ get :set_cookie_with_domain
+ assert_response :success
+ assert_cookie_header "user_name=rizwanreza; path=/"
+ end
+
+ def test_cookie_with_all_domain_option_using_ipv4_address
+ @request.host = "192.168.1.1"
+ get :set_cookie_with_domain
+ assert_response :success
+ assert_cookie_header "user_name=rizwanreza; path=/"
+ end
+
+ def test_cookie_with_all_domain_option_using_ipv6_address
+ @request.host = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
+ get :set_cookie_with_domain
+ assert_response :success
+ assert_cookie_header "user_name=rizwanreza; path=/"
+ end
+
def test_deleting_cookie_with_all_domain_option
get :delete_cookie_with_domain
assert_response :success
diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb
index 4c2b95550c..9782f328fc 100644
--- a/actionpack/test/dispatch/mime_type_test.rb
+++ b/actionpack/test/dispatch/mime_type_test.rb
@@ -6,10 +6,57 @@ class MimeTypeTest < ActiveSupport::TestCase
test "parse single" do
Mime::LOOKUP.keys.each do |mime_type|
- assert_equal [Mime::Type.lookup(mime_type)], Mime::Type.parse(mime_type)
+ unless mime_type == 'image/*'
+ assert_equal [Mime::Type.lookup(mime_type)], Mime::Type.parse(mime_type)
+ end
+ end
+ end
+
+ test "unregister" do
+ begin
+ Mime::Type.register("text/x-mobile", :mobile)
+ assert defined?(Mime::MOBILE)
+ assert_equal Mime::MOBILE, Mime::LOOKUP['text/x-mobile']
+ assert_equal Mime::MOBILE, Mime::EXTENSION_LOOKUP['mobile']
+
+ Mime::Type.unregister(:mobile)
+ assert !defined?(Mime::MOBILE), "Mime::MOBILE should not be defined"
+ assert !Mime::LOOKUP.has_key?('text/x-mobile'), "Mime::LOOKUP should not have key ['text/x-mobile]"
+ assert !Mime::EXTENSION_LOOKUP.has_key?('mobile'), "Mime::EXTENSION_LOOKUP should not have key ['mobile]"
+ ensure
+ Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
+ Mime::LOOKUP.reject!{|key,_| key == 'text/x-mobile'}
end
end
+ test "parse text with trailing star at the beginning" do
+ accept = "text/*, text/html, application/json, multipart/form-data"
+ expect = [Mime::HTML, Mime::TEXT, Mime::JS, Mime::CSS, Mime::ICS, Mime::CSV, Mime::XML, Mime::YAML, Mime::JSON, Mime::MULTIPART_FORM]
+ parsed = Mime::Type.parse(accept)
+ assert_equal expect, parsed
+ end
+
+ test "parse text with trailing star in the end" do
+ accept = "text/html, application/json, multipart/form-data, text/*"
+ expect = [Mime::HTML, Mime::JSON, Mime::MULTIPART_FORM, Mime::TEXT, Mime::JS, Mime::CSS, Mime::ICS, Mime::CSV, Mime::XML, Mime::YAML]
+ parsed = Mime::Type.parse(accept)
+ assert_equal expect, parsed
+ end
+
+ test "parse text with trailing star" do
+ accept = "text/*"
+ expect = [Mime::HTML, Mime::TEXT, Mime::JS, Mime::CSS, Mime::ICS, Mime::CSV, Mime::XML, Mime::YAML, Mime::JSON]
+ parsed = Mime::Type.parse(accept)
+ assert_equal expect, parsed
+ end
+
+ test "parse application with trailing star" do
+ accept = "application/*"
+ expect = [Mime::HTML, Mime::JS, Mime::XML, Mime::RSS, Mime::ATOM, Mime::YAML, Mime::URL_ENCODED_FORM, Mime::JSON, Mime::PDF]
+ parsed = Mime::Type.parse(accept)
+ assert_equal expect, parsed
+ end
+
test "parse without q" do
accept = "text/xml,application/xhtml+xml,text/yaml,application/xml,text/html,image/png,text/plain,application/pdf,*/*"
expect = [Mime::HTML, Mime::XML, Mime::YAML, Mime::PNG, Mime::TEXT, Mime::PDF, Mime::ALL]
@@ -44,7 +91,7 @@ class MimeTypeTest < ActiveSupport::TestCase
assert_equal Mime::GIF, Mime::SET.last
end
ensure
- Mime.module_eval { remove_const :GIF if const_defined?(:GIF) }
+ Mime::Type.unregister(:gif)
end
end
diff --git a/actionpack/test/dispatch/mount_test.rb b/actionpack/test/dispatch/mount_test.rb
index 0f584af31e..1a032539b9 100644
--- a/actionpack/test/dispatch/mount_test.rb
+++ b/actionpack/test/dispatch/mount_test.rb
@@ -2,6 +2,17 @@ require 'abstract_unit'
class TestRoutingMount < ActionDispatch::IntegrationTest
Router = ActionDispatch::Routing::RouteSet.new
+
+ class FakeEngine
+ def self.routes
+ Object.new
+ end
+
+ def self.call(env)
+ [200, {"Content-Type" => "text/html"}, ["OK"]]
+ end
+ end
+
Router.draw do
SprocketsApp = lambda { |env|
[200, {"Content-Type" => "text/html"}, ["#{env["SCRIPT_NAME"]} -- #{env["PATH_INFO"]}"]]
@@ -10,6 +21,8 @@ class TestRoutingMount < ActionDispatch::IntegrationTest
mount SprocketsApp, :at => "/sprockets"
mount SprocketsApp => "/shorthand"
+ mount FakeEngine, :at => "/fakeengine"
+
scope "/its_a" do
mount SprocketsApp, :at => "/sprocket"
end
@@ -33,4 +46,9 @@ class TestRoutingMount < ActionDispatch::IntegrationTest
get "/shorthand/omg"
assert_equal "/shorthand -- /omg", response.body
end
+
+ def test_with_fake_engine_does_not_call_invalid_method
+ get "/fakeengine"
+ assert_equal "OK", response.body
+ end
end \ No newline at end of file
diff --git a/actionpack/test/dispatch/reloader_test.rb b/actionpack/test/dispatch/reloader_test.rb
new file mode 100644
index 0000000000..995b19030c
--- /dev/null
+++ b/actionpack/test/dispatch/reloader_test.rb
@@ -0,0 +1,124 @@
+require 'abstract_unit'
+
+class ReloaderTest < Test::Unit::TestCase
+ Reloader = ActionDispatch::Reloader
+
+ def test_prepare_callbacks
+ a = b = c = nil
+ Reloader.to_prepare { |*args| a = b = c = 1 }
+ Reloader.to_prepare { |*args| b = c = 2 }
+ Reloader.to_prepare { |*args| c = 3 }
+
+ # Ensure to_prepare callbacks are not run when defined
+ assert_nil a || b || c
+
+ # Run callbacks
+ call_and_return_body
+
+ assert_equal 1, a
+ assert_equal 2, b
+ assert_equal 3, c
+ end
+
+ class MyBody < Array
+ def initialize(&block)
+ @on_close = block
+ end
+
+ def foo
+ "foo"
+ end
+
+ def bar
+ "bar"
+ end
+
+ def close
+ @on_close.call if @on_close
+ end
+ end
+
+ def test_returned_body_object_always_responds_to_close
+ body = call_and_return_body
+ assert_respond_to body, :close
+ end
+
+ def test_returned_body_object_behaves_like_underlying_object
+ body = call_and_return_body do
+ b = MyBody.new
+ b << "hello"
+ b << "world"
+ [200, { "Content-Type" => "text/html" }, b]
+ end
+ assert_equal 2, body.size
+ assert_equal "hello", body[0]
+ assert_equal "world", body[1]
+ assert_equal "foo", body.foo
+ assert_equal "bar", body.bar
+ end
+
+ def test_it_calls_close_on_underlying_object_when_close_is_called_on_body
+ close_called = false
+ body = call_and_return_body do
+ b = MyBody.new do
+ close_called = true
+ end
+ [200, { "Content-Type" => "text/html" }, b]
+ end
+ body.close
+ assert close_called
+ end
+
+ def test_returned_body_object_responds_to_all_methods_supported_by_underlying_object
+ body = call_and_return_body do
+ [200, { "Content-Type" => "text/html" }, MyBody.new]
+ end
+ assert_respond_to body, :size
+ assert_respond_to body, :each
+ assert_respond_to body, :foo
+ assert_respond_to body, :bar
+ end
+
+ def test_cleanup_callbacks_are_called_when_body_is_closed
+ cleaned = false
+ Reloader.to_cleanup { cleaned = true }
+
+ body = call_and_return_body
+ assert !cleaned
+
+ body.close
+ assert cleaned
+ end
+
+ def test_prepare_callbacks_arent_called_when_body_is_closed
+ prepared = false
+ Reloader.to_prepare { prepared = true }
+
+ body = call_and_return_body
+ prepared = false
+
+ body.close
+ assert !prepared
+ end
+
+ def test_manual_reloading
+ prepared = cleaned = false
+ Reloader.to_prepare { prepared = true }
+ Reloader.to_cleanup { cleaned = true }
+
+ Reloader.prepare!
+ assert prepared
+ assert !cleaned
+
+ prepared = cleaned = false
+ Reloader.cleanup!
+ assert !prepared
+ assert cleaned
+ end
+
+ private
+ def call_and_return_body(&block)
+ @reloader ||= Reloader.new(block || proc {[200, {}, 'response']})
+ @reloader.call({'rack.input' => StringIO.new('')})[2]
+ end
+end
diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb
index 4764a8c2a8..75b674ec1a 100644
--- a/actionpack/test/dispatch/request_test.rb
+++ b/actionpack/test/dispatch/request_test.rb
@@ -1,6 +1,31 @@
require 'abstract_unit'
class RequestTest < ActiveSupport::TestCase
+
+ def url_for(options = {})
+ options.reverse_merge!(:host => 'www.example.com')
+ ActionDispatch::Http::URL.url_for(options)
+ end
+
+ test "url_for class method" do
+ e = assert_raise(ArgumentError) { url_for(:host => nil) }
+ assert_match(/Please provide the :host parameter/, e.message)
+
+ assert_equal '/books', url_for(:only_path => true, :path => '/books')
+
+ assert_equal 'http://www.example.com', url_for
+ assert_equal 'http://api.example.com', url_for(:subdomain => 'api')
+ assert_equal 'http://www.ror.com', url_for(:domain => 'ror.com')
+ assert_equal 'http://api.ror.co.uk', url_for(:host => 'www.ror.co.uk', :subdomain => 'api', :tld_length => 2)
+ assert_equal 'http://www.example.com:8080', url_for(:port => 8080)
+ assert_equal 'https://www.example.com', url_for(:protocol => 'https')
+ assert_equal 'http://www.example.com/docs', url_for(:path => '/docs')
+ assert_equal 'http://www.example.com#signup', url_for(:anchor => 'signup')
+ assert_equal 'http://www.example.com/', url_for(:trailing_slash => true)
+ assert_equal 'http://dhh:supersecret@www.example.com', url_for(:user => 'dhh', :password => 'supersecret')
+ assert_equal 'http://www.example.com?search=books', url_for(:params => { :search => 'books' })
+ end
+
test "remote ip" do
request = stub_request 'REMOTE_ADDR' => '1.2.3.4'
assert_equal '1.2.3.4', request.remote_ip
@@ -164,12 +189,20 @@ class RequestTest < ActiveSupport::TestCase
assert !request.standard_port?
end
+ test "optional port" do
+ request = stub_request 'HTTP_HOST' => 'www.example.org:80'
+ assert_equal nil, request.optional_port
+
+ request = stub_request 'HTTP_HOST' => 'www.example.org:8080'
+ assert_equal 8080, request.optional_port
+ end
+
test "port string" do
request = stub_request 'HTTP_HOST' => 'www.example.org:80'
- assert_equal "", request.port_string
+ assert_equal '', request.port_string
request = stub_request 'HTTP_HOST' => 'www.example.org:8080'
- assert_equal ":8080", request.port_string
+ assert_equal ':8080', request.port_string
end
test "full path" do
@@ -392,7 +425,7 @@ class RequestTest < ActiveSupport::TestCase
mock_rack_env = { "QUERY_STRING" => "x[y]=1&x[y][][w]=2", "rack.input" => "foo" }
request = nil
begin
- request = stub_request(mock_rack_env)
+ request = stub_request(mock_rack_env)
request.parameters
rescue TypeError => e
# rack will raise a TypeError when parsing this query string
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index 0ac8c249cb..4bf7880294 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -13,6 +13,12 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end
end
+ class YoutubeFavoritesRedirector
+ def self.call(params, request)
+ "http://www.youtube.com/watch?v=#{params[:youtube_id]}"
+ end
+ end
+
stub_controllers do |routes|
Routes = routes
Routes.draw do
@@ -54,6 +60,16 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
match 'account/login', :to => redirect("/login")
match 'secure', :to => redirect("/secure/login")
+ match 'mobile', :to => redirect(:subdomain => 'mobile')
+ match 'documentation', :to => redirect(:domain => 'example-documentation.com', :path => '')
+ match 'new_documentation', :to => redirect(:path => '/documentation/new')
+ match 'super_new_documentation', :to => redirect(:host => 'super-docs.com')
+
+ match 'stores/:name', :to => redirect(:subdomain => 'stores', :path => '/%{name}')
+ match 'stores/:name(*rest)', :to => redirect(:subdomain => 'stores', :path => '/%{name}%{rest}')
+
+ match 'youtube_favorites/:youtube_id/:name', :to => redirect(YoutubeFavoritesRedirector)
+
constraints(lambda { |req| true }) do
match 'account/overview'
end
@@ -155,6 +171,11 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end
resources :replies do
+ collection do
+ get 'page/:page' => 'replies#index', :page => %r{\d+}
+ get ':page' => 'replies#index', :page => %r{\d+}
+ end
+
new do
post :preview
end
@@ -662,6 +683,55 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end
end
+ def test_redirect_hash_with_subdomain
+ with_test_routes do
+ get '/mobile'
+ verify_redirect 'http://mobile.example.com/mobile'
+ end
+ end
+
+ def test_redirect_hash_with_domain_and_path
+ with_test_routes do
+ get '/documentation'
+ verify_redirect 'http://www.example-documentation.com'
+ end
+ end
+
+ def test_redirect_hash_with_path
+ with_test_routes do
+ get '/new_documentation'
+ verify_redirect 'http://www.example.com/documentation/new'
+ end
+ end
+
+ def test_redirect_hash_with_host
+ with_test_routes do
+ get '/super_new_documentation?section=top'
+ verify_redirect 'http://super-docs.com/super_new_documentation?section=top'
+ end
+ end
+
+ def test_redirect_hash_path_substitution
+ with_test_routes do
+ get '/stores/iernest'
+ verify_redirect 'http://stores.example.com/iernest'
+ end
+ end
+
+ def test_redirect_hash_path_substitution_with_catch_all
+ with_test_routes do
+ get '/stores/iernest/products'
+ verify_redirect 'http://stores.example.com/iernest/products'
+ end
+ end
+
+ def test_redirect_class
+ with_test_routes do
+ get '/youtube_favorites/oHg5SJYRHA0/rick-rolld'
+ verify_redirect 'http://www.youtube.com/watch?v=oHg5SJYRHA0'
+ end
+ end
+
def test_openid
with_test_routes do
get '/openid/login'
@@ -1241,6 +1311,12 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end
end
+ def test_dynamically_generated_helpers_on_collection_do_not_clobber_resources_url_helper
+ with_test_routes do
+ assert_equal '/replies', replies_path
+ end
+ end
+
def test_scoped_controller_with_namespace_and_action
with_test_routes do
assert_equal '/account/twitter/callback', account_callback_path("twitter")
diff --git a/actionpack/test/fixtures/star_star_mime/index.js.erb b/actionpack/test/fixtures/star_star_mime/index.js.erb
new file mode 100644
index 0000000000..4da4181f56
--- /dev/null
+++ b/actionpack/test/fixtures/star_star_mime/index.js.erb
@@ -0,0 +1 @@
+function addition(a,b){ return a+b; }
diff --git a/actionpack/test/fixtures/test/scoped_translation.erb b/actionpack/test/fixtures/test/scoped_translation.erb
deleted file mode 100644
index 3be63ab3cc..0000000000
--- a/actionpack/test/fixtures/test/scoped_translation.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= t('.foo.bar').join %> \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/translation.erb b/actionpack/test/fixtures/test/translation.erb
deleted file mode 100644
index 81a837d1ff..0000000000
--- a/actionpack/test/fixtures/test/translation.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= t('.helper') %> \ No newline at end of file
diff --git a/actionpack/test/fixtures/translations/templates/array.erb b/actionpack/test/fixtures/translations/templates/array.erb
new file mode 100644
index 0000000000..d86045a172
--- /dev/null
+++ b/actionpack/test/fixtures/translations/templates/array.erb
@@ -0,0 +1 @@
+<%= t('.foo.bar') %>
diff --git a/actionpack/test/fixtures/translations/templates/found.erb b/actionpack/test/fixtures/translations/templates/found.erb
new file mode 100644
index 0000000000..080c9c0aee
--- /dev/null
+++ b/actionpack/test/fixtures/translations/templates/found.erb
@@ -0,0 +1 @@
+<%= t('.foo') %>
diff --git a/actionpack/test/fixtures/translations/templates/missing.erb b/actionpack/test/fixtures/translations/templates/missing.erb
new file mode 100644
index 0000000000..0f3f17f8ef
--- /dev/null
+++ b/actionpack/test/fixtures/translations/templates/missing.erb
@@ -0,0 +1 @@
+<%= t('.missing') %>
diff --git a/actionpack/test/lib/controller/fake_models.rb b/actionpack/test/lib/controller/fake_models.rb
index ae0c38184d..67baf369e2 100644
--- a/actionpack/test/lib/controller/fake_models.rb
+++ b/actionpack/test/lib/controller/fake_models.rb
@@ -194,3 +194,9 @@ class ArelLike
a.each { |i| yield i }
end
end
+
+class RenderJsonTestException < Exception
+ def to_json(options = nil)
+ return { :error => self.class.name, :message => self.to_s }.to_json
+ end
+end
diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb
index 6d3b26e131..c9dd27cf2a 100644
--- a/actionpack/test/template/lookup_context_test.rb
+++ b/actionpack/test/template/lookup_context_test.rb
@@ -180,6 +180,16 @@ class LookupContextTest < ActiveSupport::TestCase
assert_not_equal template, old_template
end
+
+ test "data can be stored in cached templates" do
+ template = @lookup_context.find("hello_world", "test")
+ template.data["cached"] = "data"
+ assert_equal "Hello world!", template.source
+
+ template = @lookup_context.find("hello_world", "test")
+ assert_equal "data", template.data["cached"]
+ assert_equal "Hello world!", template.source
+ end
end
class LookupContextWithFalseCaching < ActiveSupport::TestCase
@@ -205,7 +215,7 @@ class LookupContextWithFalseCaching < ActiveSupport::TestCase
assert_equal "Bar", template.source
end
- test "if no template was found in the second lookup, give it higher preference" do
+ test "if no template was found in the second lookup, with no cache, raise error" do
template = @lookup_context.find("foo", "test", true)
assert_equal "Foo", template.source
@@ -215,7 +225,7 @@ class LookupContextWithFalseCaching < ActiveSupport::TestCase
end
end
- test "if no template was cached in the first lookup, do not use the cache in the second" do
+ test "if no template was cached in the first lookup, retrieval should work in the second call" do
@resolver.hash.clear
assert_raise ActionView::MissingTemplate do
@lookup_context.find("foo", "test", true)
@@ -225,4 +235,19 @@ class LookupContextWithFalseCaching < ActiveSupport::TestCase
template = @lookup_context.find("foo", "test", true)
assert_equal "Foo", template.source
end
+
+ test "data can be stored as long as template was not updated" do
+ template = @lookup_context.find("foo", "test", true)
+ template.data["cached"] = "data"
+ assert_equal "Foo", template.source
+
+ template = @lookup_context.find("foo", "test", true)
+ assert_equal "data", template.data["cached"]
+ assert_equal "Foo", template.source
+
+ @resolver.hash["test/_foo.erb"][1] = Time.now.utc
+ template = @lookup_context.find("foo", "test", true)
+ assert_nil template.data["cached"]
+ assert_equal "Foo", template.source
+ end
end \ No newline at end of file
diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb
index ab127521ad..156b7cb5ff 100644
--- a/actionpack/test/template/number_helper_test.rb
+++ b/actionpack/test/template/number_helper_test.rb
@@ -100,6 +100,8 @@ class NumberHelperTest < ActionView::TestCase
assert_equal("0", number_with_precision(0, :precision => 0))
assert_equal("0.00100", number_with_precision(0.001, :precision => 5))
assert_equal("0.001", number_with_precision(0.00111, :precision => 3))
+ assert_equal("10.00", number_with_precision(9.995, :precision => 2))
+ assert_equal("11.00", number_with_precision(10.995, :precision => 2))
end
def test_number_with_precision_with_custom_delimiter_and_separator
@@ -125,6 +127,9 @@ class NumberHelperTest < ActionView::TestCase
assert_equal "0.0001", number_with_precision(0.0001, :precision => 1, :significant => true )
assert_equal "0.000100", number_with_precision(0.0001, :precision => 3, :significant => true )
assert_equal "0.0001", number_with_precision(0.0001111, :precision => 1, :significant => true )
+ assert_equal "10.0", number_with_precision(9.995, :precision => 3, :significant => true)
+ assert_equal "9.99", number_with_precision(9.994, :precision => 3, :significant => true)
+ assert_equal "11.0", number_with_precision(10.995, :precision => 3, :significant => true)
end
def test_number_with_precision_with_strip_insignificant_zeros
diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb
index 8087429d62..38bedd2e4e 100644
--- a/actionpack/test/template/render_test.rb
+++ b/actionpack/test/template/render_test.rb
@@ -225,6 +225,11 @@ module RenderTestCases
%'"#{template.class} #{view.class}"'
end
+ def test_render_inline_with_render_from_to_proc
+ ActionView::Template.register_template_handler :ruby_handler, :source.to_proc
+ assert_equal '3', @view.render(:inline => "(1 + 2).to_s", :type => :ruby_handler)
+ end
+
def test_render_inline_with_template_handler_with_view
ActionView::Template.register_template_handler :with_view, WithViewHandler
assert_equal 'ActionView::Template ActionView::Base', @view.render(:inline => "Hello, World!", :type => :with_view)
diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb
index 763080550b..9b5c6d127c 100644
--- a/actionpack/test/template/translation_helper_test.rb
+++ b/actionpack/test/template/translation_helper_test.rb
@@ -4,60 +4,78 @@ class TranslationHelperTest < ActiveSupport::TestCase
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TranslationHelper
- attr_reader :request
+ attr_reader :request, :view
+
def setup
+ I18n.backend.store_translations(:en,
+ :translations => {
+ :templates => {
+ :found => { :foo => 'Foo' },
+ :array => { :foo => { :bar => 'Foo Bar' } }
+ },
+ :foo => 'Foo',
+ :hello => '<a>Hello World</a>',
+ :html => '<a>Hello World</a>',
+ :hello_html => '<a>Hello World</a>',
+ :array_html => %w(foo bar),
+ :array => %w(foo bar)
+ }
+ )
+ @view = ::ActionView::Base.new(ActionController::Base.view_paths, {})
end
- def test_delegates_to_i18n_setting_the_raise_option
- I18n.expects(:translate).with(:foo, :locale => 'en', :raise => true).returns("")
+ def test_delegates_to_i18n_setting_the_rescue_format_option_to_html
+ I18n.expects(:translate).with(:foo, :locale => 'en', :rescue_format => :html).returns("")
translate :foo, :locale => 'en'
end
+ def test_delegates_localize_to_i18n
+ @time = Time.utc(2008, 7, 8, 12, 18, 38)
+ I18n.expects(:localize).with(@time)
+ localize @time
+ end
+
def test_returns_missing_translation_message_wrapped_into_span
- expected = '<span class="translation_missing">en, foo</span>'
- assert_equal expected, translate(:foo)
+ expected = '<span class="translation_missing" title="translation missing: en.translations.missing">Missing</span>'
+ assert_equal expected, translate(:"translations.missing")
+ end
+
+ def test_returns_missing_translation_message_using_nil_as_rescue_format
+ expected = 'translation missing: en.translations.missing'
+ assert_equal expected, translate(:"translations.missing", :rescue_format => nil)
end
def test_translation_returning_an_array
- I18n.expects(:translate).with(:foo, :raise => true).returns(["foo", "bar"])
- assert_equal ["foo", "bar"], translate(:foo)
+ expected = %w(foo bar)
+ assert_equal expected, translate(:"translations.array")
end
- def test_delegates_localize_to_i18n
- @time = Time.utc(2008, 7, 8, 12, 18, 38)
- I18n.expects(:localize).with(@time)
- localize @time
+ def test_finds_translation_scoped_by_partial
+ assert_equal 'Foo', view.render(:file => 'translations/templates/found').strip
end
- def test_scoping_by_partial
- I18n.expects(:translate).with("test.translation.helper", :raise => true).returns("helper")
- @view = ::ActionView::Base.new(ActionController::Base.view_paths, {})
- assert_equal "helper", @view.render(:file => "test/translation")
+ def test_finds_array_of_translations_scoped_by_partial
+ assert_equal 'Foo Bar', @view.render(:file => 'translations/templates/array').strip
end
- def test_scoping_by_partial_of_an_array
- I18n.expects(:translate).with("test.scoped_translation.foo.bar", :raise => true).returns(["foo", "bar"])
- @view = ::ActionView::Base.new(ActionController::Base.view_paths, {})
- assert_equal "foobar", @view.render(:file => "test/scoped_translation")
+ def test_missing_translation_scoped_by_partial
+ expected = '<span class="translation_missing" title="translation missing: en.translations.templates.missing.missing">Missing</span>'
+ assert_equal expected, view.render(:file => 'translations/templates/missing').strip
end
def test_translate_does_not_mark_plain_text_as_safe_html
- I18n.expects(:translate).with("hello", :raise => true).returns("Hello World")
- assert_equal false, translate("hello").html_safe?
+ assert_equal false, translate(:'translations.hello').html_safe?
end
def test_translate_marks_translations_named_html_as_safe_html
- I18n.expects(:translate).with("html", :raise => true).returns("<a>Hello World</a>")
- assert translate("html").html_safe?
+ assert translate(:'translations.html').html_safe?
end
def test_translate_marks_translations_with_a_html_suffix_as_safe_html
- I18n.expects(:translate).with("hello_html", :raise => true).returns("<a>Hello World</a>")
- assert translate("hello_html").html_safe?
+ assert translate(:'translations.hello_html').html_safe?
end
def test_translation_returning_an_array_ignores_html_suffix
- I18n.expects(:translate).with(:foo_html, :raise => true).returns(["foo", "bar"])
- assert_equal ["foo", "bar"], translate(:foo_html)
+ assert_equal ["foo", "bar"], translate(:'translations.array_html')
end
end