allow the redirect method to accept a URI style object to reduce boilerplate for...
John Napiorkowski [Tue, 25 Nov 2014 21:13:10 +0000 (15:13 -0600)]
Changes
lib/Catalyst/Response.pm
t/utf_incoming.t

diff --git a/Changes b/Changes
index 231624d..0165821 100644 (file)
--- a/Changes
+++ b/Changes
@@ -18,6 +18,8 @@
     path or pathparts in your actions.  query and body parameter keys are now also
     subject to utf8 decoding (or as specificed via the encoding configuration value).
   - lots of UTF8 changes.  Again we think this is now more correct but please test.
+  - Allow $c->res->redirect($url) to accept $url as an object that does ->as_string
+    which I think will ease a common case (and common bug) and added documentation.
 
 5.90077 - 2014-11-18
   - We store the PSGI $env in Catalyst::Engine for backcompat reasons.  Changed
index 709f0ad..7368a66 100644 (file)
@@ -351,6 +351,12 @@ qualified (= C<http://...>, etc.) or that starts with a slash
 thing and is not a standard behaviour. You may opt to use uri_for() or
 uri_for_action() instead.
 
+B<Note:> If $url is an object that does ->as_string (such as L<URI>, which is
+what you get from ->uri_for) we automatically call that to stringify.  This
+should ease the common case usage
+
+    return $c->res->redirect( $c->uri_for(...));
+
 =cut
 
 sub redirect {
@@ -360,6 +366,10 @@ sub redirect {
         my $location = shift;
         my $status   = shift || 302;
 
+        if(blessed($location) && $location->can('as_string')) {
+            $location = $location->as_string;
+        }
+
         $self->location($location);
         $self->status($status);
     }
index 096ba69..920a064 100644 (file)
@@ -172,6 +172,11 @@ use Encode 2.21 'decode_utf8', 'encode_utf8';
   is decode_utf8($res->content), "$url", 'correct body'; #should do nothing
   is $res->content, "$url", 'correct body';
   is $res->content_length, 90, 'correct length';
+
+  # Test to make sure redirect can now take an object (sorry don't have a better place for it
+  # but wanted test coverage.
+  my $location = $c->res->redirect( $c->uri_for($c->controller('Root')->action_for('uri_for')) );
+  ok !ref $location; 
 }
 
 done_testing;