Skip to content

Commit

Permalink
improved portability of some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 21, 2014
1 parent 34c7b4f commit 34e5df1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 60 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

5.63 2014-11-21
- Improved portability of some tests.
- Fixed a few multipart form handling bugs.

5.62 2014-11-18
Expand Down
33 changes: 10 additions & 23 deletions t/mojo/daemon.t
Expand Up @@ -260,28 +260,15 @@ $daemon = Mojo::Server::Daemon->new(
silent => 1
);
is scalar @{$daemon->acceptors}, 0, 'no active acceptors';
$daemon->ioloop->max_connections(500);
$daemon->start;
is $daemon->ioloop->max_connections, 500, 'right number';
is scalar @{$daemon->acceptors}, 1, 'one active acceptor';
is $daemon->app->moniker, 'mojolicious', 'right moniker';
$port = Mojo::IOLoop->acceptor($daemon->acceptors->[0])->handle->sockport;
$tx = $ua->get("http://127.0.0.1:$port/throttle1" => {Connection => 'close'});
ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Whatever!', 'right content';
$daemon->stop;
is scalar @{$daemon->acceptors}, 0, 'no active acceptors';
$tx = $ua->inactivity_timeout(0.5)
->get("http://127.0.0.1:$port/throttle2" => {Connection => 'close'});
ok !$tx->success, 'not successful';
is $tx->error->{message}, 'Inactivity timeout', 'right error';
$daemon->max_clients(600)->start;
is $daemon->ioloop->max_connections, 600, 'right number';
$tx = $ua->inactivity_timeout(10)
->get("http://127.0.0.1:$port/throttle3" => {Connection => 'close'});
ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Whatever!', 'right content';
is scalar @{$daemon->start->acceptors}, 1, 'one active acceptor';
$id = $daemon->acceptors->[0];
ok !!Mojo::IOLoop->acceptor($id), 'acceptor has been added';
is scalar @{$daemon->stop->acceptors}, 0, 'no active acceptors';
ok !Mojo::IOLoop->acceptor($id), 'acceptor has been removed';
is scalar @{$daemon->start->acceptors}, 1, 'one active acceptor';
$id = $daemon->acceptors->[0];
ok !!Mojo::IOLoop->acceptor($id), 'acceptor has been added';
undef $daemon;
ok !Mojo::IOLoop->acceptor($id), 'acceptor has been removed';

done_testing();
15 changes: 8 additions & 7 deletions t/mojo/template.t
Expand Up @@ -16,8 +16,9 @@ package main;
use Mojo::Base -strict;

use Test::More;
use File::Spec::Functions qw(catfile splitdir);
use FindBin;
use Cwd 'abs_path';
use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use Mojo::Template;

# Capture helper
Expand Down Expand Up @@ -1066,13 +1067,13 @@ EOF

# File
$mt = Mojo::Template->new;
my $file = catfile(splitdir($FindBin::Bin), qw(templates test.mt));
my $file = abs_path catfile(dirname(__FILE__), 'templates', 'test.mt');
$output = $mt->render_file($file, 3);
like $output, qr/23\nHello World!/, 'file';

# Exception in file
$mt = Mojo::Template->new;
$file = catfile(splitdir($FindBin::Bin), qw(templates exception.mt));
$file = abs_path catfile(dirname(__FILE__), 'templates', 'exception.mt');
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/exception\.mt line 2/, 'message contains filename';
Expand All @@ -1099,8 +1100,8 @@ is $output->lines_after->[0][1], '123', 'right line';
like "$output", qr/foo\.mt from DATA section line 2/, 'right result';

# Exception with UTF-8 context
$mt = Mojo::Template->new;
$file = catfile(splitdir($FindBin::Bin), qw(templates utf8_exception.mt));
$mt = Mojo::Template->new;
$file = abs_path catfile(dirname(__FILE__), 'templates', 'utf8_exception.mt');
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
is $output->lines_before->[0][1], '', 'right line';
Expand All @@ -1119,7 +1120,7 @@ is $output->lines_after->[0], undef, 'no lines after';

