Mojolicious

 view release on metacpan or  search on metacpan

t/mojolicious/longpolling_lite_app.t  view on Meta::CPAN

    }
  );
  $t->ua->start($tx);
  is $tx->res->code,             200,           'right status';
  is $tx->res->error->{message}, 'Interrupted', 'right error';
  is $buffer,                    'hi ',         'right content';
};

subtest 'Stream with delay and content length' => sub {
  my $stash = undef;
  $t->app->plugins->once(before_dispatch => sub { $stash = shift->stash });
  $t->get_ok('/longpoll/length')
    ->status_is(200)
    ->header_is(Server => 'Mojolicious (Perl)')
    ->content_type_is('text/plain')
    ->content_is('hi there plain, whats up?');
  is $stash->{drain}, 1, 'drain event has been emitted once';
};

subtest 'Stream with delay and finish' => sub {
  $t->get_ok('/longpoll/nolength')
    ->status_is(200)
    ->header_is(Server           => 'Mojolicious (Perl)')
    ->header_is('Content-Length' => undef)
    ->content_type_is('text/plain')
    ->content_is('hi there, what length?');
  ok !$t->tx->keep_alive, 'connection will not be kept alive';
};

subtest 'The drain event should be emitted on the next reactor tick' => sub {
  my $stash = undef;
  $t->app->plugins->once(before_dispatch => sub { $stash = shift->stash });
  $t->get_ok('/longpoll/order')
    ->status_is(200)
    ->header_is(Server => 'Mojolicious (Perl)')
    ->content_is('First, second, third!');
  is $stash->{order}, 1, 'the drain event was emitted on the next reactor tick';
};

subtest 'Static file with cookies and session' => sub {
  my $logs = $t->app->log->capture('trace');
  $t->get_ok('/longpoll/static')
    ->status_is(200)
    ->header_is(Server => 'Mojolicious (Perl)')
    ->header_like('Set-Cookie' => qr/bar=baz/)
    ->header_like('Set-Cookie' => qr/mojolicious=/)
    ->content_type_is('text/plain;charset=UTF-8')
    ->content_is("Hello Mojo from a static file!\n");
  like $logs, qr/Nothing has been rendered, expecting delayed response/, 'right message';
  undef $logs;
};

subtest 'Custom response' => sub {
  $t->get_ok('/longpoll/dynamic')
    ->status_is(201)
    ->header_is(Server => 'Mojolicious (Perl)')
    ->header_like('Set-Cookie' => qr/baz=yada/)
    ->content_is('Dynamic!');
};

subtest 'Chunked response streaming with drain event' => sub {
  my $stash = undef;
  $t->app->plugins->once(before_dispatch => sub { $stash = shift->stash });
  $t->get_ok('/stream')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is('0123456789');
  is $stash->{subscribers}, 0, 'no leaking subscribers';
  ok $stash->{destroyed}, 'controller has been destroyed';
};

subtest 'Rendering of template' => sub {
  my $stash = undef;
  $t->app->plugins->once(before_dispatch => sub { $stash = shift->stash });
  $t->get_ok('/render')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is('Finish!');
  Mojo::IOLoop->one_tick until $stash->{destroyed};
  ok !$stash->{writing},  'finish event timing is right';
  ok $stash->{destroyed}, 'controller has been destroyed';
};

subtest 'Request timeout' => sub {
  my $tx = $t->ua->request_timeout(0.5)->get('/too_long');
  is $tx->error->{message}, 'Request timeout', 'right error';
  $t->ua->request_timeout(0);
};

subtest 'Inactivity timeout' => sub {
  my $tx = $t->ua->inactivity_timeout(0.5)->get('/too_long');
  is $tx->error->{message}, 'Inactivity timeout', 'right error';
  $t->ua->inactivity_timeout(20);
};

done_testing();

__DATA__
@@ render.html.ep
<%= $msg %>\



( run in 2.112 seconds using v1.01-cache-2.11-cpan-6aa56a78535 )