changed the way test dumping a request works
John Napiorkowski [Thu, 17 Oct 2013 01:16:50 +0000 (20:16 -0500)]
lib/Catalyst/Request.pm
t/lib/TestApp/View/Dump.pm
t/lib/TestApp/View/Dump/Request.pm
t/lib/TestFromPSGI.pm [new file with mode: 0644]
t/lib/TestFromPSGI/Controller/Root.pm [new file with mode: 0644]

index ab87e17..de9f15f 100644 (file)
@@ -14,7 +14,7 @@ use namespace::clean -except => 'meta';
 
 with 'MooseX::Emulate::Class::Accessor::Fast';
 
-has env => (is => 'ro', writer => '_set_env');
+has env => (is => 'ro', writer => '_set_env', predicate => 'has_env');
 # XXX Deprecated crap here - warn?
 has action => (is => 'rw');
 # XXX: Deprecated in docs ages ago (2006), deprecated with warning in 5.8000 due
@@ -224,6 +224,18 @@ has _uploadtmp => (
 sub prepare_body {
     my ( $self ) = @_;
 
+    #warn "XXX ${\$self->_uploadtmp}" if $self->_has_uploadtmp;
+
+    if(my $plack_body = $self->env->{'plack.request.http.body'}) {
+      warn "wtF" x 100;
+        $self->_body($plack_body);
+        $self->_body->cleanup(1); # Make extra sure!
+        $self->_body->tmpdir( $self->_uploadtmp )
+          if $self->_has_uploadtmp;
+    } else {
+
+    }
+
     if ( my $length = $self->_read_length ) {
         unless ( $self->_body ) {
             my $type = $self->header('Content-Type');
@@ -251,6 +263,59 @@ sub prepare_body {
     }
 }
 
+sub prepare_bodyXXX {
+    my ( $self ) = @_;
+    if(my $plack_body = $self->env->{'plack.request.http.body'}) {
+    
+
+    } else {
+
+    }
+
+    die "XXX ${\$self->_uploadtmp}" x1000; $self->_has_uploadtmp;
+
+    if ( my $length = $self->_read_length ) {
+        unless ( $self->_body ) {
+            
+            ## If something plack middle already ready the body, just use
+            ## that.
+
+            my $body;
+            if(my $plack_body = $self->env->{'plack.request.http.body'}) {
+                $body = $plack_body;
+            } else {
+                my $type = $self->header('Content-Type');
+                $body = HTTP::Body->new($type, $length);
+
+                ## Play nice with Plak Middleware that looks for a body
+                $self->env->{'plack.request.http.body'} = $body;
+                $self->_body($body);
+
+                $body->cleanup(1); # Make extra sure!
+                $body->tmpdir( $self->_uploadtmp )
+                  if $self->_has_uploadtmp;
+            }
+        }
+
+        # Check for definedness as you could read '0'
+        while ( defined ( my $buffer = $self->read() ) ) {
+            $self->prepare_body_chunk($buffer);
+        }
+
+        # paranoia against wrong Content-Length header
+        my $remaining = $length - $self->_read_position;
+        if ( $remaining > 0 ) {
+            Catalyst::Exception->throw(
+                "Wrong Content-Length value: $length" );
+        }
+    }
+    else {
+        # Defined but will cause all body code to be skipped
+        $self->_body(0);
+    }
+}
+
+
 sub prepare_body_chunk {
     my ( $self, $chunk ) = @_;
 
@@ -313,7 +378,7 @@ has _body => (
 #             and provide a custom reader..
 sub body {
   my $self = shift;
-  $self->prepare_body unless ! $self->_has_body;
+  $self->prepare_body unless $self->_has_body;
   croak 'body is a reader' if scalar @_;
   return blessed $self->_body ? $self->_body->body : $self->_body;
 }
index dff41e8..016df81 100644 (file)
@@ -39,6 +39,8 @@ sub process {
     # Remove context from reference if needed
     my $context = delete $reference->{_context};
 
+    my $env = delete $reference->{env};
+
     if (my $log = $reference->{_log}) {
         $log->clear_psgi if ($log->can('psgienv'));
     }
@@ -62,6 +64,8 @@ sub process {
             $reference->{_body} = $body;
         }
 
+        if($env) { $reference->{env} = $env }
+
         return 1;
     }
 
index 97926ec..991eeca 100644 (file)
@@ -6,7 +6,7 @@ use base qw[TestApp::View::Dump];
 sub process {
     my ( $self, $c ) = @_;
     my $r = $c->request;
-    local $r->{env};
+    #local $r->{env};
     return $self->SUPER::process( $c, $r );
 }
 
diff --git a/t/lib/TestFromPSGI.pm b/t/lib/TestFromPSGI.pm
new file mode 100644 (file)
index 0000000..9b16aec
--- /dev/null
@@ -0,0 +1,38 @@
+package TestFromPSGI;
+
+use Moose;
+use Plack::Middleware::Static;
+use Plack::App::File;
+use Catalyst;
+
+extends 'Catalyst';
+
+my $static = Plack::Middleware::Static->new(
+  path => qr{^/static/}, root => TestMiddleware->path_to('share'));
+
+__PACKAGE__->config(
+  'Controller::Root', { namespace => '' },
+  'psgi_middleware', [
+    'Head',
+    $static,
+    'Static', { path => qr{^/static2/}, root => TestMiddleware->path_to('share') },
+    'Runtime',
+    '+TestMiddleware::Custom', { path => qr{^/static3/}, root => TestMiddleware->path_to('share') },
+    sub {
+      my $app = shift;
+      return sub {
+        my $env = shift;
+        if($env->{PATH_INFO} =~m/forced/) {
+          Plack::App::File->new(file=>TestMiddleware->path_to(qw/share static forced.txt/))
+            ->call($env);
+        } else {
+          return $app->($env);
+        }
+      },
+    },
+
+  ],
+);
+
+__PACKAGE__->setup;
+
diff --git a/t/lib/TestFromPSGI/Controller/Root.pm b/t/lib/TestFromPSGI/Controller/Root.pm
new file mode 100644 (file)
index 0000000..7c116a5
--- /dev/null
@@ -0,0 +1,13 @@
+package TestMiddleware::Controller::Root;
+
+use Moose;
+use MooseX::MethodAttributes;
+
+extends 'Catalyst::Controller';
+
+sub default : Path { }
+sub welcome : Path(welcome) {
+  pop->res->body('Welcome to Catalyst');
+}
+
+__PACKAGE__->meta->make_immutable;