-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#!perl | ||
use warnings; | ||
use strict; | ||
use Test::More tests => 21; | ||
use lib 't'; use Utils; | ||
use AnyEvent::HTTP; | ||
|
||
BEGIN { use_ok('Feersum') }; | ||
|
||
my ($socket,$port) = get_listen_socket(); | ||
ok $socket, "made listen socket"; | ||
|
||
my $evh = Feersum->new(); | ||
{ | ||
no warnings 'redefine'; | ||
*Feersum::DIED = sub { | ||
my $err = shift; | ||
fail "Died during request handler: $err"; | ||
}; | ||
} | ||
$evh->use_socket($socket); | ||
|
||
{ | ||
package Message; | ||
my $n = 0; | ||
sub new { return bless {}, 'Message' } | ||
sub to_json { ++$n; return qq({"message":"O hai $n"}) } | ||
} | ||
|
||
sub wait_for_new_message { | ||
my $cb = shift; | ||
my $t; $t = AE::timer rand(0.5),0,sub { | ||
$cb->(Message->new()); | ||
undef $t; # cancel circular-ref | ||
}; | ||
return; | ||
} | ||
|
||
# from the PSGI::FAQ | ||
my $APP = <<'EOAPP'; | ||
my $app = sub { | ||
my $env = shift; | ||
unless ($env->{'psgi.streaming'}) { | ||
die "This application needs psgi.streaming support"; | ||
} | ||
Test::More::pass "called app"; | ||
return sub { | ||
Test::More::pass "called streamer"; | ||
my $respond = shift; | ||
wait_for_new_message(sub { | ||
my $message = shift; | ||
my $body = [ $message->to_json ]; | ||
Test::More::pass "sending response"; | ||
undef $env; | ||
$respond->([200, ['Content-Type', 'application/json'], $body]); | ||
Test::More::pass "sent response"; | ||
}); | ||
}; | ||
}; | ||
EOAPP | ||
|
||
my $app = eval $APP; | ||
ok $app, 'got an app' || diag $@; | ||
$evh->psgi_request_handler($app); | ||
|
||
returning_body: { | ||
my $cv = AE::cv; | ||
|
||
$cv->begin; | ||
http_request GET => "http://localhost:$port/", timeout => 300, sub { | ||
my ($body, $headers) = @_; | ||
is $headers->{'Status'}, 200, "Response OK"; | ||
is $headers->{'content-type'}, 'application/json', "... is JSON"; | ||
is $body, q({"message":"O hai 1"}), '... correct body'; | ||
$cv->end; | ||
}; | ||
|
||
$cv->recv; | ||
pass "all done app 1"; | ||
} | ||
|
||
my $APP2 = <<'EOAPP'; | ||
my $app2 = sub { | ||
my $env = shift; | ||
unless ($env->{'psgi.streaming'}) { | ||
die "This application needs psgi.streaming support"; | ||
} | ||
Test::More::pass "called app2"; | ||
return sub { | ||
Test::More::pass "called streamer2"; | ||
my $respond = shift; | ||
wait_for_new_message(sub { | ||
my $message = shift; | ||
Test::More::pass "sending response2"; | ||
my $w = $respond->([200, ['Content-Type', 'application/json']]); | ||
Test::More::pass "started response2"; | ||
$w->write($message->to_json); | ||
Test::More::pass "done response2"; | ||
$w->close; | ||
undef $env; | ||
}); | ||
}; | ||
}; | ||
EOAPP | ||
|
||
my $app2 = eval $APP2; | ||
ok $app2, 'got app 2' || diag $@; | ||
$evh->psgi_request_handler($app2); | ||
|
||
using_writer: { | ||
my $cv = AE::cv; | ||
|
||
$cv->begin; | ||
http_request GET => "http://localhost:$port/", timeout => 300, sub { | ||
my ($body, $headers) = @_; | ||
is $headers->{'Status'}, 200, "Response OK"; | ||
is $headers->{'content-type'}, 'application/json', "... is JSON"; | ||
ok( | ||
($body eq "15\r\n".q({"message":"O hai 2"})."\r\n0\r\n\r\n" | ||
or | ||
$body eq q({"message":"O hai 2"})), | ||
'... correct body (maybe AE::HTTP doesn\'t decode chunked)'); | ||
$cv->end; | ||
}; | ||
|
||
$cv->recv; | ||
pass "all done app 2"; | ||
} |