Skip to content

Commit

Permalink
improved PSGI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 5, 2011
1 parent efe9ff9 commit c1a5c22
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
23 changes: 9 additions & 14 deletions lib/Mojo/Server/PSGI.pm
Expand Up @@ -33,11 +33,9 @@ sub run {
# Handle
$self->on_request->($self, $tx);

# Fix headers
# Response headers
my $res = $tx->res;
$res->fix_headers;

# Response headers
my $headers = $res->content->headers;
my @headers;
for my $name (@{$headers->names}) {
Expand All @@ -46,32 +44,29 @@ sub run {
}
}

# Response body
my $body = Mojo::Server::PSGI::_Handle->new(res => $res);

# Finish transaction
$tx->on_finish->($tx);

# PSGI response
my $code = $res->code || 404;
return [$code, \@headers, $body];
return [$res->code || 404,
\@headers, Mojo::Server::PSGI::_IO->new(tx => $tx)];
}

# "Wow! Homer must have got one of those robot cars!
# *Car crashes in background*
# Yeah, one of those AMERICAN robot cars."
package Mojo::Server::PSGI::_Handle;
package Mojo::Server::PSGI::_IO;
use Mojo::Base -base;

sub close { }
sub close {
my $tx = shift->{tx};
$tx->on_finish->($tx);
}

sub getline {
my $self = shift;

# Blocking read
my $offset = $self->{offset} //= 0;
while (1) {
my $chunk = $self->{res}->get_body_chunk($offset);
my $chunk = $self->{tx}->res->get_body_chunk($offset);

# No content yet, try again
unless (defined $chunk) {
Expand Down
23 changes: 9 additions & 14 deletions t/mojo/psgi.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 18;
use Test::More tests => 21;

use Mojo::JSON;

Expand Down Expand Up @@ -40,13 +40,11 @@ is $headers{'Content-Length'}, 43, 'right "Content-Length" value';
is $headers{'Content-Type'}, 'application/json', 'right "Content-Type" value';
my $params = '';
while (defined(my $chunk = $res->[2]->getline)) { $params .= $chunk }
is $ENV{MOJO_HELLO}, undef, 'on_finish not yet called';
$res->[2]->close;
is delete $ENV{MOJO_HELLO}, 'world', 'on_finish called';
$params = Mojo::JSON->new->decode($params);
is_deeply $params,
{
bar => 'baz',
hello => 'world',
lalala => 23
},
is_deeply $params, {bar => 'baz', hello => 'world', lalala => 23},
'right structure';

# Command
Expand Down Expand Up @@ -79,15 +77,12 @@ is $headers{'Content-Length'}, 43, 'right "Content-Length" value';
is $headers{'Content-Type'}, 'application/json', 'right "Content-Type" value';
$params = '';
while (defined(my $chunk = $res->[2]->getline)) { $params .= $chunk }
is $ENV{MOJO_HELLO}, undef, 'on_finish not yet called';
$res->[2]->close;
is delete $ENV{MOJO_HELLO}, 'world', 'on_finish called';
$params = Mojo::JSON->new->decode($params);
is_deeply $params,
{
bar => 'baz',
world => 'hello',
lalala => 23
},
is_deeply $params, {bar => 'baz', world => 'hello', lalala => 23},
'right structure';
is $ENV{MOJO_HELLO}, 'world', 'on_finish callback';

# Cookies
$env = {
Expand Down

0 comments on commit c1a5c22

Please sign in to comment.