aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch
diff options
context:
space:
mode:
authorJosh Kalderimis <josh.kalderimis@gmail.com>2010-11-30 16:36:01 +0100
committerJosh Kalderimis <josh.kalderimis@gmail.com>2010-11-30 16:36:01 +0100
commit0bda6f1ec664fcfd1b312492a6419e3d76d5baa7 (patch)
tree9839420c5a2c4f4ff1e2029589747bffa8126f89 /actionpack/test/dispatch
parent2c08ee97c78b7eb0883f1b0347c69a088c109388 (diff)
downloadrails-0bda6f1ec664fcfd1b312492a6419e3d76d5baa7.tar.gz
rails-0bda6f1ec664fcfd1b312492a6419e3d76d5baa7.tar.bz2
rails-0bda6f1ec664fcfd1b312492a6419e3d76d5baa7.zip
The redirect routing method now allows for a hash of options which only changes the relevant parts of the url, or an object which responds to call can be supplied so common redirect rules can be easily reused. This commit includes a change where url generation from parts has been moved to AD::Http::URL as a class method.
Diffstat (limited to 'actionpack/test/dispatch')
-rw-r--r--actionpack/test/dispatch/request_test.rb25
-rw-r--r--actionpack/test/dispatch/routing_test.rb48
2 files changed, 73 insertions, 0 deletions
diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb
index 8f672c1149..75b674ec1a 100644
--- a/actionpack/test/dispatch/request_test.rb
+++ b/actionpack/test/dispatch/request_test.rb
@@ -1,6 +1,31 @@
require 'abstract_unit'
class RequestTest < ActiveSupport::TestCase
+
+ def url_for(options = {})
+ options.reverse_merge!(:host => 'www.example.com')
+ ActionDispatch::Http::URL.url_for(options)
+ end
+
+ test "url_for class method" do
+ e = assert_raise(ArgumentError) { url_for(:host => nil) }
+ assert_match(/Please provide the :host parameter/, e.message)
+
+ assert_equal '/books', url_for(:only_path => true, :path => '/books')
+
+ assert_equal 'http://www.example.com', url_for
+ assert_equal 'http://api.example.com', url_for(:subdomain => 'api')
+ assert_equal 'http://www.ror.com', url_for(:domain => 'ror.com')
+ assert_equal 'http://api.ror.co.uk', url_for(:host => 'www.ror.co.uk', :subdomain => 'api', :tld_length => 2)
+ assert_equal 'http://www.example.com:8080', url_for(:port => 8080)
+ assert_equal 'https://www.example.com', url_for(:protocol => 'https')
+ assert_equal 'http://www.example.com/docs', url_for(:path => '/docs')
+ assert_equal 'http://www.example.com#signup', url_for(:anchor => 'signup')
+ assert_equal 'http://www.example.com/', url_for(:trailing_slash => true)
+ assert_equal 'http://dhh:supersecret@www.example.com', url_for(:user => 'dhh', :password => 'supersecret')
+ assert_equal 'http://www.example.com?search=books', url_for(:params => { :search => 'books' })
+ end
+
test "remote ip" do
request = stub_request 'REMOTE_ADDR' => '1.2.3.4'
assert_equal '1.2.3.4', request.remote_ip
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index bbd010ea6d..74420317c7 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -13,6 +13,12 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end
end
+ class YoutubeFavoritesRedirector
+ def self.call(params, request)
+ "http://www.youtube.com/watch?v=#{params[:youtube_id]}"
+ end
+ end
+
stub_controllers do |routes|
Routes = routes
Routes.draw do
@@ -54,6 +60,13 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
match 'account/login', :to => redirect("/login")
match 'secure', :to => redirect("/secure/login")
+ match 'mobile', :to => redirect(:subdomain => 'mobile')
+ match 'documentation', :to => redirect(:domain => 'example-documentation.com', :path => '')
+ match 'new_documentation', :to => redirect(:path => '/documentation/new')
+ match 'super_new_documentation', :to => redirect(:host => 'super-docs.com')
+
+ match 'youtube_favorites/:youtube_id/:name', :to => redirect(YoutubeFavoritesRedirector)
+
constraints(lambda { |req| true }) do
match 'account/overview'
end
@@ -667,6 +680,41 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end
end
+ def test_redirect_hash_with_subdomain
+ with_test_routes do
+ get '/mobile'
+ verify_redirect 'http://mobile.example.com/mobile'
+ end
+ end
+
+ def test_redirect_hash_with_domain_and_path
+ with_test_routes do
+ get '/documentation'
+ verify_redirect 'http://www.example-documentation.com'
+ end
+ end
+
+ def test_redirect_hash_with_path
+ with_test_routes do
+ get '/new_documentation'
+ verify_redirect 'http://www.example.com/documentation/new'
+ end
+ end
+
+ def test_redirect_hash_with_host
+ with_test_routes do
+ get '/super_new_documentation?section=top'
+ verify_redirect 'http://super-docs.com/super_new_documentation?section=top'
+ end
+ end
+
+ def test_redirect_class
+ with_test_routes do
+ get '/youtube_favorites/oHg5SJYRHA0/rick-rolld'
+ verify_redirect 'http://www.youtube.com/watch?v=oHg5SJYRHA0'
+ end
+ end
+
def test_openid
with_test_routes do
get '/openid/login'