Created
November 29, 2014 18:31
-
-
Save preaction/b318fe0674b9e65a632e to your computer and use it in GitHub Desktop.
Redirect route not working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Test::Mojo; | |
{ | |
package Statocles::Command::_MOJOAPP; | |
# Currently, as of Mojolicious 5.12, loading the Mojolicious module here | |
# will load the Mojolicious::Commands module, which calls GetOptions, which | |
# will remove -h, --help, -m, and -s from @ARGV. We fix this by copying | |
# @ARGV in bin/statocles before we call Statocles::Command. | |
# | |
# We could fix this in the future by moving this out into its own module, | |
# that is only loaded after we are done passing @ARGV into main(), above. | |
use Mojo::Base 'Mojolicious'; | |
has 'base'; | |
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 '/' ) { | |
say "Base is $base"; | |
$self->routes->get( $base, sub { | |
my ( $c ) = @_; | |
say "REDIRECTING"; | |
$c->redirect_to( $index ); | |
} ); | |
$self->routes->get( $base . '/*path', sub { | |
my ( $c ) = @_; | |
say "SERVING"; | |
$self->static->dispatch( $c ); | |
} ); | |
} | |
} | |
} | |
my $t = Test::Mojo->new( | |
Statocles::Command::_MOJOAPP->new( | |
base => 'http://example.com/nonroot', | |
), | |
); | |
# Check that / redirects | |
$t->get_ok( "/" ) | |
->status_is( 302 ) | |
->header_is( Location => '/nonroot/index.html' ) | |
; | |
# Check that /nonroot redirects | |
$t->get_ok( "/nonroot" ) | |
->status_is( 302 ) | |
->header_is( Location => '/nonroot/index.html' ) | |
; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment