2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2017-11-06 17:24:44 -05:00
|
|
|
require "rails/engine"
|
2016-08-06 12:54:50 -04:00
|
|
|
require "controller/fake_controllers"
|
2011-02-13 18:24:46 -05:00
|
|
|
|
|
|
|
class SecureArticlesController < ArticlesController; end
|
|
|
|
class BlockArticlesController < ArticlesController; end
|
2012-02-24 08:16:31 -05:00
|
|
|
class QueryArticlesController < ArticlesController; end
|
2011-02-13 18:24:46 -05:00
|
|
|
|
2015-11-28 02:32:24 -05:00
|
|
|
class SecureBooksController < BooksController; end
|
|
|
|
class BlockBooksController < BooksController; end
|
|
|
|
class QueryBooksController < BooksController; end
|
|
|
|
|
2011-02-13 18:24:46 -05:00
|
|
|
class RoutingAssertionsTest < ActionController::TestCase
|
|
|
|
def setup
|
2015-11-28 02:32:24 -05:00
|
|
|
engine = Class.new(Rails::Engine) do
|
|
|
|
def self.name
|
|
|
|
"blog_engine"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
engine.routes.draw do
|
|
|
|
resources :books
|
|
|
|
|
2017-11-06 17:35:54 -05:00
|
|
|
scope "secure", constraints: { protocol: "https://" } do
|
|
|
|
resources :books, controller: "secure_books"
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
|
2017-11-06 17:35:54 -05:00
|
|
|
scope "block", constraints: lambda { |r| r.ssl? } do
|
|
|
|
resources :books, controller: "block_books"
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
|
2017-11-06 17:35:54 -05:00
|
|
|
scope "query", constraints: lambda { |r| r.params[:use_query] == "true" } do
|
|
|
|
resources :books, controller: "query_books"
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-13 18:24:46 -05:00
|
|
|
@routes = ActionDispatch::Routing::RouteSet.new
|
|
|
|
@routes.draw do
|
|
|
|
resources :articles
|
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
scope "secure", constraints: { protocol: "https://" } do
|
|
|
|
resources :articles, controller: "secure_articles"
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
scope "block", constraints: lambda { |r| r.ssl? } do
|
|
|
|
resources :articles, controller: "block_articles"
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
2012-02-24 08:16:31 -05:00
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
scope "query", constraints: lambda { |r| r.params[:use_query] == "true" } do
|
|
|
|
resources :articles, controller: "query_articles"
|
2012-02-24 08:16:31 -05:00
|
|
|
end
|
2015-11-28 02:32:24 -05:00
|
|
|
|
|
|
|
mount engine => "/shelf"
|
2018-02-09 13:51:20 -05:00
|
|
|
|
|
|
|
get "/shelf/foo", controller: "query_articles", action: "index"
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_generates
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_generates("/articles", controller: "articles", action: "index")
|
|
|
|
assert_generates("/articles/1", controller: "articles", action: "show", id: "1")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_generates_with_defaults
|
2017-08-12 07:31:46 -04:00
|
|
|
assert_generates("/articles/1/edit", { controller: "articles", action: "edit" }, { id: "1" })
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_generates_with_extras
|
2017-08-12 07:31:46 -04:00
|
|
|
assert_generates("/articles", { controller: "articles", action: "index", page: "1" }, {}, { page: "1" })
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_recognizes({ controller: "articles", action: "index" }, "/articles")
|
|
|
|
assert_recognizes({ controller: "articles", action: "show", id: "1" }, "/articles/1")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes_with_extras
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_recognizes({ controller: "articles", action: "index", page: "1" }, "/articles", page: "1")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
2012-05-11 02:23:24 -04:00
|
|
|
|
2011-02-13 18:24:46 -05:00
|
|
|
def test_assert_recognizes_with_method
|
2017-08-12 07:31:46 -04:00
|
|
|
assert_recognizes({ controller: "articles", action: "create" }, { path: "/articles", method: :post })
|
|
|
|
assert_recognizes({ controller: "articles", action: "update", id: "1" }, { path: "/articles/1", method: :put })
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes_with_hash_constraint
|
2012-05-19 17:28:14 -04:00
|
|
|
assert_raise(Assertion) do
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_recognizes({ controller: "secure_articles", action: "index" }, "http://test.host/secure/articles")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_recognizes({ controller: "secure_articles", action: "index", protocol: "https://" }, "https://test.host/secure/articles")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes_with_block_constraint
|
2012-05-19 17:28:14 -04:00
|
|
|
assert_raise(Assertion) do
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_recognizes({ controller: "block_articles", action: "index" }, "http://test.host/block/articles")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_recognizes({ controller: "block_articles", action: "index" }, "https://test.host/block/articles")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
2012-02-24 08:16:31 -05:00
|
|
|
def test_assert_recognizes_with_query_constraint
|
2012-05-19 17:28:14 -04:00
|
|
|
assert_raise(Assertion) do
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_recognizes({ controller: "query_articles", action: "index", use_query: "false" }, "/query/articles", use_query: "false")
|
2012-02-24 08:16:31 -05:00
|
|
|
end
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_recognizes({ controller: "query_articles", action: "index", use_query: "true" }, "/query/articles", use_query: "true")
|
2012-02-24 08:16:31 -05:00
|
|
|
end
|
|
|
|
|
2014-09-05 15:47:00 -04:00
|
|
|
def test_assert_recognizes_raises_message
|
|
|
|
err = assert_raise(Assertion) do
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_recognizes({ controller: "secure_articles", action: "index" }, "http://test.host/secure/articles", {}, "This is a really bad msg")
|
2014-09-05 15:47:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_match err.message, "This is a really bad msg"
|
|
|
|
end
|
|
|
|
|
2015-11-28 02:32:24 -05:00
|
|
|
def test_assert_recognizes_with_engine
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "books", action: "index" }, "/shelf/books")
|
|
|
|
assert_recognizes({ controller: "books", action: "show", id: "1" }, "/shelf/books/1")
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes_with_engine_and_extras
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "books", action: "index", page: "1" }, "/shelf/books", page: "1")
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes_with_engine_and_method
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "books", action: "create" }, { path: "/shelf/books", method: :post })
|
|
|
|
assert_recognizes({ controller: "books", action: "update", id: "1" }, { path: "/shelf/books/1", method: :put })
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes_with_engine_and_hash_constraint
|
|
|
|
assert_raise(Assertion) do
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "secure_books", action: "index" }, "http://test.host/shelf/secure/books")
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "secure_books", action: "index", protocol: "https://" }, "https://test.host/shelf/secure/books")
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes_with_engine_and_block_constraint
|
|
|
|
assert_raise(Assertion) do
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "block_books", action: "index" }, "http://test.host/shelf/block/books")
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "block_books", action: "index" }, "https://test.host/shelf/block/books")
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes_with_engine_and_query_constraint
|
|
|
|
assert_raise(Assertion) do
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "query_books", action: "index", use_query: "false" }, "/shelf/query/books", use_query: "false")
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "query_books", action: "index", use_query: "true" }, "/shelf/query/books", use_query: "true")
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_recognizes_raises_message_with_engine
|
|
|
|
err = assert_raise(Assertion) do
|
2017-11-06 17:35:54 -05:00
|
|
|
assert_recognizes({ controller: "secure_books", action: "index" }, "http://test.host/shelf/secure/books", {}, "This is a really bad msg")
|
2015-11-28 02:32:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_match err.message, "This is a really bad msg"
|
|
|
|
end
|
|
|
|
|
2018-02-09 13:51:20 -05:00
|
|
|
def test_assert_recognizes_continue_to_recoginize_after_it_tried_engines
|
|
|
|
assert_recognizes({ controller: "query_articles", action: "index" }, "/shelf/foo")
|
|
|
|
end
|
|
|
|
|
2011-02-13 18:24:46 -05:00
|
|
|
def test_assert_routing
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_routing("/articles", controller: "articles", action: "index")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
2014-04-29 21:26:16 -04:00
|
|
|
def test_assert_routing_raises_message
|
|
|
|
err = assert_raise(Assertion) do
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_routing("/thisIsNotARoute", { controller: "articles", action: "edit", id: "1" }, { id: "1" }, {}, "This is a really bad msg")
|
2014-04-29 21:26:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_match err.message, "This is a really bad msg"
|
|
|
|
end
|
|
|
|
|
2011-02-13 18:24:46 -05:00
|
|
|
def test_assert_routing_with_defaults
|
2017-08-12 07:31:46 -04:00
|
|
|
assert_routing("/articles/1/edit", { controller: "articles", action: "edit", id: "1" }, { id: "1" })
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_routing_with_extras
|
2017-08-12 07:31:46 -04:00
|
|
|
assert_routing("/articles", { controller: "articles", action: "index", page: "1" }, {}, { page: "1" })
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_routing_with_hash_constraint
|
2012-05-19 17:28:14 -04:00
|
|
|
assert_raise(Assertion) do
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_routing("http://test.host/secure/articles", controller: "secure_articles", action: "index")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_routing("https://test.host/secure/articles", controller: "secure_articles", action: "index", protocol: "https://")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_routing_with_block_constraint
|
2012-05-19 17:28:14 -04:00
|
|
|
assert_raise(Assertion) do
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_routing("http://test.host/block/articles", controller: "block_articles", action: "index")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_routing("https://test.host/block/articles", controller: "block_articles", action: "index")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_routing
|
|
|
|
with_routing do |routes|
|
|
|
|
routes.draw do
|
2016-08-06 13:35:13 -04:00
|
|
|
resources :articles, path: "artikel"
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_routing("/artikel", controller: "articles", action: "index")
|
2012-05-19 17:28:14 -04:00
|
|
|
assert_raise(Assertion) do
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_routing("/articles", controller: "articles", action: "index")
|
2011-02-13 18:24:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|