Skip to content

Instantly share code, notes, and snippets.

@niczero
Created July 5, 2015 13:47
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 niczero/53402280f9674caeff10 to your computer and use it in GitHub Desktop.
Save niczero/53402280f9674caeff10 to your computer and use it in GitHub Desktop.
Making a fall-thru for a route condition
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();
#!/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