Created
July 5, 2015 13:47
-
-
Save niczero/53402280f9674caeff10 to your computer and use it in GitHub Desktop.
Making a fall-thru for a route condition
This file contains 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 Mojo::Base -strict; | |
use Test::More; | |
use Test::Mojo; | |
use FindBin; | |
require "$FindBin::Bin/../x"; | |
my $t = Test::Mojo->new; | |
$t->get_ok('/abc/public?username=bill')->status_is(200)->content_like(qr/public/);; | |
$t->get_ok('/abc/private?username=alan')->status_is(200)->content_like(qr/username: alan/); | |
$t->get_ok('/abc/other?username=alan')->status_is(200)->content_like(qr/something else/); | |
$t->get_ok('/abc/private?username=cara')->status_is(200)->content_like(qr/Plan C/); # timeout | |
done_testing(); |
This file contains 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
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
my $route = under 'abc'; | |
get 'public' => sub { shift->render(text => "public\n") }; | |
my $secure = $route->under(sub { | |
my $c = shift; return 1 if ($c->param('username') // '') =~ /^a/; undef | |
})->name('is_secure'); | |
$secure->get('private' => sub { | |
my $c = shift; | |
$c->render(text => sprintf "username: %s\n", $c->param('username')) | |
}); | |
$secure->any('*any' => sub { shift->render(text => "something else\n") }); | |
$route->any('*any' => sub { shift->render(text => "try Plan C\n") }); | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment