2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2010-01-07 08:20:31 -05:00
|
|
|
|
|
|
|
module AbstractController
|
|
|
|
module Testing
|
2012-07-29 16:47:05 -04:00
|
|
|
class UrlForTest < ActionController::TestCase
|
2010-01-07 08:20:31 -05:00
|
|
|
class W
|
2016-03-01 03:48:53 -05:00
|
|
|
include ActionDispatch::Routing::RouteSet.new.tap { |r|
|
|
|
|
r.draw {
|
|
|
|
ActiveSupport::Deprecation.silence {
|
2016-08-06 12:54:50 -04:00
|
|
|
get ":controller(/:action(/:id(.:format)))"
|
2016-03-01 03:48:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.url_helpers
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
W.default_url_options.clear
|
|
|
|
end
|
|
|
|
|
2014-05-19 19:14:47 -04:00
|
|
|
def test_nested_optional
|
|
|
|
klass = Class.new {
|
|
|
|
include ActionDispatch::Routing::RouteSet.new.tap { |r|
|
|
|
|
r.draw {
|
2016-08-06 13:35:13 -04:00
|
|
|
get "/foo/(:bar/(:baz))/:zot", as: "fun",
|
|
|
|
controller: :articles,
|
|
|
|
action: :index
|
2014-05-19 19:14:47 -04:00
|
|
|
}
|
|
|
|
}.url_helpers
|
2017-01-05 03:20:57 -05:00
|
|
|
default_url_options[:host] = "example.com"
|
2014-05-19 19:14:47 -04:00
|
|
|
}
|
|
|
|
|
2016-08-06 13:44:11 -04:00
|
|
|
path = klass.new.fun_path(controller: :articles,
|
2016-08-06 13:35:13 -04:00
|
|
|
baz: "baz",
|
2016-08-06 13:44:11 -04:00
|
|
|
zot: "zot")
|
2014-05-19 19:14:47 -04:00
|
|
|
# :bar key isn't provided
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/foo/zot", path
|
2014-05-19 19:14:47 -04:00
|
|
|
end
|
|
|
|
|
2015-03-05 16:11:53 -05:00
|
|
|
def add_host!(app = W)
|
2016-08-06 12:54:50 -04:00
|
|
|
app.default_url_options[:host] = "www.basecamphq.com"
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
2012-03-25 21:16:29 -04:00
|
|
|
def add_port!
|
|
|
|
W.default_url_options[:port] = 3000
|
|
|
|
end
|
|
|
|
|
2011-11-08 08:52:35 -05:00
|
|
|
def add_numeric_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
W.default_url_options[:host] = "127.0.0.1"
|
2011-11-08 08:52:35 -05:00
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
def test_exception_is_thrown_without_host
|
2010-11-22 18:31:03 -05:00
|
|
|
assert_raise ArgumentError do
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for controller: "c", action: "a", id: "i"
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_anchor
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("/c/a#anchor",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(only_path: true, controller: "c", action: "a", anchor: "anchor")
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-11-22 08:42:19 -05:00
|
|
|
def test_nil_anchor
|
|
|
|
assert_equal(
|
2016-08-06 12:54:50 -04:00
|
|
|
"/c/a",
|
|
|
|
W.new.url_for(only_path: true, controller: "c", action: "a", anchor: nil)
|
2014-11-22 08:42:19 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_false_anchor
|
|
|
|
assert_equal(
|
2016-08-06 12:54:50 -04:00
|
|
|
"/c/a",
|
|
|
|
W.new.url_for(only_path: true, controller: "c", action: "a", anchor: false)
|
2014-11-22 08:42:19 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
def test_anchor_should_call_to_param
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("/c/a#anchor",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(only_path: true, controller: "c", action: "a", anchor: Struct.new(:to_param).new("anchor"))
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-06-25 20:54:13 -04:00
|
|
|
def test_anchor_should_escape_unsafe_pchar
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("/c/a#%23anchor",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(only_path: true, controller: "c", action: "a", anchor: Struct.new(:to_param).new("#anchor"))
|
2010-06-25 20:54:13 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_anchor_should_not_escape_safe_pchar
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("/c/a#name=user&email=user@domain.com",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(only_path: true, controller: "c", action: "a", anchor: Struct.new(:to_param).new("name=user&email=user@domain.com"))
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_host
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://www.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i")
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_host_may_be_overridden
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://37signals.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(host: "37signals.basecamphq.com", controller: "c", action: "a", id: "i")
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-11-22 18:31:03 -05:00
|
|
|
def test_subdomain_may_be_changed
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://api.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(subdomain: "api", controller: "c", action: "a", id: "i")
|
2010-11-22 18:31:03 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-12-14 09:43:42 -05:00
|
|
|
def test_subdomain_may_be_object
|
2016-08-06 12:54:50 -04:00
|
|
|
model = Class.new { def self.to_param; "api"; end }
|
2011-12-14 09:43:42 -05:00
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://api.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(subdomain: model, controller: "c", action: "a", id: "i")
|
2011-12-14 09:43:42 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-10-04 04:16:34 -04:00
|
|
|
def test_subdomain_may_be_removed
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(subdomain: false, controller: "c", action: "a", id: "i")
|
2011-10-04 04:16:34 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-04-11 22:12:19 -04:00
|
|
|
def test_subdomain_may_be_removed_with_blank_string
|
2016-08-06 12:54:50 -04:00
|
|
|
W.default_url_options[:host] = "api.basecamphq.com"
|
|
|
|
assert_equal("http://basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(subdomain: "", controller: "c", action: "a", id: "i")
|
2013-04-11 22:12:19 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-10-04 04:16:34 -04:00
|
|
|
def test_multiple_subdomains_may_be_removed
|
2016-08-06 12:54:50 -04:00
|
|
|
W.default_url_options[:host] = "mobile.www.api.basecamphq.com"
|
|
|
|
assert_equal("http://basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(subdomain: false, controller: "c", action: "a", id: "i")
|
2011-10-04 04:16:34 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-11-08 08:52:35 -05:00
|
|
|
def test_subdomain_may_be_accepted_with_numeric_host
|
|
|
|
add_numeric_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://127.0.0.1/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(subdomain: "api", controller: "c", action: "a", id: "i")
|
2011-11-08 08:52:35 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-11-22 18:31:03 -05:00
|
|
|
def test_domain_may_be_changed
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://www.37signals.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(domain: "37signals.com", controller: "c", action: "a", id: "i")
|
2010-11-22 18:31:03 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tld_length_may_be_changed
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://mobile.www.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(subdomain: "mobile", tld_length: 2, controller: "c", action: "a", id: "i")
|
2010-11-22 18:31:03 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
def test_port
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://www.basecamphq.com:3000/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", port: 3000)
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-03-25 21:16:29 -04:00
|
|
|
def test_default_port
|
|
|
|
add_host!
|
|
|
|
add_port!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("http://www.basecamphq.com:3000/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i")
|
2012-03-25 21:16:29 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
def test_protocol
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("https://www.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https")
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-10-09 10:13:45 -04:00
|
|
|
def test_protocol_with_and_without_separators
|
2010-01-07 08:20:31 -05:00
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("https://www.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https")
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("https://www.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https:")
|
2010-10-09 10:13:45 -04:00
|
|
|
)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("https://www.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https://")
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-10-09 10:13:45 -04:00
|
|
|
def test_without_protocol
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("//www.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: "//")
|
2010-10-09 10:13:45 -04:00
|
|
|
)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("//www.basecamphq.com/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: false)
|
2014-05-09 14:52:10 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_without_protocol_and_with_port
|
|
|
|
add_host!
|
|
|
|
add_port!
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("//www.basecamphq.com:3000/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: "//")
|
2014-05-09 14:52:10 -04:00
|
|
|
)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("//www.basecamphq.com:3000/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: false)
|
2010-10-09 10:13:45 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
def test_trailing_slash
|
|
|
|
add_host!
|
2016-08-16 03:30:11 -04:00
|
|
|
options = { controller: "foo", trailing_slash: true, action: "bar", id: "33" }
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal("http://www.basecamphq.com/foo/bar/33/", W.new.url_for(options))
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_trailing_slash_with_protocol
|
|
|
|
add_host!
|
2016-10-28 23:05:58 -04:00
|
|
|
options = { trailing_slash: true, protocol: "https", controller: "foo", action: "bar", id: "33" }
|
|
|
|
assert_equal("https://www.basecamphq.com/foo/bar/33/", W.new.url_for(options))
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_equal "https://www.basecamphq.com/foo/bar/33/?query=string", W.new.url_for(options.merge(query: "string"))
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_trailing_slash_with_only_path
|
2016-08-16 03:30:11 -04:00
|
|
|
options = { controller: "foo", trailing_slash: true }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_equal "/foo/", W.new.url_for(options.merge(only_path: true))
|
|
|
|
options.update(action: "bar", id: "33")
|
|
|
|
assert_equal "/foo/bar/33/", W.new.url_for(options.merge(only_path: true))
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal "/foo/bar/33/?query=string", W.new.url_for(options.merge(query: "string", only_path: true))
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_trailing_slash_with_anchor
|
2016-08-16 03:30:11 -04:00
|
|
|
options = { trailing_slash: true, controller: "foo", action: "bar", id: "33", only_path: true, anchor: "chapter7" }
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/foo/bar/33/#chapter7", W.new.url_for(options)
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_equal "/foo/bar/33/?query=string#chapter7", W.new.url_for(options.merge(query: "string"))
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_trailing_slash_with_params
|
2016-08-06 13:35:13 -04:00
|
|
|
url = W.new.url_for(trailing_slash: true, only_path: true, controller: "cont", action: "act", p1: "cafe", p2: "link")
|
2010-01-07 08:20:31 -05:00
|
|
|
params = extract_params(url)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ p1: "cafe" }.to_query, params[0])
|
|
|
|
assert_equal({ p2: "link" }.to_query, params[1])
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_relative_url_root_is_respected
|
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("https://www.basecamphq.com/subdir/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https", script_name: "/subdir")
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-02-24 16:04:27 -05:00
|
|
|
def test_relative_url_root_is_respected_with_environment_variable
|
|
|
|
# `config.relative_url_root` is set by ENV['RAILS_RELATIVE_URL_ROOT']
|
2015-03-05 16:11:53 -05:00
|
|
|
w = Class.new {
|
2016-08-06 12:54:50 -04:00
|
|
|
config = ActionDispatch::Routing::RouteSet::Config.new "/subdir"
|
2015-03-05 16:11:53 -05:00
|
|
|
r = ActionDispatch::Routing::RouteSet.new(config)
|
2016-08-06 12:54:50 -04:00
|
|
|
r.draw { ActiveSupport::Deprecation.silence { get ":controller(/:action(/:id(.:format)))" } }
|
2015-03-05 16:11:53 -05:00
|
|
|
include r.url_helpers
|
|
|
|
}
|
|
|
|
add_host!(w)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("https://www.basecamphq.com/subdir/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
w.new.url_for(controller: "c", action: "a", id: "i", protocol: "https")
|
2015-02-24 16:04:27 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
def test_named_routes
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2016-08-06 13:35:13 -04:00
|
|
|
get "this/is/verbose", to: "home#index", as: :no_args
|
|
|
|
get "home/sweet/home/:user", to: "home#index", as: :home
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# We need to create a new class in order to install the new named route.
|
2010-02-26 18:56:38 -05:00
|
|
|
kls = Class.new { include set.url_helpers }
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
controller = kls.new
|
|
|
|
assert controller.respond_to?(:home_url)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "http://www.basecamphq.com/home/sweet/home/again",
|
2016-08-06 13:35:13 -04:00
|
|
|
controller.send(:home_url, host: "www.basecamphq.com", user: "again")
|
2010-01-07 08:20:31 -05:00
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal("/home/sweet/home/alabama", controller.send(:home_path, user: "alabama", host: "unused"))
|
|
|
|
assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller.send(:home_url, user: "alabama", host: "www.basecamphq.com"))
|
|
|
|
assert_equal("http://www.basecamphq.com/this/is/verbose", controller.send(:no_args_url, host: "www.basecamphq.com"))
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_relative_url_root_is_respected_for_named_routes
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2016-08-06 13:35:13 -04:00
|
|
|
get "/home/sweet/home/:user", to: "home#index", as: :home
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
2010-02-26 18:56:38 -05:00
|
|
|
kls = Class.new { include set.url_helpers }
|
2010-01-07 08:20:31 -05:00
|
|
|
controller = kls.new
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "http://www.basecamphq.com/subdir/home/sweet/home/again",
|
2016-08-06 13:35:13 -04:00
|
|
|
controller.send(:home_url, host: "www.basecamphq.com", user: "again", script_name: "/subdir")
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-25 07:30:20 -05:00
|
|
|
def test_using_nil_script_name_properly_concats_with_original_script_name
|
2014-11-25 07:29:48 -05:00
|
|
|
add_host!
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("https://www.basecamphq.com/subdir/c/a/i",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https", script_name: nil, original_script_name: "/subdir")
|
2014-11-25 07:29:48 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
def test_only_path
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2016-08-06 13:35:13 -04:00
|
|
|
get "home/sweet/home/:user", to: "home#index", as: :home
|
2016-03-01 03:48:53 -05:00
|
|
|
|
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 12:54:50 -04:00
|
|
|
get ":controller/:action/:id"
|
2016-03-01 03:48:53 -05:00
|
|
|
end
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# We need to create a new class in order to install the new named route.
|
2010-02-26 18:56:38 -05:00
|
|
|
kls = Class.new { include set.url_helpers }
|
2010-01-07 08:20:31 -05:00
|
|
|
controller = kls.new
|
2014-07-17 13:04:52 -04:00
|
|
|
assert_respond_to controller, :home_url
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/brave/new/world",
|
2016-08-06 13:35:13 -04:00
|
|
|
controller.url_for(controller: "brave", action: "new", id: "world", only_path: true)
|
2010-01-07 08:20:31 -05:00
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal("/home/sweet/home/alabama", controller.home_path(user: "alabama", host: "unused"))
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("/home/sweet/home/alabama", controller.home_path("alabama"))
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_one_parameter
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("/c/a?param=val",
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(only_path: true, controller: "c", action: "a", param: "val")
|
2010-01-07 08:20:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_two_parameters
|
2016-08-06 13:35:13 -04:00
|
|
|
url = W.new.url_for(only_path: true, controller: "c", action: "a", p1: "X1", p2: "Y2")
|
2010-01-07 08:20:31 -05:00
|
|
|
params = extract_params(url)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ p1: "X1" }.to_query, params[0])
|
|
|
|
assert_equal({ p2: "Y2" }.to_query, params[1])
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash_parameter
|
2016-08-16 03:30:11 -04:00
|
|
|
url = W.new.url_for(only_path: true, controller: "c", action: "a", query: { name: "Bob", category: "prof" })
|
2010-01-07 08:20:31 -05:00
|
|
|
params = extract_params(url)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "query[category]" => "prof" }.to_query, params[0])
|
|
|
|
assert_equal({ "query[name]" => "Bob" }.to_query, params[1])
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_array_parameter
|
2016-08-06 13:35:13 -04:00
|
|
|
url = W.new.url_for(only_path: true, controller: "c", action: "a", query: ["Bob", "prof"])
|
2010-01-07 08:20:31 -05:00
|
|
|
params = extract_params(url)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "query[]" => "Bob" }.to_query, params[0])
|
|
|
|
assert_equal({ "query[]" => "prof" }.to_query, params[1])
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash_recursive_parameters
|
2016-08-16 03:30:11 -04:00
|
|
|
url = W.new.url_for(only_path: true, controller: "c", action: "a", query: { person: { name: "Bob", position: "prof" }, hobby: "piercing" })
|
2010-01-07 08:20:31 -05:00
|
|
|
params = extract_params(url)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "query[hobby]" => "piercing" }.to_query, params[0])
|
|
|
|
assert_equal({ "query[person][name]" => "Bob" }.to_query, params[1])
|
|
|
|
assert_equal({ "query[person][position]" => "prof" }.to_query, params[2])
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash_recursive_and_array_parameters
|
2016-08-16 03:30:11 -04:00
|
|
|
url = W.new.url_for(only_path: true, controller: "c", action: "a", id: 101, query: { person: { name: "Bob", position: ["prof", "art director"] }, hobby: "piercing" })
|
2010-09-22 15:03:39 -04:00
|
|
|
assert_match(%r(^/c/a/101), url)
|
2010-01-07 08:20:31 -05:00
|
|
|
params = extract_params(url)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "query[hobby]" => "piercing" }.to_query, params[0])
|
|
|
|
assert_equal({ "query[person][name]" => "Bob" }.to_query, params[1])
|
|
|
|
assert_equal({ "query[person][position][]" => "art director" }.to_query, params[2])
|
|
|
|
assert_equal({ "query[person][position][]" => "prof" }.to_query, params[3])
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
2015-07-07 13:47:16 -04:00
|
|
|
def test_url_action_controller_parameters
|
|
|
|
add_host!
|
|
|
|
assert_raise(ArgumentError) do
|
2016-08-06 13:35:13 -04:00
|
|
|
W.new.url_for(ActionController::Parameters.new(controller: "c", action: "a", protocol: "javascript", f: "%0Aeval(name)"))
|
2015-07-07 13:47:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
def test_path_generation_for_symbol_parameter_keys
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_generates("/image", controller: :image)
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_named_routes_with_nil_keys
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2016-08-06 13:35:13 -04:00
|
|
|
get "posts.:format", to: "posts#index", as: :posts
|
|
|
|
get "/", to: "posts#index", as: :main
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# We need to create a new class in order to install the new named route.
|
2010-02-26 18:56:38 -05:00
|
|
|
kls = Class.new { include set.url_helpers }
|
2016-08-06 12:54:50 -04:00
|
|
|
kls.default_url_options[:host] = "www.basecamphq.com"
|
2010-01-07 08:20:31 -05:00
|
|
|
|
|
|
|
controller = kls.new
|
2016-08-16 03:30:11 -04:00
|
|
|
params = { action: :index, controller: :posts, format: :xml }
|
2010-01-07 08:20:31 -05:00
|
|
|
assert_equal("http://www.basecamphq.com/posts.xml", controller.send(:url_for, params))
|
|
|
|
params[:format] = nil
|
|
|
|
assert_equal("http://www.basecamphq.com/", controller.send(:url_for, params))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_multiple_includes_maintain_distinct_options
|
2010-01-29 10:16:01 -05:00
|
|
|
first_class = Class.new { include ActionController::UrlFor }
|
|
|
|
second_class = Class.new { include ActionController::UrlFor }
|
2010-01-07 08:20:31 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
first_host, second_host = "firsthost.com", "secondhost.com"
|
2010-01-07 08:20:31 -05:00
|
|
|
|
|
|
|
first_class.default_url_options[:host] = first_host
|
|
|
|
second_class.default_url_options[:host] = second_host
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal first_host, first_class.default_url_options[:host]
|
2011-06-11 02:52:13 -04:00
|
|
|
assert_equal second_host, second_class.default_url_options[:host]
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
|
2010-04-21 21:37:51 -04:00
|
|
|
def test_with_stringified_keys
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("/c", W.new.url_for("controller" => "c", "only_path" => true))
|
|
|
|
assert_equal("/c/a", W.new.url_for("controller" => "c", "action" => "a", "only_path" => true))
|
2010-04-21 21:37:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_hash_with_indifferent_access
|
2016-08-06 12:54:50 -04:00
|
|
|
W.default_url_options[:controller] = "d"
|
2010-04-22 11:33:54 -04:00
|
|
|
W.default_url_options[:only_path] = false
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal("/c", W.new.url_for(ActiveSupport::HashWithIndifferentAccess.new("controller" => "c", "only_path" => true)))
|
2010-04-22 11:33:54 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
W.default_url_options[:action] = "b"
|
|
|
|
assert_equal("/c/a", W.new.url_for(ActiveSupport::HashWithIndifferentAccess.new("controller" => "c", "action" => "a", "only_path" => true)))
|
2010-04-21 21:37:51 -04:00
|
|
|
end
|
|
|
|
|
2011-03-09 09:44:25 -05:00
|
|
|
def test_url_params_with_nil_to_param_are_not_in_url
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal("/c/a", W.new.url_for(only_path: true, controller: "c", action: "a", id: Struct.new(:to_param).new(nil)))
|
2011-03-09 09:44:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_false_url_params_are_included_in_query
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal("/c/a?show=false", W.new.url_for(only_path: true, controller: "c", action: "a", show: false))
|
2011-03-09 09:44:25 -05:00
|
|
|
end
|
|
|
|
|
2013-11-12 15:18:36 -05:00
|
|
|
def test_url_generation_with_array_and_hash
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
|
|
|
namespace :admin do
|
|
|
|
resources :posts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
kls = Class.new { include set.url_helpers }
|
2016-08-06 12:54:50 -04:00
|
|
|
kls.default_url_options[:host] = "www.basecamphq.com"
|
2013-11-12 15:18:36 -05:00
|
|
|
|
|
|
|
controller = kls.new
|
|
|
|
assert_equal("http://www.basecamphq.com/admin/posts/new?param=value",
|
2016-08-06 12:54:50 -04:00
|
|
|
controller.send(:url_for, [:new, :admin, :post, { param: "value" }])
|
2013-11-12 15:18:36 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-04 15:23:29 -04:00
|
|
|
def test_url_for_with_array_is_unmodified
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
|
|
|
namespace :admin do
|
|
|
|
resources :posts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
kls = Class.new { include set.url_helpers }
|
2016-08-06 12:54:50 -04:00
|
|
|
kls.default_url_options[:host] = "www.basecamphq.com"
|
2015-09-04 15:23:29 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
original_components = [:new, :admin, :post, { param: "value" }]
|
2015-09-04 15:23:29 -04:00
|
|
|
components = original_components.dup
|
|
|
|
|
|
|
|
kls.new.url_for(components)
|
|
|
|
|
|
|
|
assert_equal(original_components, components)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-29 10:40:47 -05:00
|
|
|
def test_default_params_first_empty
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
|
|
|
get "(:param1)/test(/:param2)" => "index#index",
|
|
|
|
defaults: {
|
|
|
|
param1: 1,
|
|
|
|
param2: 2
|
|
|
|
},
|
|
|
|
constraints: {
|
|
|
|
param1: /\d*/,
|
|
|
|
param2: /\d+/
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
kls = Class.new { include set.url_helpers }
|
|
|
|
kls.default_url_options[:host] = "www.basecamphq.com"
|
|
|
|
|
|
|
|
assert_equal "http://www.basecamphq.com/test", kls.new.url_for(controller: "index", param1: "1")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
private
|
|
|
|
def extract_params(url)
|
2016-08-06 12:54:50 -04:00
|
|
|
url.split("?", 2).last.split("&").sort
|
2010-01-07 08:20:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-04-22 11:33:54 -04:00
|
|
|
end
|