2017-07-24 20:20:53 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 16:54:50 +00:00
|
|
|
require "abstract_unit"
|
|
|
|
require "controller/fake_controllers"
|
2005-07-18 03:12:45 +00:00
|
|
|
|
2015-07-08 21:14:59 +00:00
|
|
|
class UrlRewriterTests < ActionController::TestCase
|
2010-03-09 05:08:31 +00:00
|
|
|
class Rewriter
|
|
|
|
def initialize(request)
|
|
|
|
@options = {
|
2016-08-06 17:35:13 +00:00
|
|
|
host: request.host_with_port,
|
|
|
|
protocol: request.protocol
|
2010-03-09 05:08:31 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-03-30 19:05:42 +00:00
|
|
|
def rewrite(routes, options)
|
|
|
|
routes.url_for(@options.merge(options))
|
2010-03-09 05:08:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-18 03:12:45 +00:00
|
|
|
def setup
|
|
|
|
@params = {}
|
2017-12-14 08:30:54 +00:00
|
|
|
@rewriter = Rewriter.new(@request)
|
2010-08-05 13:44:23 +00:00
|
|
|
@routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
|
|
|
|
r.draw do
|
2016-03-01 08:48:53 +00:00
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 16:54:50 +00:00
|
|
|
get ":controller(/:action(/:id))"
|
2016-03-01 08:48:53 +00:00
|
|
|
end
|
2010-08-05 13:44:23 +00:00
|
|
|
end
|
|
|
|
end
|
2008-07-24 18:41:51 +00:00
|
|
|
end
|
2005-09-06 22:06:11 +00:00
|
|
|
|
2007-02-25 20:22:09 +00:00
|
|
|
def test_port
|
2016-08-06 16:54:50 +00:00
|
|
|
assert_equal("http://test.host:1271/c/a/i",
|
2016-08-06 17:35:13 +00:00
|
|
|
@rewriter.rewrite(@routes, controller: "c", action: "a", id: "i", port: 1271)
|
2007-02-25 20:22:09 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_protocol_with_and_without_separator
|
2016-08-06 16:54:50 +00:00
|
|
|
assert_equal("https://test.host/c/a/i",
|
2016-08-06 17:35:13 +00:00
|
|
|
@rewriter.rewrite(@routes, protocol: "https", controller: "c", action: "a", id: "i")
|
2007-02-25 20:22:09 +00:00
|
|
|
)
|
|
|
|
|
2016-08-06 16:54:50 +00:00
|
|
|
assert_equal("https://test.host/c/a/i",
|
2016-08-06 17:35:13 +00:00
|
|
|
@rewriter.rewrite(@routes, protocol: "https://", controller: "c", action: "a", id: "i")
|
2007-03-03 22:19:54 +00:00
|
|
|
)
|
|
|
|
end
|
2008-07-24 18:41:51 +00:00
|
|
|
|
2007-03-03 22:19:54 +00:00
|
|
|
def test_user_name_and_password
|
|
|
|
assert_equal(
|
2016-08-06 16:54:50 +00:00
|
|
|
"http://david:secret@test.host/c/a/i",
|
2016-08-06 17:35:13 +00:00
|
|
|
@rewriter.rewrite(@routes, user: "david", password: "secret", controller: "c", action: "a", id: "i")
|
2007-02-25 20:22:09 +00:00
|
|
|
)
|
|
|
|
end
|
2007-03-04 20:10:51 +00:00
|
|
|
|
|
|
|
def test_user_name_and_password_with_escape_codes
|
|
|
|
assert_equal(
|
2016-08-06 16:54:50 +00:00
|
|
|
"http://openid.aol.com%2Fnextangler:one+two%3F@test.host/c/a/i",
|
2016-08-06 17:35:13 +00:00
|
|
|
@rewriter.rewrite(@routes, user: "openid.aol.com/nextangler", password: "one two?", controller: "c", action: "a", id: "i")
|
2007-03-04 20:10:51 +00:00
|
|
|
)
|
|
|
|
end
|
2008-07-24 18:41:51 +00:00
|
|
|
|
|
|
|
def test_anchor
|
|
|
|
assert_equal(
|
2016-08-06 16:54:50 +00:00
|
|
|
"http://test.host/c/a/i#anchor",
|
2016-08-06 17:35:13 +00:00
|
|
|
@rewriter.rewrite(@routes, controller: "c", action: "a", id: "i", anchor: "anchor")
|
2008-07-24 18:41:51 +00:00
|
|
|
)
|
2007-05-02 04:40:33 +00:00
|
|
|
end
|
2007-03-04 20:10:51 +00:00
|
|
|
|
2009-06-02 17:28:44 +00:00
|
|
|
def test_anchor_should_call_to_param
|
|
|
|
assert_equal(
|
2016-08-06 16:54:50 +00:00
|
|
|
"http://test.host/c/a/i#anchor",
|
2016-08-06 17:35:13 +00:00
|
|
|
@rewriter.rewrite(@routes, controller: "c", action: "a", id: "i", anchor: Struct.new(:to_param).new("anchor"))
|
2009-06-02 17:28:44 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-10-14 04:41:30 +00:00
|
|
|
def test_anchor_should_be_uri_escaped
|
2009-06-02 17:28:44 +00:00
|
|
|
assert_equal(
|
2016-08-06 16:54:50 +00:00
|
|
|
"http://test.host/c/a/i#anc/hor",
|
2016-08-06 17:35:13 +00:00
|
|
|
@rewriter.rewrite(@routes, controller: "c", action: "a", id: "i", anchor: Struct.new(:to_param).new("anc/hor"))
|
2009-06-02 17:28:44 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2007-07-14 09:28:56 +00:00
|
|
|
def test_trailing_slash
|
2016-08-16 07:30:11 +00:00
|
|
|
options = { controller: "foo", action: "bar", id: "3", only_path: true }
|
2016-08-06 16:54:50 +00:00
|
|
|
assert_equal "/foo/bar/3", @rewriter.rewrite(@routes, options)
|
2016-08-06 17:44:11 +00:00
|
|
|
assert_equal "/foo/bar/3?query=string", @rewriter.rewrite(@routes, options.merge(query: "string"))
|
|
|
|
options.update(trailing_slash: true)
|
2016-08-06 16:54:50 +00:00
|
|
|
assert_equal "/foo/bar/3/", @rewriter.rewrite(@routes, options)
|
2016-08-06 17:44:11 +00:00
|
|
|
options.update(query: "string")
|
2016-08-06 16:54:50 +00:00
|
|
|
assert_equal "/foo/bar/3/?query=string", @rewriter.rewrite(@routes, options)
|
2007-07-14 09:28:56 +00:00
|
|
|
end
|
2005-07-18 03:12:45 +00:00
|
|
|
end
|