From: Brian Phillips Date: Tue, 13 Mar 2012 18:50:47 +0000 (-0500) Subject: prevent body_params from being reset on each access X-Git-Tag: 5.90012~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=d003ff83ac25ab0af3988de66867f73af54ff631 prevent body_params from being reset on each access --- diff --git a/lib/Catalyst/Request.pm b/lib/Catalyst/Request.pm index 9c43407..3d0c03a 100644 --- a/lib/Catalyst/Request.pm +++ b/lib/Catalyst/Request.pm @@ -127,7 +127,7 @@ has body_parameters => ( is => 'rw', required => 1, lazy => 1, - default => sub { {} }, + builder => 'prepare_body_parameters', ); has uploads => ( @@ -152,8 +152,6 @@ has parameters => ( sub prepare_parameters { my ( $self ) = @_; - - $self->prepare_body; my $parameters = {}; my $body_parameters = $self->body_parameters; my $query_parameters = $self->query_parameters; @@ -175,12 +173,6 @@ sub prepare_parameters { $parameters; } -before body_parameters => sub { - my ($self) = @_; - $self->prepare_body; - $self->prepare_body_parameters; -}; - has _uploadtmp => ( is => 'ro', predicate => '_has_uploadtmp', @@ -225,9 +217,10 @@ sub prepare_body_chunk { sub prepare_body_parameters { my ( $self ) = @_; + $self->prepare_body if ! $self->_has_body; return unless $self->_body; - $self->{body_parameters} = $self->_body->param; # FIXME!! Recursion here. + return $self->_body->param; } sub prepare_connection { @@ -277,7 +270,7 @@ has _body => ( # and provide a custom reader.. sub body { my $self = shift; - $self->prepare_body(); + $self->prepare_body unless ! $self->_has_body; croak 'body is a reader' if scalar @_; return blessed $self->_body ? $self->_body->body : $self->_body; } diff --git a/t/lib/TestApp/Controller/BodyParams.pm b/t/lib/TestApp/Controller/BodyParams.pm new file mode 100644 index 0000000..5732211 --- /dev/null +++ b/t/lib/TestApp/Controller/BodyParams.pm @@ -0,0 +1,13 @@ +package TestApp::Controller::BodyParams; + +use strict; +use base 'Catalyst::Controller'; + +sub default : Private { + my ( $self, $c ) = @_; + $c->req->body_params({override => 'that'}); + $c->res->output($c->req->body_params->{override}); + $c->res->status(200); +} + +1; diff --git a/t/live_catalyst_test.t b/t/live_catalyst_test.t index 9fb299f..e7f8df9 100644 --- a/t/live_catalyst_test.t +++ b/t/live_catalyst_test.t @@ -5,6 +5,7 @@ use FindBin; use lib "$FindBin::Bin/lib"; use Catalyst::Test 'TestApp', {default_host => 'default.com'}; use Catalyst::Request; +use HTTP::Request::Common; use Test::More; @@ -44,5 +45,10 @@ my $req = '/dump/request'; is( $creq->uri->host, $opts{host}, 'target host is mutable via options hashref' ); } +{ + my $response = request( POST( '/bodyparams', { override => 'this' } ) )->content; + is($response, 'that', 'body param overridden'); +} + done_testing;