Navigation Menu

Skip to content

Commit

Permalink
cleaned up tests that change the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 28, 2011
1 parent c9b64d0 commit 43609be
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 252 deletions.
96 changes: 48 additions & 48 deletions t/mojo/asset.t
Expand Up @@ -56,52 +56,52 @@ is $mem->contains('cdef'), 1, '"cdef" at position 1';
is $mem->contains('db'), -1, 'does not contain "db"';

# File asset range support (ab[cdefghi]jk)
my $backup = $ENV{MOJO_CHUNK_SIZE} || '';
$ENV{MOJO_CHUNK_SIZE} = 1024;
$file = Mojo::Asset::File->new(start_range => 2, end_range => 8);
$file->add_chunk('abcdefghijk');
is $file->contains(''), 0, 'empty string at position 0';
is $file->contains('cdefghi'), 0, '"cdefghi" at position 0';
is $file->contains('fghi'), 3, '"fghi" at position 3';
is $file->contains('f'), 3, '"f" at position 3';
is $file->contains('hi'), 5, '"hi" at position 5';
is $file->contains('db'), -1, 'does not contain "db"';
my $chunk = $file->get_chunk(0);
is $chunk, 'cdefghi', 'chunk from position 0';
$chunk = $file->get_chunk(1);
is $chunk, 'defghi', 'chunk from position 1';
$chunk = $file->get_chunk(5);
is $chunk, 'hi', 'chunk from position 5';
$ENV{MOJO_CHUNK_SIZE} = 1;
$chunk = $file->get_chunk(0);
is $chunk, 'c', 'chunk from position 0 with size 1';
$chunk = $file->get_chunk(5);
is $chunk, 'h', 'chunk from position 5 with size 1';
$ENV{MOJO_CHUNK_SIZE} = $backup;
{
local $ENV{MOJO_CHUNK_SIZE} = 1024;
$file = Mojo::Asset::File->new(start_range => 2, end_range => 8);
$file->add_chunk('abcdefghijk');
is $file->contains(''), 0, 'empty string at position 0';
is $file->contains('cdefghi'), 0, '"cdefghi" at position 0';
is $file->contains('fghi'), 3, '"fghi" at position 3';
is $file->contains('f'), 3, '"f" at position 3';
is $file->contains('hi'), 5, '"hi" at position 5';
is $file->contains('db'), -1, 'does not contain "db"';
my $chunk = $file->get_chunk(0);
is $chunk, 'cdefghi', 'chunk from position 0';
$chunk = $file->get_chunk(1);
is $chunk, 'defghi', 'chunk from position 1';
$chunk = $file->get_chunk(5);
is $chunk, 'hi', 'chunk from position 5';
$ENV{MOJO_CHUNK_SIZE} = 1;
$chunk = $file->get_chunk(0);
is $chunk, 'c', 'chunk from position 0 with size 1';
$chunk = $file->get_chunk(5);
is $chunk, 'h', 'chunk from position 5 with size 1';
}

# Memory asset range support (ab[cdefghi]jk)
$backup = $ENV{MOJO_CHUNK_SIZE} || '';
$ENV{MOJO_CHUNK_SIZE} = 1024;
$mem = Mojo::Asset::Memory->new(start_range => 2, end_range => 8);
$mem->add_chunk('abcdefghijk');
is $mem->contains(''), 0, 'empty string at position 0';
is $mem->contains('cdefghi'), 0, '"cdefghi" at position 0';
is $mem->contains('fghi'), 3, '"fghi" at position 3';
is $mem->contains('f'), 3, '"f" at position 3';
is $mem->contains('hi'), 5, '"hi" at position 5';
is $mem->contains('db'), -1, 'does not contain "db"';
$chunk = $mem->get_chunk(0);
is $chunk, 'cdefghi', 'chunk from position 0';
$chunk = $mem->get_chunk(1);
is $chunk, 'defghi', 'chunk from position 1';
$chunk = $mem->get_chunk(5);
is $chunk, 'hi', 'chunk from position 5';
$ENV{MOJO_CHUNK_SIZE} = 1;
$chunk = $mem->get_chunk(0);
is $chunk, 'c', 'chunk from position 0 with size 1';
$chunk = $mem->get_chunk(5);
is $chunk, 'h', 'chunk from position 5 with size 1';
$ENV{MOJO_CHUNK_SIZE} = $backup;
{
local $ENV{MOJO_CHUNK_SIZE} = 1024;
$mem = Mojo::Asset::Memory->new(start_range => 2, end_range => 8);
$mem->add_chunk('abcdefghijk');
is $mem->contains(''), 0, 'empty string at position 0';
is $mem->contains('cdefghi'), 0, '"cdefghi" at position 0';
is $mem->contains('fghi'), 3, '"fghi" at position 3';
is $mem->contains('f'), 3, '"f" at position 3';
is $mem->contains('hi'), 5, '"hi" at position 5';
is $mem->contains('db'), -1, 'does not contain "db"';
my $chunk = $mem->get_chunk(0);
is $chunk, 'cdefghi', 'chunk from position 0';
$chunk = $mem->get_chunk(1);
is $chunk, 'defghi', 'chunk from position 1';
$chunk = $mem->get_chunk(5);
is $chunk, 'hi', 'chunk from position 5';
$ENV{MOJO_CHUNK_SIZE} = 1;
$chunk = $mem->get_chunk(0);
is $chunk, 'c', 'chunk from position 0 with size 1';
$chunk = $mem->get_chunk(5);
is $chunk, 'h', 'chunk from position 5 with size 1';
}