# Different encodings
$mt = Mojo::Template->new(encoding => 'shift_jis');
$file = catfile(splitdir($FindBin::Bin), qw(templates utf8_exception.mt));
$file = abs_path catfile(dirname(__FILE__), 'templates', 'utf8_exception.mt');
ok !eval { $mt->render_file($file) }, 'file not rendered';
like $@, qr/invalid encoding/, 'right error';

Expand Down
7 changes: 2 additions & 5 deletions t/mojo/transactor.t
@@ -1,8 +1,6 @@
use Mojo::Base -strict;

use Test::More;
use File::Spec::Functions 'catdir';
use FindBin;
use Mojo::Asset::File;
use Mojo::Asset::Memory;
use Mojo::Transaction::WebSocket;
Expand Down Expand Up @@ -203,9 +201,8 @@ is_deeply $tx->req->every_param('a'), [1, 2, 3], 'right values';
is_deeply [$tx->req->param('b')], [4], 'right values';

# Multipart form with real file and custom header
my $path = catdir $FindBin::Bin, 'transactor.t';
$tx = $t->tx(POST => 'http://example.com/foo' => form =>
{mytext => {file => $path, DNT => 1}});
{mytext => {file => __FILE__, DNT => 1}});
is $tx->req->url->to_abs, 'http://example.com/foo', 'right URL';
is $tx->req->method, 'POST', 'right method';
is $tx->req->headers->content_type, 'multipart/form-data',
Expand All @@ -224,7 +221,7 @@ is $tx->req->content->parts->[1], undef, 'no more parts';
$tx
= $t->tx(POST => 'http://example.com/foo' =>
{'Content-Type' => 'multipart/mojo-form'} => form =>
{mytext => {file => Mojo::Asset::File->new(path => $path)}});
{mytext => {file => Mojo::Asset::File->new(path => __FILE__)}});
is $tx->req->url->to_abs, 'http://example.com/foo', 'right URL';
is $tx->req->method, 'POST', 'right method';
is $tx->req->headers->content_type, 'multipart/mojo-form',
Expand Down
30 changes: 5 additions & 25 deletions t/mojolicious/longpolling_lite_app.t
Expand Up @@ -214,9 +214,8 @@ get '/too_long' => sub {
my $c = shift;
$c->res->code(200);
$c->res->headers->content_type('text/plain');
$c->res->headers->content_length(12);
$c->write('how');
Mojo::IOLoop->timer(5 => sub { $c->write('dy plain!') });
$c->res->headers->content_length(17);
$c->write('Waiting forever!');
};

my $steps;
Expand Down Expand Up @@ -424,33 +423,14 @@ ok !$stash->{writing}, 'finish event timing is right';
ok $stash->{destroyed}, 'controller has been destroyed';

# Request timeout
$tx = $t->ua->request_timeout(0.5)->build_tx(GET => '/too_long');
$buffer = '';
$tx->res->content->unsubscribe('read')->on(
read => sub {
my ($content, $chunk) = @_;
$buffer .= $chunk;
}
);
$t->ua->start($tx);
is $tx->res->code, 200, 'right status';
$tx = $t->ua->request_timeout(0.5)->get('/too_long');
is $tx->error->{message}, 'Request timeout', 'right error';
is $buffer, 'how', 'right content';
$t->ua->request_timeout(0);

# Inactivity timeout
$tx = $t->ua->inactivity_timeout(0.5)->build_tx(GET => '/too_long');
$buffer = '';
$tx->res->content->unsubscribe('read')->on(
read => sub {
my ($content, $chunk) = @_;
$buffer .= $chunk;
}
);
$t->ua->start($tx);
is $tx->res->code, 200, 'right status';
$tx = $t->ua->inactivity_timeout(0.5)->get('/too_long');
is $tx->error->{message}, 'Inactivity timeout', 'right error';
is $buffer, 'how', 'right content';
$t->ua->inactivity_timeout(20);

# Transaction is available after rendering early in steps
$t->get_ok('/steps')->status_is(200)->content_is('second');
Expand Down

0 comments on commit 34e5df1

Please sign in to comment.