Skip to content

Commit

Permalink
fixed bridges/ladders and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 13, 2009
1 parent 69a3f6f commit ab43fc9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
This file documents the revision history for Perl extension Mojo.

0.999908 2009-11-11 00:00:00
- Fixed bridges/ladders and added tests.

0.999907 2009-11-11 00:00:00
- Fixed another connection close bug in ioloop.
- Fixed relaxed placeholder format handling in
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo.pm
Expand Up @@ -28,7 +28,7 @@ __PACKAGE__->attr(home => sub { Mojo::Home->new });
__PACKAGE__->attr(log => sub { Mojo::Log->new });

# Oh, so they have internet on computers now!
our $VERSION = '0.999907';
our $VERSION = '0.999908';

sub new {
my $self = shift->SUPER::new(@_);
Expand Down
2 changes: 1 addition & 1 deletion lib/MojoX/Routes.pm
Expand Up @@ -149,7 +149,7 @@ sub match {
$match->path($path);

# Reset stack
$match->stack($snapshot);
$self->parent ? $match->stack($snapshot) : $match->stack([]);
}

$match->endpoint($self) if $self->is_endpoint && $match->is_path_empty;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/Routes.pm
Expand Up @@ -64,7 +64,7 @@ sub _walk {
my ($self, $node, $depth, $routes) = @_;

# Line
my $pattern = $node->pattern->pattern;
my $pattern = $node->pattern->pattern || '/';
my $name = $node->name;
my $line = ' ' x ($depth * 4);
$line .= $pattern;
Expand Down
35 changes: 35 additions & 0 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -76,6 +76,9 @@ sub import {
$defaults ||= {};
$defaults = {%$defaults, callback => $cb};

# Name
$name ||= '';

# Create bridge
return $ROUTES =
$APP->routes->bridge($pattern, {@$constraints})->over($conditions)
Expand Down Expand Up @@ -376,6 +379,38 @@ multiple features at once.
</body>
</html>
Ladders can be used to share code between multiple routes and for
authentication.
All routes following a ladder are only evaluated if the ladder returns a
true value.
use Mojolicious::Lite;
# Authenticate based on name parameter
ladder sub {
my $self = shift;
# Authenticated
my $name = $self->param('name') || '';
return 1 if $name eq 'Bender';
# Not authenticated
$self->render('denied');
return;
};
# GET / (with ladder authentication)
get '/' => 'index';
shagadelic;
__DATA__;
@@ denied.html.ep
You are not Bender, permission denied!
@@ index.html.ep
Hi Bender!
Conditions such as C<agent> allow even more powerful route constructs.
# /foo
Expand Down
71 changes: 70 additions & 1 deletion t/mojolicious/lite_app.t
Expand Up @@ -7,7 +7,7 @@ use warnings;

use utf8;

use Test::More tests => 148;
use Test::More tests => 164;

# Wait you're the only friend I have...
# You really want a robot for a friend?
Expand Down Expand Up @@ -163,6 +163,25 @@ get '/with_ladder_too' => sub {
$self->render_text('Ladders are cool too!');
};

ladder sub {
my $self = shift;

# Authenticated
my $name = $self->param('name') || '';
return 1 if $name eq 'Bender';

# Not authenticated
$self->render('param_auth_denied');
return;
};

# GET /param_auth
get '/param_auth' => 'param_auth';

# GET /param_auth/too
get '/param_auth/too' =>
sub { shift->render_text('You could be Bender too!') };

# Oh Fry, I love you more than the moon, and the stars,
# and the POETIC IMAGE NUMBER 137 NOT FOUND
my $app = Mojolicious::Lite->new;
Expand Down Expand Up @@ -578,7 +597,57 @@ $client->get(
}
)->process;

# GET /param_auth
$client->get(
'/param_auth' => sub {
my ($self, $tx) = @_;
is($tx->res->code, 200);
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->body, "Not Bender!\n");
}
)->process;

# GET /param_auth?name=Bender
$client->get(
'/param_auth?name=Bender' => sub {
my ($self, $tx) = @_;
is($tx->res->code, 200);
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->body, "Bender!\n");
}
)->process;

# GET /param_auth/too
$client->get(
'/param_auth/too' => sub {
my ($self, $tx) = @_;
is($tx->res->code, 200);
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->body, "Not Bender!\n");
}
)->process;

# GET /param_auth/too?name=Bender
$client->get(
'/param_auth/too?name=Bender' => sub {
my ($self, $tx) = @_;
is($tx->res->code, 200);
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->body, 'You could be Bender too!');
}
)->process;

__DATA__
@@ param_auth.html.epl
Bender!
@@ param_auth_denied.html.epl
Not Bender!
@@ root.html.epl
%== shift->url_for('root_path')
Expand Down

0 comments on commit ab43fc9

Please sign in to comment.