# Move memory asset to file
$mem = Mojo::Asset::Memory->new;
Expand Down Expand Up @@ -134,10 +134,10 @@ $asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';

# Temporary directory
$backup = $ENV{MOJO_TMPDIR} || '';
$ENV{MOJO_TMPDIR} = '/does/not/exist';
is(Mojo::Asset::File->new->tmpdir, '/does/not/exist', 'right value');
$ENV{MOJO_TMPDIR} = $backup;
{
local $ENV{MOJO_TMPDIR} = '/does/not/exist';
is(Mojo::Asset::File->new->tmpdir, '/does/not/exist', 'right value');
}

# Temporary file without cleanup
$file = Mojo::Asset::File->new(cleanup => 0)->add_chunk('test');
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/bytestream.t
Expand Up @@ -376,10 +376,10 @@ is $stream->split('/')->map(sub { shift->quote })->join(', '),
my $buffer = '';
open my $handle, '>', \$buffer;
b('te', 'st')->say($handle);
my $backup = *STDOUT;
my $stdout = *STDOUT;
*STDOUT = $handle;
b(1, 2, 3)->say;
*STDOUT = $backup;
*STDOUT = $stdout;
is $buffer, "test\n123\n", 'right output';

# Nested bytestreams
Expand Down
16 changes: 8 additions & 8 deletions t/mojo/home.t
Expand Up @@ -11,18 +11,18 @@ use List::Util 'first';
use_ok 'Mojo::Home';

