[Catalyst-commits] r10854 - in trunk/Catalyst-Plugin-Session-State-URI: lib/Catalyst/Plugin/Session/State t t/lib t/lib/RewritingTestApp/Controller

autarch at dev.catalyst.perl.org autarch at dev.catalyst.perl.org
Sat Jul 11 18:40:16 GMT 2009


Author: autarch
Date: 2009-07-11 18:40:16 +0000 (Sat, 11 Jul 2009)
New Revision: 10854

Modified:
   trunk/Catalyst-Plugin-Session-State-URI/lib/Catalyst/Plugin/Session/State/URI.pm
   trunk/Catalyst-Plugin-Session-State-URI/t/lib/RewritingTestApp.pm
   trunk/Catalyst-Plugin-Session-State-URI/t/lib/RewritingTestApp/Controller/URI.pm
   trunk/Catalyst-Plugin-Session-State-URI/t/lib/TestApp.pm
   trunk/Catalyst-Plugin-Session-State-URI/t/live_rewrite.t
Log:
Separate the rewrite config item into rewrite_redirect and rewrite_body.

The old rewrite parameter still works, but is no longer documented.

This checkin also includes tests and doc updates.

Modified: trunk/Catalyst-Plugin-Session-State-URI/lib/Catalyst/Plugin/Session/State/URI.pm
===================================================================
--- trunk/Catalyst-Plugin-Session-State-URI/lib/Catalyst/Plugin/Session/State/URI.pm	2009-07-11 18:39:23 UTC (rev 10853)
+++ trunk/Catalyst-Plugin-Session-State-URI/lib/Catalyst/Plugin/Session/State/URI.pm	2009-07-11 18:40:16 UTC (rev 10854)
@@ -39,12 +39,21 @@
     $c->maybe::next::method(@_);
 
     my %defaults = (
-        rewrite              => 1,
+        rewrite_redirect     => 1,
+        rewrite_body         => 1,
         no_rewrite_if_cookie => 1,
     );
 
     my $config = $c->config->{session} ||= {};
 
