1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
# frozen_string_literal: true
require "abstract_unit"
require "rails/engine"
require "controller/fake_controllers"
class SecureArticlesController < ArticlesController; end
class BlockArticlesController < ArticlesController; end
class QueryArticlesController < ArticlesController; end
class SecureBooksController < BooksController; end
class BlockBooksController < BooksController; end
class QueryBooksController < BooksController; end
module RoutingAssertionsSharedTests
def setup
root_engine = Class.new(Rails::Engine) do
def self.name
"root_engine"
end
end
root_engine.routes.draw do
root to: "books#index"
end
engine = Class.new(Rails::Engine) do
def self.name
"blog_engine"
end
end
engine.routes.draw do
resources :books
scope "secure", constraints: { protocol: "https://" } do
resources :books, controller: "secure_books"
end
scope "block", constraints: lambda { |r| r.ssl? } do
resources :books, controller: "block_books"
end
scope "query", constraints: lambda { |r| r.params[:use_query] == "true" } do
resources :books, controller: "query_books"
end
end
@routes = ActionDispatch::Routing::RouteSet.new
@routes.draw do
resources :articles
scope "secure", constraints: { protocol: "https://" } do
resources :articles, controller: "secure_articles"
end
scope "block", constraints: lambda { |r| r.ssl? } do
resources :articles, controller: "block_articles"
end
scope "query", constraints: lambda { |r| r.params[:use_query] == "true" } do
resources :articles, controller: "query_articles"
end
mount engine => "/shelf"
mount root_engine => "/"
get "/shelf/foo", controller: "query_articles", action: "index"
end
end
def test_assert_generates
assert_generates("/articles", controller: "articles", action: "index")
assert_generates("/articles/1", controller: "articles", action: "show", id: "1")
end
def test_assert_generates_with_defaults
assert_generates("/articles/1/edit", { controller: "articles", action: "edit" }, { id: "1" })
end
def test_assert_generates_with_extras
assert_generates("/articles", { controller: "articles", action: "index", page: "1" }, {}, { page: "1" })
end
def test_assert_recognizes
assert_recognizes({ controller: "articles", action: "index" }, "/articles")
assert_recognizes({ controller: "articles", action: "show", id: "1" }, "/articles/1")
end
def test_assert_recognizes_with_extras
assert_recognizes({ controller: "articles", action: "index", page: "1" }, "/articles", page: "1")
end
def test_assert_recognizes_with_method
assert_recognizes({ controller: "articles", action: "create" }, { path: "/articles", method: :post })
assert_recognizes({ controller: "articles", action: "update", id: "1" }, { path: "/articles/1", method: :put })
end
def test_assert_recognizes_with_hash_constraint
assert_raise(Minitest::Assertion) do
assert_recognizes({ controller: "secure_articles", action: "index" }, "http://test.host/secure/articles")
end
assert_recognizes({ controller: "secure_articles", action: "index", protocol: "https://" }, "https://test.host/secure/articles")
end
def test_assert_recognizes_with_block_constraint
assert_raise(Minitest::Assertion) do
assert_recognizes({ controller: "block_articles", action: "index" }, "http://test.host/block/articles")
end
assert_recognizes({ controller: "block_articles", action: "index" }, "https://test.host/block/articles")
end
def test_assert_recognizes_with_query_constraint
assert_raise(Minitest::Assertion) do
assert_recognizes({ controller: "query_articles", action: "index", use_query: "false" }, "/query/articles", use_query: "false")
end
assert_recognizes({ controller: "query_articles", action: "index", use_query: "true" }, "/query/articles", use_query: "true")
end
def test_assert_recognizes_raises_message
err = assert_raise(Minitest::Assertion) do
assert_recognizes({ controller: "secure_articles", action: "index" }, "http://test.host/secure/articles", {}, "This is a really bad msg")
end
assert_match err.message, "This is a really bad msg"
end
def test_assert_recognizes_with_engine
assert_recognizes({ controller: "books", action: "index" }, "/shelf/books")
assert_recognizes({ controller: "books", action: "show", id: "1" }, "/shelf/books/1")
end
def test_assert_recognizes_with_engine_at_root
assert_recognizes({ controller: "books", action: "index" }, "/")
end
def test_assert_recognizes_with_engine_and_extras
assert_recognizes({ controller: "books", action: "index", page: "1" }, "/shelf/books", page: "1")
end
def test_assert_recognizes_with_engine_and_method
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 })
end
def test_assert_recognizes_with_engine_and_hash_constraint
assert_raise(Minitest::Assertion) do
assert_recognizes({ controller: "secure_books", action: "index" }, "http://test.host/shelf/secure/books")
end
assert_recognizes({ controller: "secure_books", action: "index", protocol: "https://" }, "https://test.host/shelf/secure/books")
end
def test_assert_recognizes_with_engine_and_block_constraint
assert_raise(Minitest::Assertion) do
assert_recognizes({ controller: "block_books", action: "index" }, "http://test.host/shelf/block/books")
end
assert_recognizes({ controller: "block_books", action: "index" }, "https://test.host/shelf/block/books")
end
def test_assert_recognizes_with_engine_and_query_constraint
assert_raise(Minitest::Assertion) do
assert_recognizes({ controller: "query_books", action: "index", use_query: "false" }, "/shelf/query/books", use_query: "false")
end
assert_recognizes({ controller: "query_books", action: "index", use_query: "true" }, "/shelf/query/books", use_query: "true")
end
def test_assert_recognizes_raises_message_with_engine
err = assert_raise(Minitest::Assertion) do
assert_recognizes({ controller: "secure_books", action: "index" }, "http://test.host/shelf/secure/books", {}, "This is a really bad msg")
end
assert_match err.message, "This is a really bad msg"
end
def test_assert_recognizes_continue_to_recognize_after_it_tried_engines
assert_recognizes({ controller: "query_articles", action: "index" }, "/shelf/foo")
end
def test_assert_routing
assert_routing("/articles", controller: "articles", action: "index")
end
def test_assert_routing_raises_message
err = assert_raise(Minitest::Assertion) do
assert_routing("/thisIsNotARoute", { controller: "articles", action: "edit", id: "1" }, { id: "1" }, {}, "This is a really bad msg")
end
assert_match err.message, "This is a really bad msg"
end
def test_assert_routing_with_defaults
assert_routing("/articles/1/edit", { controller: "articles", action: "edit", id: "1" }, { id: "1" })
end
def test_assert_routing_with_extras
assert_routing("/articles", { controller: "articles", action: "index", page: "1" }, {}, { page: "1" })
end
def test_assert_routing_with_hash_constraint
assert_raise(Minitest::Assertion) do
assert_routing("http://test.host/secure/articles", controller: "secure_articles", action: "index")
end
assert_routing("https://test.host/secure/articles", controller: "secure_articles", action: "index", protocol: "https://")
end
def test_assert_routing_with_block_constraint
assert_raise(Minitest::Assertion) do
assert_routing("http://test.host/block/articles", controller: "block_articles", action: "index")
end
assert_routing("https://test.host/block/articles", controller: "block_articles", action: "index")
end
def test_with_routing
with_routing do |routes|
routes.draw do
resources :articles, path: "artikel"
end
assert_routing("/artikel", controller: "articles", action: "index")
assert_raise(Minitest::Assertion) do
assert_routing("/articles", controller: "articles", action: "index")
end
end
end
module WithRoutingSharedTests
extend ActiveSupport::Concern
def before_setup
@routes = ActionDispatch::Routing::RouteSet.new
@routes.draw do
resources :articles
end
super
end
included do
with_routing do |routes|
routes.draw do
resources :articles, path: "artikel"
end
end
end
def test_with_routing_for_the_entire_test_file
assert_routing("/artikel", controller: "articles", action: "index")
assert_raise(Minitest::Assertion) do
assert_routing("/articles", controller: "articles", action: "index")
end
end
def test_with_routing_for_entire_test_file_can_be_overwritten_for_individual_test
with_routing do |routes|
routes.draw do
resources :articles, path: "articolo"
end
assert_routing("/articolo", controller: "articles", action: "index")
assert_raise(Minitest::Assertion) do
assert_routing("/artikel", controller: "articles", action: "index")
end
end
assert_routing("/artikel", controller: "articles", action: "index")
assert_raise(Minitest::Assertion) do
assert_routing("/articolo", controller: "articles", action: "index")
end
end
end
end
class RoutingAssertionsControllerTest < ActionController::TestCase
include RoutingAssertionsSharedTests
class WithRoutingTest < ActionController::TestCase
include RoutingAssertionsSharedTests::WithRoutingSharedTests
end
end
class RoutingAssertionsIntegrationTest < ActionDispatch::IntegrationTest
include RoutingAssertionsSharedTests
test "https and host settings are set on new session" do
https!
host! "newhost.com"
with_routing do |routes|
routes.draw { }
assert_predicate integration_session, :https?
assert_equal "newhost.com", integration_session.host
end
end
class WithRoutingTest < ActionDispatch::IntegrationTest
include RoutingAssertionsSharedTests::WithRoutingSharedTests
end
class WithRoutingSettingsTest < ActionDispatch::IntegrationTest
setup do
https!
host! "newhost.com"
end
with_routing do |routes|
routes.draw { }
end
test "https and host settings are set on new session" do
assert_predicate integration_session, :https?
assert_equal "newhost.com", integration_session.host
end
end
end
|