# ENV detection
my $backup = $ENV{MOJO_HOME} || '';
$ENV{MOJO_HOME} = '.';
my $home = Mojo::Home->new->detect;
is_deeply [split /\\|\//, File::Spec->canonpath($home->to_string)],
[split /\\|\//, File::Spec->canonpath(realpath cwd())],
'right path detected';
$ENV{MOJO_HOME} = $backup;
{
local $ENV{MOJO_HOME} = '.';
my $home = Mojo::Home->new->detect;
is_deeply [split /\\|\//, File::Spec->canonpath($home->to_string)],
[split /\\|\//, File::Spec->canonpath(realpath cwd())],
'right path detected';
}

# Class detection
my $original =
File::Spec->catdir(File::Spec->splitdir($FindBin::Bin), '..', '..');
$home = Mojo::Home->new->detect;
my $home = Mojo::Home->new->detect;
my $target = realpath $original;
is_deeply [split /\\|\//, $target], [split /\\|\//, $home],
'right path detected';
Expand Down
176 changes: 88 additions & 88 deletions t/mojo/request.t
Expand Up @@ -306,44 +306,44 @@ is $req->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $req->headers->content_length, undef, 'no "Content-Length" value';

# Parse full HTTP 1.0 request (file storage)
my $backup = $ENV{MOJO_MAX_MEMORY_SIZE} || '';
$ENV{MOJO_MAX_MEMORY_SIZE} = 12;
$req = Mojo::Message::Request->new;
my ($upgrade, $size);
$req->content->asset->on(
upgrade => sub {
my ($mem, $file) = @_;
$upgrade = $file->is_file;
$size = $file->size;
}
);
is $req->content->progress, 0, 'right progress';
$req->parse('GET /foo/bar/baz.html?fo');
is $req->content->progress, 0, 'right progress';
$req->parse("o=13#23 HTTP/1.0\x0d\x0aContent");
$req->parse('-Type: text/');
is $req->content->progress, 0, 'right progress';
$req->parse("plain\x0d\x0aContent-Length: 27\x0d\x0a\x0d\x0aHell");
is $req->content->progress, 4, 'right progress';
ok !$req->content->asset->is_file, 'stored in memory';
ok !$upgrade, 'upgrade event has not been emitted';
$req->parse("o World!\n");
ok $upgrade, 'upgrade event has been emitted';
is $size, 0, 'file was empty when upgrade event got emitted';
is $req->content->progress, 13, 'right progress';
ok $req->content->asset->is_file, 'stored in file';
$req->parse("1234\nlalalala\n");
is $req->content->progress, 27, 'right progress';
ok $req->content->asset->is_file, 'stored in file';
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
is $req->version, '1.0', 'right version';
ok $req->at_least_version('1.0'), 'at least version 1.0';
ok !$req->at_least_version('1.2'), 'not version 1.2';
is $req->url, '/foo/bar/baz.html?foo=13#23', 'right URL';
is $req->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $req->headers->content_length, 27, 'right "Content-Length" value';
$ENV{MOJO_MAX_MEMORY_SIZE} = $backup;
{
local $ENV{MOJO_MAX_MEMORY_SIZE} = 12;
$req = Mojo::Message::Request->new;
my ($upgrade, $size);
$req->content->asset->on(
upgrade => sub {
my ($mem, $file) = @_;
$upgrade = $file->is_file;
$size = $file->size;
}
);
is $req->content->progress, 0, 'right progress';
$req->parse('GET /foo/bar/baz.html?fo');
is $req->content->progress, 0, 'right progress';
$req->parse("o=13#23 HTTP/1.0\x0d\x0aContent");
$req->parse('-Type: text/');
is $req->content->progress, 0, 'right progress';
$req->parse("plain\x0d\x0aContent-Length: 27\x0d\x0a\x0d\x0aHell");
is $req->content->progress, 4, 'right progress';
ok !$req->content->asset->is_file, 'stored in memory';
ok !$upgrade, 'upgrade event has not been emitted';
$req->parse("o World!\n");
ok $upgrade, 'upgrade event has been emitted';
is $size, 0, 'file was empty when upgrade event got emitted';
is $req->content->progress, 13, 'right progress';
ok $req->content->asset->is_file, 'stored in file';
$req->parse("1234\nlalalala\n");
is $req->content->progress, 27, 'right progress';
ok $req->content->asset->is_file, 'stored in file';
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
is $req->version, '1.0', 'right version';
ok $req->at_least_version('1.0'), 'at least version 1.0';
ok !$req->at_least_version('1.2'), 'not version 1.2';
is $req->url, '/foo/bar/baz.html?foo=13#23', 'right URL';
is $req->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $req->headers->content_length, 27, 'right "Content-Length" value';
}

# Parse HTTP 1.0 start line and headers, no body (missing Content-Length)
$req = Mojo::Message::Request->new;
Expand All @@ -360,64 +360,64 @@ is $req->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $req->headers->content_length, undef, 'no "Content-Length" value';

# Parse HTTP 1.0 start line (with line size limit)
$req = Mojo::Message::Request->new;
$backup = $ENV{MOJO_MAX_LINE_SIZE} || '';
$ENV{MOJO_MAX_LINE_SIZE} = 5;
$req->parse('GET /foo/bar/baz.html HTTP/1');
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum line size exceeded.', 'right error');
is(($req->error)[1], 431, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
$ENV{MOJO_MAX_LINE_SIZE} = $backup;
{
$req = Mojo::Message::Request->new;
local $ENV{MOJO_MAX_LINE_SIZE} = 5;
$req->parse('GET /foo/bar/baz.html HTTP/1');
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum line size exceeded.', 'right error');
is(($req->error)[1], 431, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
}

# Parse HTTP 1.0 start line and headers (with line size limit)
$req = Mojo::Message::Request->new;
$backup = $ENV{MOJO_MAX_LINE_SIZE} || '';
$ENV{MOJO_MAX_LINE_SIZE} = 20;
$req->parse("GET / HTTP/1.0\x0d\x0a");
$req->parse("Content-Type: text/plain\x0d\x0a");
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum line size exceeded.', 'right error');
is(($req->error)[1], 431, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
$ENV{MOJO_MAX_LINE_SIZE} = $backup;
{
$req = Mojo::Message::Request->new;
local $ENV{MOJO_MAX_LINE_SIZE} = 20;
$req->parse("GET / HTTP/1.0\x0d\x0a");
$req->parse("Content-Type: text/plain\x0d\x0a");
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum line size exceeded.', 'right error');
is(($req->error)[1], 431, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
}

# Parse HTTP 1.0 start line (with message size limit)
$req = Mojo::Message::Request->new;
$backup = $ENV{MOJO_MAX_MESSAGE_SIZE} || '';
$ENV{MOJO_MAX_MESSAGE_SIZE} = 5;
$req->parse('GET /foo/bar/baz.html HTTP/1');
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum message size exceeded.', 'right error');
is(($req->error)[1], 413, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
$ENV{MOJO_MAX_MESSAGE_SIZE} = $backup;
{
$req = Mojo::Message::Request->new;
local $ENV{MOJO_MAX_MESSAGE_SIZE} = 5;
$req->parse('GET /foo/bar/baz.html HTTP/1');
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum message size exceeded.', 'right error');
is(($req->error)[1], 413, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
}

# Parse HTTP 1.0 start line and headers (with message size limit)
$req = Mojo::Message::Request->new;
$backup = $ENV{MOJO_MAX_MESSAGE_SIZE} || '';
$ENV{MOJO_MAX_MESSAGE_SIZE} = 20;
$req->parse("GET / HTTP/1.0\x0d\x0a");
$req->parse("Content-Type: text/plain\x0d\x0a");
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum message size exceeded.', 'right error');
is(($req->error)[1], 413, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
$ENV{MOJO_MAX_MESSAGE_SIZE} = $backup;
{
$req = Mojo::Message::Request->new;
local $ENV{MOJO_MAX_MESSAGE_SIZE} = 20;
$req->parse("GET / HTTP/1.0\x0d\x0a");
$req->parse("Content-Type: text/plain\x0d\x0a");
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum message size exceeded.', 'right error');
is(($req->error)[1], 413, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
}

# Parse HTTP 1.0 start line, headers and body (with message size limit)
$req = Mojo::Message::Request->new;
$backup = $ENV{MOJO_MAX_MESSAGE_SIZE} || '';
$ENV{MOJO_MAX_MESSAGE_SIZE} = 50;
$req->parse("GET / HTTP/1.0\x0d\x0a");
$req->parse("Content-Length: 24\x0d\x0a\x0d\x0a");
$req->parse('Hello World!');
$req->parse('Hello World!');
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum message size exceeded.', 'right error');
is(($req->error)[1], 413, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
$ENV{MOJO_MAX_MESSAGE_SIZE} = $backup;
{
$req = Mojo::Message::Request->new;
local $ENV{MOJO_MAX_MESSAGE_SIZE} = 50;
$req->parse("GET / HTTP/1.0\x0d\x0a");
$req->parse("Content-Length: 24\x0d\x0a\x0d\x0a");
$req->parse('Hello World!');
$req->parse('Hello World!');
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum message size exceeded.', 'right error');
is(($req->error)[1], 413, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
}

# Parse full HTTP 1.0 request
$req = Mojo::Message::Request->new;
Expand Down
10 changes: 5 additions & 5 deletions t/mojo/server.t
Expand Up @@ -21,11 +21,11 @@ my $app = $server->new(app_class => 'Mojo::TestServerViaApp')->app;
isa_ok $app, 'Mojo::TestServerViaApp', 'right object';

# Test setting the class name through the environment
my $backup = $ENV{MOJO_APP} || '';
$ENV{MOJO_APP} = 'Mojo::TestServerViaEnv';
$app = $server->new->app;
isa_ok $app, 'Mojo::TestServerViaEnv', 'right object';
$ENV{MOJO_APP} = $backup;
{
local $ENV{MOJO_APP} = 'Mojo::TestServerViaEnv';
$app = $server->new->app;
isa_ok $app, 'Mojo::TestServerViaEnv', 'right object';
}

# Test the default
$app = $server->new->app;
Expand Down

0 comments on commit 43609be

Please sign in to comment.