aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG4
-rw-r--r--actionpack/actionpack.gemspec2
-rw-r--r--actionpack/lib/action_dispatch/http/mime_type.rb33
-rw-r--r--actionpack/lib/action_dispatch/http/url.rb79
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb39
-rw-r--r--actionpack/lib/action_dispatch/routing/redirection.rb110
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb62
-rw-r--r--actionpack/lib/action_dispatch/testing/integration.rb4
-rw-r--r--actionpack/lib/action_view/helpers/translation_helper.rb32
-rw-r--r--actionpack/lib/action_view/renderer/template_renderer.rb2
-rw-r--r--actionpack/lib/action_view/template/handlers.rb8
-rw-r--r--actionpack/lib/action_view/template/resolver.rb2
-rw-r--r--actionpack/test/controller/mime_responds_test.rb16
-rw-r--r--actionpack/test/controller/routing_test.rb581
-rw-r--r--actionpack/test/dispatch/mime_type_test.rb28
-rw-r--r--actionpack/test/dispatch/request_test.rb25
-rw-r--r--actionpack/test/dispatch/routing_test.rb65
-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.rb2
-rw-r--r--actionpack/test/template/translation_helper_test.rb72
24 files changed, 710 insertions, 461 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 74d017cc3d..acd9bd5b63 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,9 @@
*Rails 3.1.0 (unreleased)*
+* url_for and named url helpers now accept :subdomain and :domain as options [Josh Kalderimis]
+
+* The redirect route method now also accepts a hash of options which will only change the parts of the url in question, or an object which responds to call, allowing for redirects to be reused (check the documentation for examples). [Josh Kalderimis]
+
* Added config.action_controller.include_all_helpers. By default 'helper :all' is done in ActionController::Base, which includes all the helpers by default. Setting include_all_helpers to false will result in including only application_helper and helper corresponding to controller (like foo_helper for foo_controller). [Piotr Sarnacki]
* Added a convenience idiom to generate HTML5 data-* attributes in tag helpers from a :data hash of options:
diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec
index 44967631ff..b7d1d8c2af 100644
--- a/actionpack/actionpack.gemspec
+++ b/actionpack/actionpack.gemspec
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.add_dependency('activemodel', version)
s.add_dependency('rack-cache', '~> 0.5.3')
s.add_dependency('builder', '~> 3.0.0')
- s.add_dependency('i18n', '~> 0.4.2')
+ s.add_dependency('i18n', '~> 0.5.0')
s.add_dependency('rack', '~> 1.2.1')
s.add_dependency('rack-test', '~> 0.5.6')
s.add_dependency('rack-mount', '~> 0.6.13')
diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb
index c85aa64572..5b87a80c1b 100644
--- a/actionpack/lib/action_dispatch/http/mime_type.rb
+++ b/actionpack/lib/action_dispatch/http/mime_type.rb
@@ -115,15 +115,20 @@ module Mime
end
else
# keep track of creation order to keep the subsequent sort stable
- list = []
- accept_header.split(/,/).each_with_index do |header, index|
+ list, index = [], 0
+ accept_header.split(/,/).each do |header|
params, q = header.split(/;\s*q=/)
- if params
+ if params.present?
params.strip!
+
if params =~ TRAILING_STAR_REGEXP
- parse_data_with_trailing_star($1).each { |m| list << AcceptItem.new(index, m.to_s, q) }
+ parse_data_with_trailing_star($1).each do |m|
+ list << AcceptItem.new(index, m.to_s, q)
+ index += 1
+ end
else
- list << AcceptItem.new(index, params, q) unless params.empty?
+ list << AcceptItem.new(index, params, q)
+ index += 1
end
end
end
@@ -178,20 +183,22 @@ module Mime
# input: 'application'
# returned value: [Mime::HTML, Mime::JS, Mime::XML, Mime::YAML, Mime::ATOM, Mime::JSON, Mime::RSS, Mime::URL_ENCODED_FORM]
def parse_data_with_trailing_star(input)
- keys = Mime::LOOKUP.keys.select{|k| k.include?(input)}
- Mime::LOOKUP.values_at(*keys).uniq
+ Mime::SET.select { |m| m =~ input }
end
# This method is opposite of register method.
#
# Usage:
#
- # Mime::Type.unregister("text/x-mobile", :mobile)
- def unregister(string, symbol)
- EXTENSION_LOOKUP.delete(symbol.to_s)
- LOOKUP.delete(string)
- symbol = symbol.to_s.upcase.intern
- Mime.module_eval { remove_const(symbol) if const_defined?(symbol) }
+ # Mime::Type.unregister(:mobile)
+ def unregister(symbol)
+ symbol = symbol.to_s.upcase
+ mime = Mime.const_get(symbol)
+ Mime.instance_eval { remove_const(symbol) }
+
+ SET.delete_if { |v| v.eql?(mime) }
+ LOOKUP.delete_if { |k,v| v.eql?(mime) }
+ EXTENSION_LOOKUP.delete_if { |k,v| v.eql?(mime) }
end
end
diff --git a/actionpack/lib/action_dispatch/http/url.rb b/actionpack/lib/action_dispatch/http/url.rb
index 1e7054f381..796cd8c09b 100644
--- a/actionpack/lib/action_dispatch/http/url.rb
+++ b/actionpack/lib/action_dispatch/http/url.rb
@@ -4,26 +4,74 @@ module ActionDispatch
mattr_accessor :tld_length
self.tld_length = 1
- def self.extract_domain(host, tld_length = @@tld_length)
- return nil unless named_host?(host)
+ class << self
+ def extract_domain(host, tld_length = @@tld_length)
+ return nil unless named_host?(host)
+ host.split('.').last(1 + tld_length).join('.')
+ end
- host.split('.').last(1 + tld_length).join('.')
- end
+ def extract_subdomains(host, tld_length = @@tld_length)
+ return [] unless named_host?(host)
+ parts = host.split('.')
+ parts[0..-(tld_length+2)]
+ end
- def self.extract_subdomains(host, tld_length = @@tld_length)
- return [] unless named_host?(host)
- parts = host.split('.')
- parts[0..-(tld_length+2)]
- end
+ def extract_subdomain(host, tld_length = @@tld_length)
+ extract_subdomains(host, tld_length).join('.')
+ end
- def self.extract_subdomain(host, tld_length = @@tld_length)
- extract_subdomains(host, tld_length).join('.')
- end
+ def url_for(options = {})
+ unless options[:host].present? || options[:only_path].present?
+ raise ArgumentError, 'Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true'
+ end
- def self.named_host?(host)
- !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
- end
+ rewritten_url = ""
+
+ unless options[:only_path]
+ rewritten_url << (options[:protocol] || "http")
+ rewritten_url << "://" unless rewritten_url.match("://")
+ rewritten_url << rewrite_authentication(options)
+ rewritten_url << host_or_subdomain_and_domain(options)
+ rewritten_url << ":#{options.delete(:port)}" if options[:port]
+ end
+
+ path = options.delete(:path) || ''
+
+ params = options[:params] || {}
+ params.reject! {|k,v| !v }
+
+ rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
+ rewritten_url << "?#{params.to_query}" unless params.empty?
+ rewritten_url << "##{Rack::Mount::Utils.escape_uri(options[:anchor].to_param.to_s)}" if options[:anchor]
+ rewritten_url
+ end
+
+ private
+
+ def named_host?(host)
+ !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
+ end
+ def rewrite_authentication(options)
+ if options[:user] && options[:password]
+ "#{Rack::Utils.escape(options[:user])}:#{Rack::Utils.escape(options[:password])}@"
+ else
+ ""
+ end
+ end
+
+ def host_or_subdomain_and_domain(options)
+ return options[:host] unless options[:subdomain] || options[:domain]
+
+ tld_length = options[:tld_length] || @@tld_length
+
+ host = ""
+ host << (options[:subdomain] || extract_subdomain(options[:host], tld_length))
+ host << "."
+ host << (options[:domain] || extract_domain(options[:host], tld_length))
+ host
+ end
+ end
# Returns the complete URL used for this request.
def url
@@ -116,7 +164,6 @@ module ActionDispatch
def subdomain(tld_length = @@tld_length)
subdomains(tld_length)
end
-
end
end
end
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 01826fcede..5a38158e9f 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -2,6 +2,7 @@ require 'erb'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/object/blank'
require 'active_support/inflector'
+require 'action_dispatch/routing/redirection'
module ActionDispatch
module Routing
@@ -383,39 +384,6 @@ module ActionDispatch
map_method(:delete, *args, &block)
end
- # Redirect any path to another path:
- #
- # match "/stories" => redirect("/posts")
- def redirect(*args)
- options = args.last.is_a?(Hash) ? args.pop : {}
-
- path = args.shift || Proc.new
- path_proc = path.is_a?(Proc) ? path : proc { |params| (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) }
- status = options[:status] || 301
-
- lambda do |env|
- req = Request.new(env)
-
- params = [req.symbolized_path_parameters]
- params << req if path_proc.arity > 1
-
- uri = URI.parse(path_proc.call(*params))
- uri.scheme ||= req.scheme
- uri.host ||= req.host
- uri.port ||= req.port unless req.standard_port?
-
- body = %(<html><body>You are being <a href="#{ERB::Util.h(uri.to_s)}">redirected</a>.</body></html>)
-
- headers = {
- 'Location' => uri.to_s,
- 'Content-Type' => 'text/html',
- 'Content-Length' => body.length.to_s
- }
-
- [ status, headers, [body] ]
- end
- end
-
private
def map_method(method, *args, &block)
options = args.extract_options!
@@ -636,7 +604,7 @@ module ActionDispatch
:shallow_path => path, :shallow_prefix => path }.merge!(options)
scope(options) { yield }
end
-
+
# === Parameter Restriction
# Allows you to constrain the nested routes based on a set of rules.
# For instance, in order to change the routes to allow for a dot character in the +id+ parameter:
@@ -647,7 +615,7 @@ module ActionDispatch
#
# Now routes such as +/posts/1+ will no longer be valid, but +/posts/1.1+ will be.
# The +id+ parameter must match the constraint passed in for this example.
- #
+ #
# You may use this to also resrict other parameters:
#
# resources :posts do
@@ -1369,6 +1337,7 @@ module ActionDispatch
include Base
include HttpHelpers
+ include Redirection
include Scoping
include Resources
include Shorthand
diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb
new file mode 100644
index 0000000000..804991ad5f
--- /dev/null
+++ b/actionpack/lib/action_dispatch/routing/redirection.rb
@@ -0,0 +1,110 @@
+require 'action_dispatch/http/request'
+
+module ActionDispatch
+ module Routing
+ module Redirection
+
+ # Redirect any path to another path:
+ #
+ # match "/stories" => redirect("/posts")
+ #
+ # You can also use interpolation in the supplied redirect argument:
+ #
+ # match 'docs/:article', :to => redirect('/wiki/%{article}')
+ #
+ # Alternatively you can use one of the other syntaxes:
+ #
+ # The block version of redirect allows for the easy encapsulation of any logic associated with
+ # the redirect in question. Either the params and request are supplied as arguments, or just
+ # params, depending of how many arguments your block accepts. A string is required as a
+ # return value.
+ #
+ # match 'jokes/:number', :to => redirect do |params, request|
+ # path = (params[:number].to_i.even? ? "/wheres-the-beef" : "/i-love-lamp")
+ # "http://#{request.host_with_port}/#{path}"
+ # end
+ #
+ # The options version of redirect allows you to supply only the parts of the url which need
+ # to change, it also supports interpolation of the path similar to the first example.
+ #
+ # match 'stores/:name', :to => redirect(:subdomain => 'stores', :path => '/%{name}')
+ # match 'stores/:name(*all)', :to => redirect(:subdomain => 'stores', :path => '/%{name}%{all}')
+ #
+ # Finally, an object which responds to call can be supplied to redirect, allowing you to reuse
+ # common redirect routes. The call method must accept two arguments, params and request, and return
+ # a string.
+ #
+ # match 'accounts/:name' => redirect(SubdomainRedirector.new('api'))
+ #
+ def redirect(*args, &block)
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ status = options.delete(:status) || 301
+
+ path = args.shift
+
+ path_proc = if path.is_a?(String)
+ proc { |params| (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) }
+ elsif options.any?
+ options_proc(options)
+ elsif path.respond_to?(:call)
+ proc { |params, request| path.call(params, request) }
+ elsif block
+ block
+ else
+ raise ArgumentError, "redirection argument not supported"
+ end
+
+ redirection_proc(status, path_proc)
+ end
+
+ private
+
+ def options_proc(options)
+ proc do |params, request|
+ path = if options[:path].nil?
+ request.path
+ elsif params.empty? || !options[:path].match(/%\{\w*\}/)
+ options.delete(:path)
+ else
+ (options.delete(:path) % params)
+ end
+
+ default_options = {
+ :protocol => request.protocol,
+ :host => request.host,
+ :port => request.optional_port,
+ :path => path,
+ :params => request.query_parameters
+ }
+
+ ActionDispatch::Http::URL.url_for(options.reverse_merge(default_options))
+ end
+ end
+
+ def redirection_proc(status, path_proc)
+ lambda do |env|
+ req = Request.new(env)
+
+ params = [req.symbolized_path_parameters]
+ params << req if path_proc.arity > 1
+
+ uri = URI.parse(path_proc.call(*params))
+ uri.scheme ||= req.scheme
+ uri.host ||= req.host
+ uri.port ||= req.port unless req.standard_port?
+
+ body = %(<html><body>You are being <a href="#{ERB::Util.h(uri.to_s)}">redirected</a>.</body></html>)
+
+ headers = {
+ 'Location' => uri.to_s,
+ 'Content-Type' => 'text/html',
+ 'Content-Length' => body.length.to_s
+ }
+
+ [ status, headers, [body] ]
+ end
+ end
+
+ end
+ end
+end \ No newline at end of file
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index ebced9cabe..03bfe178e5 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -442,12 +442,9 @@ module ActionDispatch
raise_routing_error unless path
- params.reject! {|k,v| !v }
-
return [path, params.keys] if @extras
- path << "?#{params.to_query}" unless params.empty?
- path
+ [path, params]
rescue Rack::Mount::RoutingError
raise_routing_error
end
@@ -486,7 +483,7 @@ module ActionDispatch
end
RESERVED_OPTIONS = [:host, :protocol, :port, :subdomain, :domain, :tld_length,
- :trailing_slash, :script_name, :anchor, :params, :only_path ]
+ :trailing_slash, :anchor, :params, :only_path, :script_name]
def _generate_prefix(options = {})
nil
@@ -498,29 +495,24 @@ module ActionDispatch
handle_positional_args(options)
- rewritten_url = ""
-
- path_segments = options.delete(:_path_segments)
- unless options[:only_path]
- rewritten_url << (options[:protocol] || "http")
- rewritten_url << "://" unless rewritten_url.match("://")
- rewritten_url << rewrite_authentication(options)
- rewritten_url << host_from_options(options)
- rewritten_url << ":#{options.delete(:port)}" if options[:port]
- end
+ user, password = extract_authentication(options)
+ path_segments = options.delete(:_path_segments)
+ script_name = options.delete(:script_name)
- script_name = options.delete(:script_name)
path = (script_name.blank? ? _generate_prefix(options) : script_name.chomp('/')).to_s
path_options = options.except(*RESERVED_OPTIONS)
path_options = yield(path_options) if block_given?
- path << generate(path_options, path_segments || {})
- # ROUTES TODO: This can be called directly, so script_name should probably be set in the routes
- rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
- rewritten_url << "##{Rack::Mount::Utils.escape_uri(options[:anchor].to_param.to_s)}" if options[:anchor]
+ path_addition, params = generate(path_options, path_segments || {})
+ path << path_addition
- rewritten_url
+ ActionDispatch::Http::URL.url_for(options.merge({
+ :path => path,
+ :params => params,
+ :user => user,
+ :password => password
+ }))
end
def call(env)
@@ -561,23 +553,12 @@ module ActionDispatch
private
- def host_from_options(options)
- computed_host = subdomain_and_domain(options) || options[:host]
- unless computed_host
- raise ArgumentError, "Missing host to link to! Please provide :host parameter or set default_url_options[:host]"
+ def extract_authentication(options)
+ if options[:user] && options[:password]
+ [options.delete(:user), options.delete(:password)]
+ else
+ nil
end
- computed_host
- end
-
- def subdomain_and_domain(options)
- return nil unless options[:subdomain] || options[:domain]
- tld_length = options[:tld_length] || ActionDispatch::Http::URL.tld_length
-
- host = ""
- host << (options[:subdomain] || ActionDispatch::Http::URL.extract_subdomain(options[:host], tld_length))
- host << "."
- host << (options[:domain] || ActionDispatch::Http::URL.extract_domain(options[:host], tld_length))
- host
end
def handle_positional_args(options)
@@ -590,13 +571,6 @@ module ActionDispatch
options.merge!(Hash[args.zip(keys).map { |v, k| [k, v] }])
end
- def rewrite_authentication(options)
- if options[:user] && options[:password]
- "#{Rack::Utils.escape(options.delete(:user))}:#{Rack::Utils.escape(options.delete(:password))}@"
- else
- ""
- end
- end
end
end
end
diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb
index 8fe74c3c80..5c6416a19e 100644
--- a/actionpack/lib/action_dispatch/testing/integration.rb
+++ b/actionpack/lib/action_dispatch/testing/integration.rb
@@ -235,9 +235,7 @@ module ActionDispatch
# Set the host name to use in the next request.
#
# session.host! "www.example.com"
- def host!(name)
- @host = name
- end
+ alias :host! :host=
private
def _mock_session
diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb
index 8574ca6595..e7ec1df2c8 100644
--- a/actionpack/lib/action_view/helpers/translation_helper.rb
+++ b/actionpack/lib/action_view/helpers/translation_helper.rb
@@ -1,13 +1,33 @@
require 'action_view/helpers/tag_helper'
+require 'i18n/exceptions'
+
+module I18n
+ class ExceptionHandler
+ include Module.new {
+ def call(exception, locale, key, options)
+ exception.is_a?(MissingTranslationData) ? super.html_safe : super
+ end
+ }
+ end
+end
module ActionView
# = Action View Translation Helpers
module Helpers
module TranslationHelper
# Delegates to I18n#translate but also performs three additional functions.
- # First, it'll catch MissingTranslationData exceptions and turn them into
- # inline spans that contains the missing key, such that you can see in a
- # view what is missing where.
+ #
+ # First, it'll pass the :rescue_format => :html option to I18n so that any caught
+ # MissingTranslationData exceptions will be turned into inline spans that
+ #
+ # * have a "translation-missing" class set,
+ # * contain the missing key as a title attribute and
+ # * a titleized version of the last key segment as a text.
+ #
+ # E.g. the value returned for a missing translation key :"blog.post.title" will be
+ # <span class="translation_missing" title="translation missing: blog.post.title">Title</span>.
+ # This way your views will display rather reasonableful strings but it will still
+ # be easy to spot missing translations.
#
# Second, it'll scope the key by the current partial if the key starts
# with a period. So if you call <tt>translate(".foo")</tt> from the
@@ -24,15 +44,13 @@ module ActionView
# naming convention helps to identify translations that include HTML tags so that
# you know what kind of output to expect when you call translate in a template.
def translate(key, options = {})
- translation = I18n.translate(scope_key_by_partial(key), options.merge!(:raise => true))
+ options.merge!(:rescue_format => :html) unless options.key?(:rescue_format)
+ translation = I18n.translate(scope_key_by_partial(key), options)
if html_safe_translation_key?(key) && translation.respond_to?(:html_safe)
translation.html_safe
else
translation
end
- rescue I18n::MissingTranslationData => e
- keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope])
- content_tag('span', keys.join(', '), :class => 'translation_missing')
end
alias :t :translate
diff --git a/actionpack/lib/action_view/renderer/template_renderer.rb b/actionpack/lib/action_view/renderer/template_renderer.rb
index 6912acee31..ece3f621b6 100644
--- a/actionpack/lib/action_view/renderer/template_renderer.rb
+++ b/actionpack/lib/action_view/renderer/template_renderer.rb
@@ -45,7 +45,7 @@ module ActionView
elsif options.key?(:file)
with_fallbacks { find_template(options[:file], options[:prefix], false, keys) }
elsif options.key?(:inline)
- handler = Template.handler_class_for_extension(options[:type] || "erb")
+ handler = Template.handler_for_extension(options[:type] || "erb")
Template.new(options[:inline], "inline template", handler, :locals => keys)
elsif options.key?(:template)
options[:template].respond_to?(:render) ?
diff --git a/actionpack/lib/action_view/template/handlers.rb b/actionpack/lib/action_view/template/handlers.rb
index 60347e2a95..4438199497 100644
--- a/actionpack/lib/action_view/template/handlers.rb
+++ b/actionpack/lib/action_view/template/handlers.rb
@@ -44,7 +44,13 @@ module ActionView #:nodoc:
end
def handler_class_for_extension(extension)
- (extension && registered_template_handler(extension.to_sym)) || @@default_template_handlers
+ ActiveSupport::Deprecation.warn "handler_class_for_extension is deprecated. " <<
+ "Please use handler_for_extension instead", caller
+ handler_for_extension(extension)
+ end
+
+ def handler_for_extension(extension)
+ registered_template_handler(extension) || @@default_template_handlers
end
end
end
diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb
index 7707dbcf98..9f15661669 100644
--- a/actionpack/lib/action_view/template/resolver.rb
+++ b/actionpack/lib/action_view/template/resolver.rb
@@ -129,7 +129,7 @@ module ActionView
def extract_handler_and_format(path, default_formats)
pieces = File.basename(path).split(".")
pieces.shift
- handler = Template.handler_class_for_extension(pieces.pop)
+ handler = Template.handler_for_extension(pieces.pop)
format = pieces.last && Mime[pieces.last]
[handler, format]
end
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb
index 82969b2979..98c9d43b93 100644
--- a/actionpack/test/controller/mime_responds_test.rb
+++ b/actionpack/test/controller/mime_responds_test.rb
@@ -201,8 +201,8 @@ class RespondToControllerTest < ActionController::TestCase
def teardown
super
- Mime::Type.unregister('text/x-mobile', :iphone)
- Mime::Type.unregister('text/iphone', :mobile)
+ Mime::Type.unregister(:iphone)
+ Mime::Type.unregister(:mobile)
end
def test_html
@@ -622,12 +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('text/x-mobile', :iphone)
- Mime::Type.unregister('text/iphone', :mobile)
+ Mime::Type.unregister(:iphone)
+ Mime::Type.unregister(:mobile)
end
def test_using_resource
@@ -929,7 +931,8 @@ class RespondWithControllerTest < ActionController::TestCase
@controller = RenderJsonRespondWithController.new
@request.accept = "application/json"
get :index, :format => :json
- assert_equal %Q{{"message":"boom","error":"RenderJsonTestException"}}, @response.body
+ assert_match(/"message":"boom"/, @response.body)
+ assert_match(/"error":"RenderJsonTestException"/, @response.body)
end
def test_no_double_render_is_raised
@@ -1020,8 +1023,7 @@ class MimeControllerLayoutsTest < ActionController::TestCase
def teardown
super
- Mime::Type.unregister('text/x-mobile', :iphone)
- Mime::Type.unregister('text/iphone', :mobile)
+ Mime::Type.unregister(:iphone)
end
def test_missing_layout_renders_properly
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/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb
index 9424d88498..9782f328fc 100644
--- a/actionpack/test/dispatch/mime_type_test.rb
+++ b/actionpack/test/dispatch/mime_type_test.rb
@@ -19,7 +19,7 @@ class MimeTypeTest < ActiveSupport::TestCase
assert_equal Mime::MOBILE, Mime::LOOKUP['text/x-mobile']
assert_equal Mime::MOBILE, Mime::EXTENSION_LOOKUP['mobile']
- Mime::Type.unregister("text/x-mobile", :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]"
@@ -31,38 +31,30 @@ class MimeTypeTest < ActiveSupport::TestCase
test "parse text with trailing star at the beginning" do
accept = "text/*, text/html, application/json, multipart/form-data"
- expect = [Mime::JSON, Mime::XML, Mime::ICS, Mime::HTML, Mime::CSS, Mime::CSV, Mime::TEXT, Mime::YAML, Mime::JS, Mime::MULTIPART_FORM]
+ 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.size, parsed.size
- Range.new(0,expect.size-1).to_a.each do |index|
- assert_equal expect[index], parsed[index], "Failed for index number #{index}"
- end
+ 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::XML, Mime::ICS, Mime::CSS, Mime::CSV, Mime::JS, Mime::YAML, Mime::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 10, parsed.size
- Range.new(0,expect.size-1).to_a.each do |index|
- assert_equal expect[index], parsed[index], "Failed for index number #{index}"
- end
+ assert_equal expect, parsed
end
test "parse text with trailing star" do
accept = "text/*"
- expect = [Mime::JSON, Mime::XML, Mime::ICS, Mime::HTML, Mime::CSS, Mime::CSV, Mime::JS, Mime::YAML, Mime::TEXT].sort_by(&:to_s)
+ 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 9, parsed.size
- assert_equal expect, parsed.sort_by(&:to_s)
+ assert_equal expect, parsed
end
test "parse application with trailing star" do
accept = "application/*"
- expect = [Mime::HTML, Mime::JS, Mime::XML, Mime::YAML, Mime::ATOM, Mime::JSON, Mime::RSS, Mime::PDF, Mime::URL_ENCODED_FORM].sort_by(&:to_s)
+ 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 9, parsed.size
- assert_equal expect, parsed.sort_by(&:to_s)
+ assert_equal expect, parsed
end
test "parse without q" do
@@ -99,7 +91,7 @@ class MimeTypeTest < ActiveSupport::TestCase
assert_equal Mime::GIF, Mime::SET.last
end
ensure
- Mime::Type.unregister('image/gif', :gif)
+ Mime::Type.unregister(:gif)
end
end
diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb
index 8f672c1149..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
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index bbd010ea6d..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
@@ -667,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'
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 bd18cdc1b8..67baf369e2 100644
--- a/actionpack/test/lib/controller/fake_models.rb
+++ b/actionpack/test/lib/controller/fake_models.rb
@@ -197,6 +197,6 @@ end
class RenderJsonTestException < Exception
def to_json(options = nil)
- return { :error => self.class.name, :message => self.to_str }.to_json
+ return { :error => self.class.name, :message => self.to_s }.to_json
end
end
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