from psgi res tests working
John Napiorkowski [Fri, 18 Oct 2013 20:38:17 +0000 (15:38 -0500)]
lib/Catalyst/Response.pm
t/lib/TestFromPSGI.pm
t/lib/TestFromPSGI/Controller/Root.pm
t/more-psgi-compat.t [new file with mode: 0644]

index f5ec77c..c44b23c 100644 (file)
@@ -117,7 +117,7 @@ sub from_psgi_response {
     if(ref $psgi_res eq 'ARRAY') {
         my ($status, $headers, $body) = @$psgi_res;
         $self->status($status);
-        $self->headers($headers);
+        $self->headers(HTTP::Headers->new(@$headers));
         if(ref $body eq 'ARRAY') {
           $self->body(join '', grep defined, @$body);
         } else {
@@ -125,9 +125,10 @@ sub from_psgi_response {
         }
     } elsif(ref $psgi_res eq 'CODE') {
         $psgi_res->(sub {
-            my ($status, $headers, $maybe_body) = @_;
+            my $response = shift;
+            my ($status, $headers, $maybe_body) = @$response;
             $self->status($status);
-            $self->headers($headers);
+            $self->headers(HTTP::Headers->new(@$headers));
             if($maybe_body) {
                 if(ref $maybe_body eq 'ARRAY') {
                   $self->body(join '', grep defined, @$maybe_body);
@@ -137,8 +138,8 @@ sub from_psgi_response {
             } else {
                 return $self->write_fh;
             }
-        });        
-    } else {
+        });  
+     } else {
         die "You can't set a Catalyst response from that, expect a valid PSGI response";
     }
 }
index 9b16aec..def2832 100644 (file)
@@ -1,37 +1,10 @@
 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;
index 7c116a5..f3a0477 100644 (file)
@@ -1,13 +1,56 @@
-package TestMiddleware::Controller::Root;
+package TestFromPSGI::Controller::Root;
 
 use Moose;
 use MooseX::MethodAttributes;
 
 extends 'Catalyst::Controller';
 
-sub default : Path { }
-sub welcome : Path(welcome) {
-  pop->res->body('Welcome to Catalyst');
+sub test_psgi_keys :Local Args(1) {
+  my ($self, $c, $key) = @_;
+  $c->res->body($c->req->env->{$key});
+}
+
+sub from_psgi_array : Local {
+  my ($self, $c) = @_;
+  my $res = sub {
+    my ($env) = @_;
+    return [200, ['Content-Type'=>'text/plain'],
+      [qw/hello world today/]];
+  }->($c->req->env);
+
+  $c->res->from_psgi_response($res);
+}
+
+sub from_psgi_code : Local {
+  my ($self, $c) = @_;
+
+  my $res = sub {
+    my ($env) = @_;
+    return sub {
+      my $responder = shift;
+      return $responder->([200, ['Content-Type'=>'text/plain'],
+        [qw/hello world today2/]]);
+    };
+  }->($c->req->env);
+
+  $c->res->from_psgi_response($res);
+}
+
+sub from_psgi_code_itr : Local {
+  my ($self, $c) = @_;
+  my $res = sub {
+    my ($env) = @_;
+    return sub {
+      my $responder = shift;
+      my $writer = $responder->([200, ['Content-Type'=>'text/plain']]);
+      $writer->write('hello');
+      $writer->write('world');
+      $writer->write('today3');
+      $writer->close;
+    };
+  }->($c->req->env);
+
+  $c->res->from_psgi_response($res);
 }
 
 __PACKAGE__->meta->make_immutable;
diff --git a/t/more-psgi-compat.t b/t/more-psgi-compat.t
new file mode 100644 (file)
index 0000000..71dc283
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use FindBin;
+use Test::More;
+use HTTP::Request::Common;
+
+use lib "$FindBin::Bin/lib";
+use Catalyst::Test 'TestFromPSGI';
+
+{
+  ok my $response = request GET '/from_psgi_array',
+    'got welcome from a catalyst controller';
+
+  is $response->content, 'helloworldtoday',
+    'expected content body /from_psgi_array';
+}
+
+{
+  ok my $response = request GET '/from_psgi_code',
+    'got welcome from a catalyst controller';
+
+  is $response->content, 'helloworldtoday2',
+    'expected content body /from_psgi_code';
+}
+
+{
+  ok my $response = request GET '/from_psgi_code_itr',
+    'got welcome from a catalyst controller';
+
+  is $response->content, 'helloworldtoday3',
+    'expected content body /from_psgi_code_itr';
+}
+
+done_testing;