2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2012-12-14 08:15:57 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
module Routing
|
|
|
|
class RouteSetTest < ActiveSupport::TestCase
|
|
|
|
class SimpleApp
|
|
|
|
def initialize(response)
|
|
|
|
@response = response
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2016-08-06 12:54:50 -04:00
|
|
|
[ 200, { "Content-Type" => "text/plain" }, [response] ]
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@set = RouteSet.new
|
|
|
|
end
|
|
|
|
|
2015-05-18 18:21:20 -04:00
|
|
|
test "not being empty when route is added" do
|
|
|
|
assert empty?
|
|
|
|
|
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "foo", to: SimpleApp.new("foo#index")
|
2015-05-18 18:21:20 -04:00
|
|
|
end
|
|
|
|
|
2015-05-18 18:33:25 -04:00
|
|
|
assert_not empty?
|
2015-05-18 18:21:20 -04:00
|
|
|
end
|
|
|
|
|
2019-04-01 13:28:02 -04:00
|
|
|
test "URL helpers are added when route is added" do
|
2012-12-14 08:15:57 -05:00
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "foo", to: SimpleApp.new("foo#index")
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/foo", url_helpers.foo_path
|
2012-12-14 08:15:57 -05:00
|
|
|
assert_raises NoMethodError do
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/bar", url_helpers.bar_path
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "foo", to: SimpleApp.new("foo#index")
|
|
|
|
get "bar", to: SimpleApp.new("bar#index")
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/foo", url_helpers.foo_path
|
|
|
|
assert_equal "/bar", url_helpers.bar_path
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
|
2019-04-01 13:28:02 -04:00
|
|
|
test "URL helpers are updated when route is updated" do
|
2012-12-14 08:15:57 -05:00
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "bar", to: SimpleApp.new("bar#index"), as: :bar
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/bar", url_helpers.bar_path
|
2012-12-14 08:15:57 -05:00
|
|
|
|
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "baz", to: SimpleApp.new("baz#index"), as: :bar
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/baz", url_helpers.bar_path
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
|
2019-04-01 13:28:02 -04:00
|
|
|
test "URL helpers are removed when route is removed" do
|
2012-12-14 08:15:57 -05:00
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "foo", to: SimpleApp.new("foo#index")
|
|
|
|
get "bar", to: SimpleApp.new("bar#index")
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/foo", url_helpers.foo_path
|
|
|
|
assert_equal "/bar", url_helpers.bar_path
|
2012-12-14 08:15:57 -05:00
|
|
|
|
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "foo", to: SimpleApp.new("foo#index")
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/foo", url_helpers.foo_path
|
2012-12-14 08:15:57 -05:00
|
|
|
assert_raises NoMethodError do
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/bar", url_helpers.bar_path
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-28 12:43:33 -04:00
|
|
|
test "only_path: true with *_url and no :host option" do
|
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "foo", to: SimpleApp.new("foo#index")
|
2014-10-28 12:43:33 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/foo", url_helpers.foo_url(only_path: true)
|
2014-10-28 12:43:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "only_path: false with *_url and no :host option" do
|
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "foo", to: SimpleApp.new("foo#index")
|
2014-10-28 12:43:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "http://example.com/foo", url_helpers.foo_url(only_path: false)
|
2014-10-28 12:43:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "only_path: false with *_url and local :host option" do
|
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "foo", to: SimpleApp.new("foo#index")
|
2014-10-28 12:43:33 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "http://example.com/foo", url_helpers.foo_url(only_path: false, host: "example.com")
|
2014-10-28 12:43:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "only_path: false with *_url and global :host option" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@set.default_url_options = { host: "example.com" }
|
2014-10-28 12:43:33 -04:00
|
|
|
|
|
|
|
draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "foo", to: SimpleApp.new("foo#index")
|
2014-10-28 12:43:33 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "http://example.com/foo", url_helpers.foo_url(only_path: false)
|
2014-10-28 12:43:33 -04:00
|
|
|
end
|
|
|
|
|
2013-05-16 17:21:53 -04:00
|
|
|
test "explicit keys win over implicit keys" do
|
|
|
|
draw do
|
|
|
|
resources :foo do
|
2016-08-06 12:54:50 -04:00
|
|
|
resources :bar, to: SimpleApp.new("foo#show")
|
2013-05-16 17:21:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/foo/1/bar/2", url_helpers.foo_bar_path(1, 2)
|
|
|
|
assert_equal "/foo/1/bar/2", url_helpers.foo_bar_path(2, foo_id: 1)
|
2013-05-16 17:21:53 -04:00
|
|
|
end
|
|
|
|
|
2014-12-13 15:46:52 -05:00
|
|
|
test "having an optional scope with resources" do
|
|
|
|
draw do
|
|
|
|
scope "(/:foo)" do
|
|
|
|
resources :users
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/users/1", url_helpers.user_path(1)
|
|
|
|
assert_equal "/users/1", url_helpers.user_path(1, foo: nil)
|
|
|
|
assert_equal "/a/users/1", url_helpers.user_path(1, foo: "a")
|
2014-12-13 15:46:52 -05:00
|
|
|
end
|
|
|
|
|
2017-06-22 08:10:21 -04:00
|
|
|
test "implicit path components consistently return the same result" do
|
|
|
|
draw do
|
|
|
|
resources :users, to: SimpleApp.new("foo#index")
|
|
|
|
end
|
|
|
|
assert_equal "/users/1.json", url_helpers.user_path(1, :json)
|
|
|
|
assert_equal "/users/1.json", url_helpers.user_path(1, format: :json)
|
|
|
|
assert_equal "/users/1.json", url_helpers.user_path(1, :json)
|
|
|
|
end
|
|
|
|
|
2012-12-14 08:15:57 -05:00
|
|
|
private
|
|
|
|
def draw(&block)
|
|
|
|
@set.draw(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_helpers
|
|
|
|
@set.url_helpers
|
|
|
|
end
|
2015-05-18 18:21:20 -04:00
|
|
|
|
|
|
|
def empty?
|
|
|
|
@set.empty?
|
|
|
|
end
|
2012-12-14 08:15:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|