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 {
}
} 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);
} else {
return $self->write_fh;
}
- });
- } else {
+ });
+ } else {
die "You can't set a Catalyst response from that, expect a valid PSGI response";
}
}
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;
-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;
--- /dev/null
+#!/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;