sub call {
my ($self, $env) = @_;
- $self->_dispatch($env, $self->app);
+ my $res = $self->_dispatch($env, $self->app);
+ return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE';
+ return $res;
}
sub _dispatch {
my ($self, $first, $result, $match, $env) = @_;
if (ref($first) eq 'ARRAY') {
- return $self->_unpack_array_match($first);
+ return $first;
}
elsif (blessed($first) && $first->isa('Plack::Middleware')) {
return $self->_uplevel_middleware($first, $result);
return;
}
-sub _unpack_array_match {
- my ($self, $match) = @_;
- return $match->[0] if @{$match} == 1 and ref($match->[0]) eq 'CODE';
- return $match;
-}
-
sub _uplevel_middleware {
my ($self, $match, $results) = @_;
die "Multiple results but first one is a middleware ($match)"
}
);
- my $get = run_request( GET => 'http://localhost/' );
+ eval { run_request( GET => 'http://localhost/' ) };
- cmp_ok $get->code, '==', 999,
- "if a route returns an arrayref with a single sub in it, then WD redispatches to that sub";
+ like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
+"if a route returns an arrayref with a single sub in it, then that sub is returned as a response by WD, causing HTTP::Message::PSGI to choke";
}
sub array_with_no_sub {
--- /dev/null
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More (
+ eval { require HTTP::Request::AsCGI }
+ ? 'no_plan'
+ : (skip_all => 'No HTTP::Request::AsCGI')
+);
+
+use HTTP::Request::Common qw(GET POST);
+
+my $app = StreamTest->new;
+
+ok run_request( $app, GET 'http://localhost/' )->is_success;
+is run_request( $app, GET 'http://localhost/' )->content, "foo";
+
+done_testing;
+
+sub run_request {
+ my ( $app, $request ) = @_;
+ my $c = HTTP::Request::AsCGI->new( $request )->setup;
+ $app->run;
+ $c->restore;
+ return $c->response;
+}
+
+{
+
+ package StreamTest;
+ use Web::Simple;
+
+ sub dispatch_request {
+
+ sub (GET) {
+ [
+ sub {
+ my $respond = shift;
+ my $writer = $respond->( [ 200, [ "Content-type" => "text/plain" ] ] );
+ $writer->write( 'f' );
+ $writer->write( 'o' );
+ $writer->write( 'o' );
+ }
+ ];
+ },;
+ }
+}