2010-03-05 00:59:42 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
module TestUrlGeneration
|
|
|
|
class WithMountPoint < ActionDispatch::IntegrationTest
|
2010-07-01 18:29:20 -04:00
|
|
|
Routes = ActionDispatch::Routing::RouteSet.new
|
2012-05-07 17:53:57 -04:00
|
|
|
include Routes.url_helpers
|
2010-03-05 00:59:42 -05:00
|
|
|
|
|
|
|
class ::MyRouteGeneratingController < ActionController::Base
|
2010-07-01 18:29:20 -04:00
|
|
|
include Routes.url_helpers
|
2010-03-05 00:59:42 -05:00
|
|
|
def index
|
|
|
|
render :text => foo_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-07 17:53:57 -04:00
|
|
|
Routes.draw do
|
|
|
|
get "/foo", :to => "my_route_generating#index", :as => :foo
|
|
|
|
|
2014-05-05 18:25:29 -04:00
|
|
|
resources :bars
|
|
|
|
|
2012-05-07 17:53:57 -04:00
|
|
|
mount MyRouteGeneratingController.action(:index), at: '/bar'
|
|
|
|
end
|
2010-03-05 00:59:42 -05:00
|
|
|
|
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
|
2010-03-05 00:59:42 -05:00
|
|
|
assert_equal "/bar/foo", foo_path(:script_name => "/bar")
|
|
|
|
end
|
|
|
|
|
2012-05-07 17:53:57 -04:00
|
|
|
test "the request's SCRIPT_NAME takes precedence over the route" do
|
2015-01-29 09:19:41 -05: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
|
2015-01-04 04:35:06 -05: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!
|
2010-03-16 01:44:49 -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,
|
|
|
|
format: 'json'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generating URLS with querystring and trailing slashes" do
|
|
|
|
assert_equal "/bars.json?a=b", bars_path(
|
|
|
|
trailing_slash: true,
|
|
|
|
a: 'b',
|
|
|
|
format: 'json'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-03-05 00:59:42 -05:00
|
|
|
end
|
2010-03-15 22:26:58 -04:00
|
|
|
end
|
2010-07-13 18:46:42 -04:00
|
|
|
|