2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "rails/engine"
|
2010-03-08 15:24:49 -05:00
|
|
|
|
|
|
|
class TestRoutingMount < ActionDispatch::IntegrationTest
|
|
|
|
Router = ActionDispatch::Routing::RouteSet.new
|
2010-12-09 07:15:25 -05:00
|
|
|
|
2014-07-16 18:41:47 -04:00
|
|
|
class AppWithRoutes < Rails::Engine
|
2010-12-09 07:15:25 -05:00
|
|
|
def self.routes
|
2013-10-30 07:05:13 -04:00
|
|
|
@routes ||= ActionDispatch::Routing::RouteSet.new
|
2010-12-09 07:15:25 -05:00
|
|
|
end
|
2014-07-16 18:41:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Test for mounting apps that respond to routes, but aren't Rails-like apps.
|
|
|
|
class SinatraLikeApp
|
|
|
|
def self.routes; Object.new; end
|
2010-12-09 07:15:25 -05:00
|
|
|
|
|
|
|
def self.call(env)
|
2016-08-16 03:30:11 -04:00
|
|
|
[200, { "Content-Type" => "text/html" }, ["OK"]]
|
2010-12-09 07:15:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-08 15:24:49 -05:00
|
|
|
Router.draw do
|
2010-03-12 05:50:45 -05:00
|
|
|
SprocketsApp = lambda { |env|
|
2016-08-16 03:30:11 -04:00
|
|
|
[200, { "Content-Type" => "text/html" }, ["#{env["SCRIPT_NAME"]} -- #{env["PATH_INFO"]}"]]
|
2010-03-12 05:50:45 -05:00
|
|
|
}
|
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
mount SprocketsApp, at: "/sprockets"
|
2019-04-15 07:21:02 -04:00
|
|
|
mount SprocketsApp, at: "/star*"
|
2010-03-08 15:24:49 -05:00
|
|
|
mount SprocketsApp => "/shorthand"
|
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
mount SinatraLikeApp, at: "/fakeengine", as: :fake
|
|
|
|
mount SinatraLikeApp, at: "/getfake", via: :get
|
2010-12-09 07:15:25 -05:00
|
|
|
|
2010-03-08 15:24:49 -05:00
|
|
|
scope "/its_a" do
|
2016-08-06 13:35:13 -04:00
|
|
|
mount SprocketsApp, at: "/sprocket"
|
2010-03-08 15:24:49 -05:00
|
|
|
end
|
2013-10-30 07:05:13 -04:00
|
|
|
|
|
|
|
resources :users do
|
2016-08-06 13:35:13 -04:00
|
|
|
mount AppWithRoutes, at: "/fakeengine", as: :fake_mounted_at_resource
|
2013-10-30 07:05:13 -04:00
|
|
|
end
|
2014-06-12 02:15:29 -04:00
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
mount SprocketsApp, at: "/", via: :get
|
2010-03-08 15:24:49 -05:00
|
|
|
end
|
|
|
|
|
2014-07-07 13:21:57 -04:00
|
|
|
APP = RoutedRackApp.new Router
|
2010-03-08 15:24:49 -05:00
|
|
|
def app
|
2014-07-07 13:21:57 -04:00
|
|
|
APP
|
2010-03-08 15:24:49 -05:00
|
|
|
end
|
|
|
|
|
2013-10-30 07:05:13 -04:00
|
|
|
def test_app_name_is_properly_generated_when_engine_is_mounted_in_resources
|
|
|
|
assert Router.mounted_helpers.method_defined?(:user_fake_mounted_at_resource),
|
|
|
|
"A mounted helper should be defined with a parent's prefix"
|
2015-08-18 19:33:18 -04:00
|
|
|
assert Router.named_routes.key?(:user_fake_mounted_at_resource),
|
2013-10-30 07:05:13 -04:00
|
|
|
"A named route should be defined with a parent's prefix"
|
|
|
|
end
|
|
|
|
|
2014-06-12 02:15:29 -04:00
|
|
|
def test_mounting_at_root_path
|
|
|
|
get "/omg"
|
|
|
|
assert_equal " -- /omg", response.body
|
2019-04-15 07:21:02 -04:00
|
|
|
|
|
|
|
get "/~omg"
|
|
|
|
assert_equal " -- /~omg", response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mounting_at_path_with_non_word_character
|
|
|
|
get "/star*/omg"
|
|
|
|
assert_equal "/star* -- /omg", response.body
|
2014-06-12 02:15:29 -04:00
|
|
|
end
|
|
|
|
|
2010-03-08 15:24:49 -05:00
|
|
|
def test_mounting_sets_script_name
|
|
|
|
get "/sprockets/omg"
|
|
|
|
assert_equal "/sprockets -- /omg", response.body
|
|
|
|
end
|
|
|
|
|
2012-05-07 17:53:57 -04:00
|
|
|
def test_mounting_works_with_nested_script_name
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/foo/sprockets/omg", headers: { "SCRIPT_NAME" => "/foo", "PATH_INFO" => "/sprockets/omg" }
|
2012-05-07 17:53:57 -04:00
|
|
|
assert_equal "/foo/sprockets -- /omg", response.body
|
|
|
|
end
|
|
|
|
|
2010-03-08 15:24:49 -05:00
|
|
|
def test_mounting_works_with_scope
|
|
|
|
get "/its_a/sprocket/omg"
|
|
|
|
assert_equal "/its_a/sprocket -- /omg", response.body
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-03-08 15:24:49 -05:00
|
|
|
def test_mounting_with_shorthand
|
|
|
|
get "/shorthand/omg"
|
|
|
|
assert_equal "/shorthand -- /omg", response.body
|
|
|
|
end
|
2010-12-09 07:15:25 -05:00
|
|
|
|
2019-02-03 16:34:02 -05:00
|
|
|
def test_mounting_does_not_match_similar_paths
|
|
|
|
get "/shorthandomg"
|
|
|
|
assert_not_equal "/shorthand -- /omg", response.body
|
|
|
|
assert_equal " -- /shorthandomg", response.body
|
|
|
|
end
|
|
|
|
|
2012-08-20 11:25:23 -04:00
|
|
|
def test_mounting_works_with_via
|
|
|
|
get "/getfake"
|
|
|
|
assert_equal "OK", response.body
|
|
|
|
|
|
|
|
post "/getfake"
|
|
|
|
assert_response :not_found
|
|
|
|
end
|
|
|
|
|
2010-12-09 07:15:25 -05:00
|
|
|
def test_with_fake_engine_does_not_call_invalid_method
|
|
|
|
get "/fakeengine"
|
|
|
|
assert_equal "OK", response.body
|
|
|
|
end
|
2011-12-19 16:34:54 -05:00
|
|
|
end
|