Skip to content

Instantly share code, notes, and snippets.

@preaction
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preaction/f0b94a7433964eab7c52 to your computer and use it in GitHub Desktop.
Save preaction/f0b94a7433964eab7c52 to your computer and use it in GitHub Desktop.
Server only works once?
use Test::More;
use Mojo::Base;
use Mojo::IOLoop;
use Mojo::IOLoop::Delay;
use Mojo::UserAgent;
use Mojo::IOLoop;
{
package Statocles::Command::_MOJOAPP;
use Mojo::Base 'Mojolicious';
has 'base' => 'http://example.com/nonroot';
sub startup {
my ( $self ) = @_;
my $base;
if ( $self->base ) {
$base = Mojo::URL->new( $self->base )->path;
$base =~ s{/$}{};
}
my $index = "/index.html";
if ( $base ) {
$index = $base . $index;
}
$self->routes->get( '/', sub {
my ( $c ) = @_;
$c->redirect_to( $index );
} );
# Add a route for the "home" URL
if ( $base && $base ne '/' ) {
$self->routes->get( $base, sub {
my ( $c ) = @_;
$c->redirect_to( $index );
} );
}
$self->routes->get( $base . '/*path', sub {
my ( $c ) = @_;
$self->static->dispatch( $c );
} );
}
}
my $daemon;
my $port;
my $timeout = Mojo::IOLoop->singleton->timer( 0, sub {
my $id = $daemon->acceptors->[0];
$port = $daemon->ioloop->acceptor( $id )->handle->sockport;
my $ua = Mojo::UserAgent->new( inactivity_timeout => 5, max_redirects => 0, ioloop => $daemon->ioloop );
my $delay = Mojo::IOLoop::Delay->new( ioloop => $daemon->ioloop );
$delay->steps(
sub {
my ( $delay ) = @_;
# Check that / redirects
$ua->get( "http://127.0.0.1:$port", $delay->begin );
},
sub {
my ( $delay, $tx ) = @_;
is $tx->res->code, 302, 'got redirect';
my $location = $tx->res->headers->location;
is $location, '/nonroot/index.html', 'redirect to real root';
# Check that /nonroot redirects
$ua->get( "http://127.0.0.1:$port/nonroot", $delay->begin );
},
sub {
my ( $delay, $tx ) = @_;
is $tx->res->code, 302, 'got redirect';
my $location = $tx->res->headers->location;
is $location, '/nonroot/index.html', 'redirect to real root';
# Check that /nonroot/index.html gets the right content
$ua->get( "http://127.0.0.1:$port/nonroot/index.html", $delay->begin );
},
sub {
my ( $delay, $tx ) = @_;
is $tx->res->code, 200, 'got ok' or diag $tx->res->body;
Mojo::IOLoop->stop;
},
);
} );
$daemon = Mojo::Server::Daemon->new(
silent => 1,
app => Statocles::Command::_MOJOAPP->new(
base => '/nonroot',
),
);
# Using start() instead of run() so we can stop() inside the tests
$daemon->start;
# Find the port we're listening on
my $id = $daemon->acceptors->[0];
my $handle = $daemon->ioloop->acceptor( $id )->handle;
print "Listening on " . sprintf( 'http://%s:%d', $handle->sockhost || '127.0.0.1', $handle->sockport ) . "\n";
# Give control to the IOLoop
Mojo::IOLoop->start;
done_testing;
Listening on http://0.0.0.0:3000
[Thu Nov 27 23:33:24 2014] [debug] Your secret passphrase needs to be changed!!!
[Thu Nov 27 23:33:24 2014] [debug] GET "/".
[Thu Nov 27 23:33:24 2014] [debug] Routing to a callback.
[Thu Nov 27 23:33:24 2014] [debug] 302 Found (0.000634s, 1577.287/s).
ok 1 - got redirect
ok 2 - redirect to real root
[Thu Nov 27 23:33:24 2014] [debug] GET "/nonroot".
[Thu Nov 27 23:33:24 2014] [debug] Template "not_found.development.html.ep" not found.
[Thu Nov 27 23:33:24 2014] [debug] Template "not_found.html.ep" not found.
[Thu Nov 27 23:33:24 2014] [debug] Rendering inline template "8122878e3bc1034543706fb58b4de464".
[Thu Nov 27 23:33:24 2014] [debug] Rendering inline template "4fcf2af99f1803a7a26c2e9b04430f8c".
[Thu Nov 27 23:33:24 2014] [debug] 404 Not Found (0.025639s, 39.003/s).
not ok 3 - got redirect
# Failed test 'got redirect'
# at t/mojoapp.t line 76.
# got: '404'
# expected: '302'
not ok 4 - redirect to real root
# Failed test 'redirect to real root'
# at t/mojoapp.t line 78.
# got: undef
# expected: '/nonroot/index.html'
not ok 5 - got ok
# Failed test 'got ok'
# at t/mojoapp.t line 85.
# got: undef
# expected: '200'
#
1..5
# Looks like you failed 3 tests of 5.
@preaction
Copy link
Author

I also must run the second request at least. If I don't, I can't stop the Mojo::IOLoop. Is there some cleanup I'm supposed to run?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment