2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2010-03-05 00:59:42 -05:00
|
|
|
|
|
|
|
module TestUrlGeneration
|
|
|
|
class WithMountPoint < ActionDispatch::IntegrationTest
|
|
|
|
class ::MyRouteGeneratingController < ActionController::Base
|
|
|
|
def index
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: foo_path
|
2010-03-05 00:59:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-24 14:30:15 -04:00
|
|
|
Routes = ActionDispatch::Routing::RouteSet.new
|
2012-05-07 17:53:57 -04:00
|
|
|
Routes.draw do
|
2016-08-06 13:35:13 -04:00
|
|
|
get "/foo", to: "my_route_generating#index", as: :foo
|
2012-05-07 17:53:57 -04:00
|
|
|
|
2014-05-05 18:25:29 -04:00
|
|
|
resources :bars
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
mount MyRouteGeneratingController.action(:index), at: "/bar"
|
2012-05-07 17:53:57 -04:00
|
|
|
end
|
2010-03-05 00:59:42 -05:00
|
|
|
|
2018-09-24 14:30:15 -04:00
|
|
|
class ::MyRouteGeneratingController
|
|
|
|
include Routes.url_helpers
|
|
|
|
end
|
|
|
|
|
2014-07-07 13:21:57 -04:00
|
|
|
APP = build_app Routes
|
|
|
|
|
2010-07-01 18:29:20 -04:00
|
|
|
def _routes
|
|
|
|
Routes
|
2010-03-05 00:59:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
2014-07-07 13:21:57 -04:00
|
|
|
APP
|
2010-03-05 00:59:42 -05:00
|
|
|
end
|
|
|
|
|
2010-03-05 01:17:25 -05:00
|
|
|
test "generating URLS normally" do
|
|
|
|
assert_equal "/foo", foo_path
|
2010-03-05 00:59:42 -05:00
|
|
|
end
|
|
|
|
|
2010-03-05 01:17:25 -05:00
|
|
|
test "accepting a :script_name option" do
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal "/bar/foo", foo_path(script_name: "/bar")
|
2010-03-05 00:59:42 -05:00
|
|
|
end
|
|
|
|
|
2012-05-07 17:53:57 -04:00
|
|
|
test "the request's SCRIPT_NAME takes precedence over the route" do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/foo", headers: { "SCRIPT_NAME" => "/new", "action_dispatch.routes" => Routes }
|
2010-03-05 00:59:42 -05:00
|
|
|
assert_equal "/new/foo", response.body
|
|
|
|
end
|
2010-03-15 22:26:58 -04:00
|
|
|
|
2012-05-07 17:53:57 -04:00
|
|
|
test "the request's SCRIPT_NAME wraps the mounted app's" do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/new/bar/foo", headers: { "SCRIPT_NAME" => "/new", "PATH_INFO" => "/bar/foo", "action_dispatch.routes" => Routes }
|
2012-05-07 17:53:57 -04:00
|
|
|
assert_equal "/new/bar/foo", response.body
|
|
|
|
end
|
|
|
|
|
2010-03-15 22:26:58 -04:00
|
|
|
test "handling http protocol with https set" do
|
|
|
|
https!
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal "http://www.example.com/foo", foo_url(protocol: "http")
|
2010-03-15 22:26:58 -04:00
|
|
|
end
|
2013-03-18 17:09:15 -04:00
|
|
|
|
|
|
|
test "extracting protocol from host when protocol not present" do
|
|
|
|
assert_equal "httpz://www.example.com/foo", foo_url(host: "httpz://www.example.com", protocol: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "formatting host when protocol is present" do
|
|
|
|
assert_equal "http://www.example.com/foo", foo_url(host: "httpz://www.example.com", protocol: "http://")
|
|
|
|
end
|
2013-04-18 12:02:21 -04:00
|
|
|
|
|
|
|
test "default ports are removed from the host" do
|
|
|
|
assert_equal "http://www.example.com/foo", foo_url(host: "www.example.com:80", protocol: "http://")
|
|
|
|
assert_equal "https://www.example.com/foo", foo_url(host: "www.example.com:443", protocol: "https://")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "port is extracted from the host" do
|
|
|
|
assert_equal "http://www.example.com:8080/foo", foo_url(host: "www.example.com:8080", protocol: "http://")
|
2014-05-11 07:56:33 -04:00
|
|
|
assert_equal "//www.example.com:8080/foo", foo_url(host: "www.example.com:8080", protocol: "//")
|
|
|
|
assert_equal "//www.example.com:80/foo", foo_url(host: "www.example.com:80", protocol: "//")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "port option is used" do
|
|
|
|
assert_equal "http://www.example.com:8080/foo", foo_url(host: "www.example.com", protocol: "http://", port: 8080)
|
|
|
|
assert_equal "//www.example.com:8080/foo", foo_url(host: "www.example.com", protocol: "//", port: 8080)
|
|
|
|
assert_equal "//www.example.com:80/foo", foo_url(host: "www.example.com", protocol: "//", port: 80)
|
2013-04-18 12:02:21 -04:00
|
|
|
end
|
|
|
|
|
2013-12-15 14:14:37 -05:00
|
|
|
test "port option overrides the host" do
|
2013-04-18 12:02:21 -04:00
|
|
|
assert_equal "http://www.example.com:8080/foo", foo_url(host: "www.example.com:8443", protocol: "http://", port: 8080)
|
2014-05-11 07:56:33 -04:00
|
|
|
assert_equal "//www.example.com:8080/foo", foo_url(host: "www.example.com:8443", protocol: "//", port: 8080)
|
|
|
|
assert_equal "//www.example.com:80/foo", foo_url(host: "www.example.com:443", protocol: "//", port: 80)
|
2013-04-18 12:02:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "port option disables the host when set to nil" do
|
|
|
|
assert_equal "http://www.example.com/foo", foo_url(host: "www.example.com:8443", protocol: "http://", port: nil)
|
2014-05-11 07:56:33 -04:00
|
|
|
assert_equal "//www.example.com/foo", foo_url(host: "www.example.com:8443", protocol: "//", port: nil)
|
2013-04-18 12:02:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "port option disables the host when set to false" do
|
|
|
|
assert_equal "http://www.example.com/foo", foo_url(host: "www.example.com:8443", protocol: "http://", port: false)
|
2014-05-11 07:56:33 -04:00
|
|
|
assert_equal "//www.example.com/foo", foo_url(host: "www.example.com:8443", protocol: "//", port: false)
|
2013-04-18 12:02:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "keep subdomain when key is true" do
|
|
|
|
assert_equal "http://www.example.com/foo", foo_url(subdomain: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "keep subdomain when key is missing" do
|
|
|
|
assert_equal "http://www.example.com/foo", foo_url
|
|
|
|
end
|
|
|
|
|
|
|
|
test "omit subdomain when key is nil" do
|
|
|
|
assert_equal "http://example.com/foo", foo_url(subdomain: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "omit subdomain when key is false" do
|
|
|
|
assert_equal "http://example.com/foo", foo_url(subdomain: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "omit subdomain when key is blank" do
|
|
|
|
assert_equal "http://example.com/foo", foo_url(subdomain: "")
|
|
|
|
end
|
2014-05-05 18:25:29 -04:00
|
|
|
|
|
|
|
test "generating URLs with trailing slashes" do
|
|
|
|
assert_equal "/bars.json", bars_path(
|
|
|
|
trailing_slash: true,
|
2016-08-06 12:54:50 -04:00
|
|
|
format: "json"
|
2014-05-05 18:25:29 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generating URLS with querystring and trailing slashes" do
|
|
|
|
assert_equal "/bars.json?a=b", bars_path(
|
|
|
|
trailing_slash: true,
|
2016-08-06 12:54:50 -04:00
|
|
|
a: "b",
|
|
|
|
format: "json"
|
2014-05-05 18:25:29 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-08-14 18:19:56 -04:00
|
|
|
test "generating URLS with empty querystring" do
|
|
|
|
assert_equal "/bars.json", bars_path(
|
|
|
|
a: {},
|
2016-08-06 12:54:50 -04:00
|
|
|
format: "json"
|
2015-08-14 18:19:56 -04:00
|
|
|
)
|
|
|
|
end
|
2010-03-05 00:59:42 -05:00
|
|
|
end
|
2010-03-15 22:26:58 -04:00
|
|
|
end
|