patch to make prepare_parameters not be both a builder and a preparer
John Napiorkowski [Thu, 16 Aug 2012 18:59:41 +0000 (14:59 -0400)]
lib/Catalyst/Engine.pm
lib/Catalyst/Request.pm
t/aggregate/live_engine_request_prepare_parameters.t [new file with mode: 0755]
t/lib/TestApp/Controller/Dump.pm

index c414896..65ef3fe 100644 (file)
@@ -394,14 +394,17 @@ sub prepare_body_parameters {
 
 =head2 $self->prepare_parameters($c)
 
-sets up parameters from query and post parameters.
+Sets up parameters from query and post parameters.
+If parameters have already been set up will clear
+existing parameters and set up again.
 
 =cut
 
 sub prepare_parameters {
     my ( $self, $c ) = @_;
 
-    $c->request->parameters;
+    $c->request->_clear_parameters;
+    return $c->request->parameters;
 }
 
 =head2 $self->prepare_path($c)
index d2c1c7f..b8d05b4 100644 (file)
@@ -141,7 +141,8 @@ has uploads => (
 has parameters => (
     is => 'rw',
     lazy => 1,
-    builder => 'prepare_parameters',
+    builder => '_build_parameters',
+    clearer => '_clear_parameters',
 );
 
 # TODO:
@@ -154,6 +155,14 @@ has parameters => (
 
 sub prepare_parameters {
     my ( $self ) = @_;
+    $self->_clear_parameters;
+    return $self->parameters;
+}
+
+
+
+sub _build_parameters {
+    my ( $self ) = @_;
     my $parameters = {};
     my $body_parameters = $self->body_parameters;
     my $query_parameters = $self->query_parameters;
@@ -870,7 +879,8 @@ request method, hostname requested etc.
 Ensures that the body has been parsed, then builds the parameters, which are
 combined from those in the request and those in the body.
 
-This method is the builder for the 'parameters' attribute.
+If parameters have already been set will clear the parameters and build them again.
+
 
 =head2 meta
 
diff --git a/t/aggregate/live_engine_request_prepare_parameters.t b/t/aggregate/live_engine_request_prepare_parameters.t
new file mode 100755 (executable)
index 0000000..e933fb5
--- /dev/null
@@ -0,0 +1,39 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/../lib";
+
+use Test::More tests => 8;
+use Catalyst::Test 'TestApp';
+
+use Catalyst::Request;
+use HTTP::Headers;
+use HTTP::Request::Common;
+
+{
+    my $creq;
+
+    my $parameters = { 'a' => [qw(A b C d E f G)], };
+
+    my $query = join( '&', map { 'a=' . $_ } @{ $parameters->{a} } );
+
+    ok( my $response = request("http://localhost/dump/prepare_parameters?$query"),
+        'Request' );
+    ok( $response->is_success, 'Response Successful 2xx' );
+    is( $response->content_type, 'text/plain', 'Response Content-Type' );
+    like(
+        $response->content,
+        qr/^bless\( .* 'Catalyst::Request' \)$/s,
+        'Content is a serialized Catalyst::Request'
+    );
+    ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
+    isa_ok( $creq, 'Catalyst::Request' );
+    is( $creq->method, 'GET', 'Catalyst::Request method' );
+    is_deeply( $creq->parameters, $parameters,
+        'Catalyst::Request parameters' );
+}
+
+
index 84ebe8d..0864822 100644 (file)
@@ -27,6 +27,20 @@ sub request : Action Relative {
     $c->forward('TestApp::View::Dump::Request');
 }
 
+sub prepare_parameters : Action Relative {
+    my ( $self, $c ) = @_;
+
+    die 'Must pass in parameters' unless keys %{$c->req->parameters};
+
+    $c->req->parameters( {} );
+    die 'parameters are not empty' if keys %{$c->req->parameters};
+
+    # Now reset and reload
+    $c->prepare_parameters;
+    die 'Parameters were not reset' unless keys %{$c->req->parameters};
+
+    $c->forward('TestApp::View::Dump::Request');
+}
 sub response : Action Relative {
     my ( $self, $c ) = @_;
     $c->forward('TestApp::View::Dump::Response');