Skip to content
This repository
Browse code

adds URL to the body generated by the redirect macro in the routes ma…

…pper as per the RFC, extracts common test pattern into a test macro, adds a test to cover the :status option
  • Loading branch information...
commit 4434e407e93001409605e5f02650b591a0cede32 1 parent d87c57b
Xavier Noria authored August 09, 2010
5  actionpack/lib/action_dispatch/routing/mapper.rb
... ...
@@ -1,3 +1,4 @@
  1
+require 'erb'
1 2
 require 'active_support/core_ext/hash/except'
2 3
 require 'active_support/core_ext/object/blank'
3 4
 
@@ -277,7 +278,6 @@ def redirect(*args, &block)
277 278
           path      = args.shift || block
278 279
           path_proc = path.is_a?(Proc) ? path : proc { |params| path % params }
279 280
           status    = options[:status] || 301
280  
-          body      = 'Moved Permanently'
281 281
 
282 282
           lambda do |env|
283 283
             req = Request.new(env)
@@ -290,11 +290,14 @@ def redirect(*args, &block)
290 290
             uri.host   ||= req.host
291 291
             uri.port   ||= req.port unless req.port == 80
292 292
 
  293
+            body = %(<html><body>You are being <a href="#{ERB::Util.h(uri.to_s)}">redirected</a>.</body></html>)
  294
+
293 295
             headers = {
294 296
               'Location' => uri.to_s,
295 297
               'Content-Type' => 'text/html',
296 298
               'Content-Length' => body.length.to_s
297 299
             }
  300
+
298 301
             [ status, headers, [body] ]
299 302
           end
300 303
         end
55  actionpack/test/dispatch/routing_test.rb
... ...
@@ -1,3 +1,4 @@
  1
+require 'erb'
1 2
 require 'abstract_unit'
2 3
 require 'controller/fake_controllers'
3 4
 
@@ -56,7 +57,7 @@ def self.matches?(request)
56 57
       match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" }
57 58
       match 'account/proc_req' => redirect {|params, req| "/#{req.method}" }
58 59
 
59  
-      match 'account/google' => redirect('http://www.google.com/')
  60
+      match 'account/google' => redirect('http://www.google.com/', :status => 302)
60 61
 
61 62
       match 'openid/login', :via => [:get, :post], :to => "openid#login"
62 63
 
@@ -501,9 +502,7 @@ def test_login
501 502
   def test_login_redirect
502 503
     with_test_routes do
503 504
       get '/account/login'
504  
-      assert_equal 301, @response.status
505  
-      assert_equal 'http://www.example.com/login', @response.headers['Location']
506  
-      assert_equal 'Moved Permanently', @response.body
  505
+      verify_redirect 'http://www.example.com/login'
507 506
     end
508 507
   end
509 508
 
@@ -511,18 +510,14 @@ def test_logout_redirect_without_to
511 510
     with_test_routes do
512 511
       assert_equal '/account/logout', logout_redirect_path
513 512
       get '/account/logout'
514  
-      assert_equal 301, @response.status
515  
-      assert_equal 'http://www.example.com/logout', @response.headers['Location']
516  
-      assert_equal 'Moved Permanently', @response.body
  513
+      verify_redirect 'http://www.example.com/logout'
517 514
     end
518 515
   end
519 516
 
520 517
   def test_namespace_redirect
521 518
     with_test_routes do
522 519
       get '/private'
523  
-      assert_equal 301, @response.status
524  
-      assert_equal 'http://www.example.com/private/index', @response.headers['Location']
525  
-      assert_equal 'Moved Permanently', @response.body
  520
+      verify_redirect 'http://www.example.com/private/index'
526 521
     end
527 522
   end
528 523
 
@@ -586,27 +581,21 @@ def test_member_on_resource
586 581
   def test_redirect_modulo
587 582
     with_test_routes do
588 583
       get '/account/modulo/name'
589  
-      assert_equal 301, @response.status
590  
-      assert_equal 'http://www.example.com/names', @response.headers['Location']
591  
-      assert_equal 'Moved Permanently', @response.body
  584
+      verify_redirect 'http://www.example.com/names'
592 585
     end
593 586
   end
594 587
 
595 588
   def test_redirect_proc
596 589
     with_test_routes do
597 590
       get '/account/proc/person'
598  
-      assert_equal 301, @response.status
599  
-      assert_equal 'http://www.example.com/people', @response.headers['Location']
600  
-      assert_equal 'Moved Permanently', @response.body
  591
+      verify_redirect 'http://www.example.com/people'
601 592
     end
602 593
   end
603 594
 
604 595
   def test_redirect_proc_with_request
605 596
     with_test_routes do
606 597
       get '/account/proc_req'
607  
-      assert_equal 301, @response.status
608  
-      assert_equal 'http://www.example.com/GET', @response.headers['Location']
609  
-      assert_equal 'Moved Permanently', @response.body
  598
+      verify_redirect 'http://www.example.com/GET'
610 599
     end
611 600
   end
612 601
 
@@ -1203,12 +1192,10 @@ def test_convention_with_explicit_end
1203 1192
     end
1204 1193
   end
1205 1194
 
1206  
-  def test_redirect_with_complete_url
  1195
+  def test_redirect_with_complete_url_and_status
1207 1196
     with_test_routes do
1208 1197
       get '/account/google'
1209  
-      assert_equal 301, @response.status
1210  
-      assert_equal 'http://www.google.com/', @response.headers['Location']
1211  
-      assert_equal 'Moved Permanently', @response.body
  1198
+      verify_redirect 'http://www.google.com/', 302
1212 1199
     end
1213 1200
   end
1214 1201
 
@@ -1216,9 +1203,7 @@ def test_redirect_with_port
1216 1203
     previous_host, self.host = self.host, 'www.example.com:3000'
1217 1204
     with_test_routes do
1218 1205
       get '/account/login'
1219  
-      assert_equal 301, @response.status
1220  
-      assert_equal 'http://www.example.com:3000/login', @response.headers['Location']
1221  
-      assert_equal 'Moved Permanently', @response.body
  1206
+      verify_redirect 'http://www.example.com:3000/login'
1222 1207
     end
1223 1208
   ensure
1224 1209
     self.host = previous_host
@@ -1899,8 +1884,18 @@ def test_only_scope_should_override_parent_except_scope
1899 1884
     end
1900 1885
   end
1901 1886
 
1902  
-  private
1903  
-    def with_test_routes
1904  
-      yield
1905  
-    end
  1887
+private
  1888
+  def with_test_routes
  1889
+    yield
  1890
+  end
  1891
+
  1892
+  def verify_redirect(url, status=301)
  1893
+    assert_equal status, @response.status
  1894
+    assert_equal url, @response.headers['Location']
  1895
+    assert_equal expected_redirect_body(url), @response.body
  1896
+  end
  1897
+
  1898
+  def expected_redirect_body(url)
  1899
+    %(<html><body>You are being <a href="#{ERB::Util.h(url)}">redirected</a>.</body></html>)
  1900
+  end
1906 1901
 end

0 notes on commit 4434e40

Please sign in to comment.
Something went wrong with that request. Please try again.