Skip to content

Commit

Permalink
improved Mojo::Log tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 31, 2012
1 parent b4eb64a commit 8260910
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
27 changes: 12 additions & 15 deletions lib/Mojo/Server/CGI.pm
Expand Up @@ -16,14 +16,13 @@ sub run {
my $req = $tx->req->parse(\%ENV);

# Store connection information
$tx->remote_address($ENV{REMOTE_ADDR});
$tx->local_port($ENV{SERVER_PORT});
$tx->remote_address($ENV{REMOTE_ADDR});

# Request body
binmode STDIN;
until ($req->is_finished) {
my $read = STDIN->read(my $buffer, CHUNK_SIZE, 0);
last unless $read;
last unless my $read = STDIN->read(my $buffer, CHUNK_SIZE, 0);
$req->parse($buffer);
}

Expand All @@ -35,21 +34,19 @@ sub run {
binmode STDOUT;
my $res = $tx->res;
my $offset = 0;
if ($self->nph) {
while (1) {
my $chunk = $res->get_start_line_chunk($offset);
while ($self->nph) {
my $chunk = $res->get_start_line_chunk($offset);

# No start line yet, try again
sleep 1 and next unless defined $chunk;
# No start line yet, try again
sleep 1 and next unless defined $chunk;

# End of start line
last unless length $chunk;
# End of start line
last unless length $chunk;

# Start line
return unless STDOUT->opened;
print STDOUT $chunk;
$offset += length $chunk;
}
# Start line
return unless STDOUT->opened;
print STDOUT $chunk;
$offset += length $chunk;
}

# Response headers
Expand Down
15 changes: 5 additions & 10 deletions lib/Mojo/Server/PSGI.pm
Expand Up @@ -15,32 +15,27 @@ sub run {
my $req = $tx->req->parse($env);

# Store connection information
$tx->remote_address($env->{REMOTE_ADDR});
$tx->local_port($env->{SERVER_PORT});
$tx->remote_address($env->{REMOTE_ADDR});

# Request body
my $len = $env->{CONTENT_LENGTH};
until ($req->is_finished) {
my $chunk = ($len && $len < CHUNK_SIZE) ? $len : CHUNK_SIZE;
my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0);
last unless $read;
last unless my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0);
$req->parse($buffer);
$len -= $read;
last if $len <= 0;
last if ($len -= $read) <= 0;
}

# Handle
$self->emit(request => $tx);

# Response headers
my $res = $tx->res;
$res->fix_headers;
my $res = $tx->res->fix_headers;
my $headers = $res->content->headers;
my @headers;
for my $name (@{$headers->names}) {
for my $values ($headers->header($name)) {
push @headers, $name => $_ for @$values;
}
push @headers, $name => $_ for map {@$_} $headers->header($name);
}

# PSGI response
Expand Down
18 changes: 16 additions & 2 deletions t/mojo/log.t
@@ -1,14 +1,28 @@
use Mojo::Base -strict;

use Test::More tests => 44;
use Test::More tests => 45;

# "Don't let Krusty's death get you down, boy.
# People die all the time, just like that.
# Why, you could wake up dead tomorrow! Well, good night."
use File::Spec::Functions 'catdir';
use File::Temp;
use Mojo::Asset::File;
use Mojo::Log;

# Logging to file
my $dir = File::Temp::tempdir(CLEANUP => 1);
my $path = catdir $dir, 'test.log';
my $log = Mojo::Log->new(level => 'debug', path => $path);
$log->debug('Just works.');
$log = Mojo::Log->new;
like(
Mojo::Asset::File->new(path => $path)->slurp,
qr/^\[.*\] \[debug\] Just works\.\n$/,
'right content'
);

# Formatting
my $log = Mojo::Log->new;
like $log->format(debug => 'Test 123.'), qr/^\[.*\] \[debug\] Test 123\.\n$/,
'right format';
like $log->format(qw/debug Test 123./), qr/^\[.*\] \[debug\] Test\n123\.\n$/,
Expand Down

0 comments on commit 8260910

Please sign in to comment.