+
+    if ( delete $config->{rewrite} ) {
+        $config->{rewrite_redirect} = 1
+            unless exists $config->{rewrite_redirect};
+        $config->{rewrite_body} = 1
+            unless exists $config->{rewrite_body};
+    }
+
     foreach my $key ( keys %defaults ) {
         $config->{$key} = $defaults{$key}
             unless exists $config->{$key};
@@ -178,7 +187,8 @@
 sub session_should_rewrite {
     my $c = shift;
 
-    return unless $c->config->{session}{rewrite};
+    return unless $c->config->{session}{rewrite_redirect}
+        ||  $c->config->{session}{rewrite_body};
 
     if ( $c->isa("Catalyst::Plugin::Session::State::Cookie")
         and $c->config->{session}{no_rewrite_if_cookie}
@@ -210,11 +220,13 @@
 
 sub session_should_rewrite_body {
     my $c = shift;
+    return unless $c->config->{session}{rewrite_body};
     return $c->session_should_rewrite_type;
 }
 
 sub session_should_rewrite_redirect {
     my $c = shift;
+    return unless $c->config->{session}{rewrite_redirect};
     ($c->response->status || 0) =~ /^\s*3\d\d\s*$/;
 }
 
@@ -361,9 +373,11 @@
 This method is consulted by C<finalize>. The body will be rewritten only if it
 returns a true value.
 
-This method will B<not> return true unless
-C<< $c->config->{session}{rewrite} >> is true (the default). To globally
-disable rewriting simply set this parameter to false.
+This method will B<not> return true unless C<<
+$c->config->{session}{rewrite_body} >> or C<<
+$c->config->{session}{rewrite_redirect} >> is true. Both of these
+parameters default to true. To globally disable rewriting simply set
+these parameters to false.
 
 If C<< $c->config->{session}{no_rewrite_if_cookie} >> is true (the default),
 L<Catalyst::Plugin::Session::State::Cookie> is also in use, and the user agent
@@ -371,7 +385,8 @@
 
 =item session_should_rewrite_body
 
-This method just calls C<session_should_rewrite_type>.
+This method checks C<< $c->config->{session}{rewrite_body} >>
+first. If this is true, it then calls C<session_should_rewrite_type>.
 
 =item session_should_rewrite_type
 
@@ -386,7 +401,9 @@
 
 Whether or not to rewrite the C<Location> header of the response.
 
-If the status code is a number in the 3xx range then this returns true.
+This method checks C<< $c->config->{session}{rewrite_redirect} >>
+first. If this is true, it then checks if the status code is a number
+in the 3xx range.
 
 =item session_should_rewrite_uri $uri_text
 
@@ -464,9 +481,7 @@
 
 =item finalize
 
-If C<session_should_rewrite> returns a true value, L<HTML::TokePaser::Simple> is used to
-traverse the body to replace all URLs which get true returned by C<session_should_rewrite_uri> so that they contain
-the session ID.
+Rewrite a redirect or the body HTML as appropriate.
 
 =item delete_session_id
 

Modified: trunk/Catalyst-Plugin-Session-State-URI/t/lib/RewritingTestApp/Controller/URI.pm
===================================================================
--- trunk/Catalyst-Plugin-Session-State-URI/t/lib/RewritingTestApp/Controller/URI.pm	2009-07-11 18:39:23 UTC (rev 10853)
+++ trunk/Catalyst-Plugin-Session-State-URI/t/lib/RewritingTestApp/Controller/URI.pm	2009-07-11 18:40:16 UTC (rev 10854)
@@ -12,6 +12,9 @@
 sub first_request : Global {
     my ( $self, $c ) = @_;
 
+    $c->config->{session}{rewrite_body} = 1;
+    $c->config->{session}{rewrite_redirect} = 1;
+
     ok( !$c->session_is_valid, "no session" );
 
     $c->session->{counter} = 1;
@@ -22,6 +25,9 @@
 sub second_request : Global {
     my ( $self, $c ) = @_;
 
+    $c->config->{session}{rewrite_body} = 1;
+    $c->config->{session}{rewrite_redirect} = 1;
+
     ok( $c->session_is_valid, "session exists" );
 
     is( ++$c->session->{counter}, 2, "counter is OK" );
@@ -33,6 +39,9 @@
 sub third_request : Global {
     my ( $self, $c ) = @_;
 
+    $c->config->{session}{rewrite_body} = 1;
+    $c->config->{session}{rewrite_redirect} = 1;
+
     ok( $c->session_is_valid, "session exists" );
 
     is( ++$c->session->{counter}, 3, "counter is OK" );
@@ -67,12 +76,73 @@
 sub text_request : Global {
     my ( $self, $c ) = @_;
 
+    $c->config->{session}{rewrite_body} = 1;
+    $c->config->{session}{rewrite_redirect} = 1;
+
     $c->session->{counter} = 42;
     $c->forward("add_some_html");
 
     $c->response->content_type("text/plain") if $c->request->param("plain");
 }
 
+sub redirect : Global {
+    my ( $self, $c ) = @_;
+
+    $c->config->{session}{rewrite_body} = 1;
+    $c->config->{session}{rewrite_redirect} = 1;
+
+    $c->session->{counter} = 43;
+
+    $c->response->status(302);
+    $c->response->location( '/whatever' );
+}
+
+sub only_rewrite_redirect : Global {
+    my ( $self, $c ) = @_;
+
+    $c->config->{session}{rewrite_body} = 0;
+    $c->config->{session}{rewrite_redirect} = 1;
+
+    $c->session->{counter} = 43;
+
+    $c->response->status(302);
+    $c->response->location( '/whatever' );
+}
+
+sub dont_rewrite_redirect : Global {
+    my ( $self, $c ) = @_;
+
+    $c->config->{session}{rewrite_body} = 0;
+    $c->config->{session}{rewrite_redirect} = 0;
+
+    $c->session->{counter} = 43;
+
+    $c->response->status(302);
+    $c->response->location( '/whatever' );
+}
+
+sub only_rewrite_body : Global {
+    my ( $self, $c ) = @_;
+
+    $c->config->{session}{rewrite_body} = 1;
+    $c->config->{session}{rewrite_redirect} = 0;
+
+    $c->session->{counter} = 43;
+
+    $c->forward("add_some_html");
+}
+
+sub dont_rewrite_body : Global {
+    my ( $self, $c ) = @_;
+
+    $c->config->{session}{rewrite_body} = 0;
+    $c->config->{session}{rewrite_redirect} = 0;
+
+    $c->session->{counter} = 43;
+
+    $c->forward("add_some_html");
+}
+
 __PACKAGE__;
 
 __END__

Modified: trunk/Catalyst-Plugin-Session-State-URI/t/lib/RewritingTestApp.pm
===================================================================
--- trunk/Catalyst-Plugin-Session-State-URI/t/lib/RewritingTestApp.pm	2009-07-11 18:39:23 UTC (rev 10853)
+++ trunk/Catalyst-Plugin-Session-State-URI/t/lib/RewritingTestApp.pm	2009-07-11 18:40:16 UTC (rev 10854)
@@ -12,7 +12,8 @@
     name => __PACKAGE__,
     home => "/",
     session => {
-        rewrite => 1,
+        rewrite_body => 1,
+        rewrite_redirect => 1,
         no_rewrite_if_cookie => 1, # FIXME better name
         rewrite_types => [qw{ text/html }],
     }

Modified: trunk/Catalyst-Plugin-Session-State-URI/t/lib/TestApp.pm
===================================================================
--- trunk/Catalyst-Plugin-Session-State-URI/t/lib/TestApp.pm	2009-07-11 18:39:23 UTC (rev 10853)
+++ trunk/Catalyst-Plugin-Session-State-URI/t/lib/TestApp.pm	2009-07-11 18:40:16 UTC (rev 10854)
@@ -12,7 +12,15 @@
 
 our $VERSION = '0.01';
 
-TestApp->config( name => 'TestApp', root => '/some/dir', session => { param => 'sid', rewrite => 0 } );
+__PACKAGE__->config(
+    name => __PACKAGE__,
+    root => '/some/dir',
+    session => {
+        param => 'sid',
+        rewrite_body => 0,
+        rewrite_redirect => 0,
+    }
+);
 
 TestApp->setup(qw/Session::State::URI/);
 

Modified: trunk/Catalyst-Plugin-Session-State-URI/t/live_rewrite.t
===================================================================
--- trunk/Catalyst-Plugin-Session-State-URI/t/live_rewrite.t	2009-07-11 18:39:23 UTC (rev 10853)
+++ trunk/Catalyst-Plugin-Session-State-URI/t/live_rewrite.t	2009-07-11 18:40:16 UTC (rev 10854)
@@ -16,7 +16,7 @@
     eval { require Test::WWW::Mechanize::Catalyst }
         or plan skip_all => "Test::WWW::Mechanize::Catalyst is required for this test";
 
-    plan tests => 32;
+    plan tests => 46;
 }
 
 use Test::WWW::Mechanize::Catalyst "RewritingTestApp";
@@ -49,6 +49,49 @@
 
     $m->content_like( qr/counter: 3\b/, "counter at 3" );
 
+    no warnings 'redefine', 'once';
+    local *Test::WWW::Mechanize::redirect_ok = sub { 0 };
+
+    $m->get( "http://localhost/redirect", "got redirect" );
+    my $resp = $m->response;
+    is( $resp->code, 302, "got a 302 response" );
+
+    unless ($use_cookies) {
+        like( $resp->header("Location"), qr{/-/.+$},
+              "Location header has session id with redirect" );
+    }
+
+    $m->get( "http://localhost/only_rewrite_redirect", "got redirect" );
+
+    $resp = $m->response;
+    is( $resp->code, 302, "got a 302 response" );
+
+    unless ($use_cookies) {
+        like( $resp->header("Location"), qr{/-/.+$},
+              "Location header has session id with redirect and rewrite_redirect true" );
+    }
+
+    $m->get( "http://localhost/dont_rewrite_redirect", "got redirect" );
+
+    $resp = $m->response;
+    is( $resp->code, 302, "got a 302 response" );
+
+    unlike( $resp->header("Location"), qr{/-/.+$},
+            "Location header does not have session id with redirect and rewrite_redirect false" );
+
+    unless ($use_cookies) {
+        $m->get_ok( "http://localhost/only_rewrite_body", "get only_rewrite_body" );
+
+        my $third = $m->find_link( text => "third" );
+        like( $third->URI, qr{/-/},
+              "body uri was rewritten" );
+
+        $m->get_ok( "http://localhost/dont_rewrite_body", "get dont_rewrite_body" );
+
+        $third = $m->find_link( text => "third" );
+        unlike( $third->URI, qr{/-/},
+                "body uri was not rewritten" );
+    }
 }
 
 {




More information about the Catalyst-commits mailing list