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