use Mojo::Base -strict; use Test::More tests => 6; use Mojo::IOLoop; use Mojolicious::Lite; use Test::Mojo; # GET /too_long my $too_long; get '/too_long' => sub { my $self = shift; $self->on(finish => sub { $too_long = 'finished!' }); $self->res->code(200); $self->res->headers->content_type('text/plain'); $self->res->headers->content_length(1001); $self->write('x' x 1000); Mojo::IOLoop->timer( 5 => sub { $self->write( sub { my $self = shift; $self->write('x'); } ); } ); }; my $t = Test::Mojo->new; # GET /too_long (request timeout) my $tx = $t->ua->request_timeout(0.5)->build_tx(GET => '/too_long'); my $buffer = ''; $tx->res->body( sub { my ($self, $chunk) = @_; $buffer .= $chunk; } ); $t->ua->start($tx); is $tx->res->code, 200, 'right status'; is $tx->error, 'Request timeout.', 'right error'; is $buffer, 'x' x 1000, 'right content'; $t->ua->request_timeout(0); # GET /too_long (request timeout) $tx = $t->ua->request_timeout(3)->build_tx(GET => '/too_long'); $buffer = ''; $tx->res->body( sub { my ($self, $chunk) = @_; $buffer .= $chunk; } ); $t->ua->start($tx); is $tx->res->code, 200, 'right status'; is $tx->error, 'Request timeout.', 'right error'; is $buffer, 'x' x 1000, 'right content'; $t->ua->request_timeout(0);