Skip to content

Commit

Permalink
added any support and many more tests to Mojolicious::Lite
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 25, 2009
1 parent 96ec80e commit 228acdd
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 15 deletions.
12 changes: 11 additions & 1 deletion lib/MojoX/Routes.pm
Expand Up @@ -178,7 +178,16 @@ sub url_for {

sub via {
my $self = shift;
$self->{_methods} = {map { lc $_ => 1 } @_};

# Methods
my $methods = ref $_[0] ? $_[0] : [@_];

# Shortcut
return $self unless @$methods;

# Cache
$self->{_methods} = {map { lc $_ => 1 } @$methods};

return $self;
}

Expand Down Expand Up @@ -287,6 +296,7 @@ follwing the ones.
$routes = $routes->via('get');
$routes = $routes->via(qw/get post/);
$routes = $routes->via([qw/get post/]);
=head2 C<waypoint>
Expand Down
7 changes: 4 additions & 3 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -34,7 +34,7 @@ sub import {

# Route generator
my $route = sub {
my $method = shift;
my $methods = shift;

my ($cb, $constraints, $defaults, $name, $pattern);

Expand All @@ -47,7 +47,7 @@ sub import {
# Second scalar is the route name
elsif (!ref $arg) { $name = $arg }

# Handler
# Callback
elsif (ref $arg eq 'CODE') { $cb = $arg }

# Constraints
Expand All @@ -66,7 +66,7 @@ sub import {
$defaults = {%$defaults, callback => $cb};

# Create route
$APP->routes->route($pattern, {@$constraints})->via($method)
$APP->routes->route($pattern, {@$constraints})->via($methods)
->to($defaults)->name($name);
};

Expand All @@ -76,6 +76,7 @@ sub import {

# Export
*{"${caller}::app"} = sub {$APP};
*{"${caller}::any"} = sub { $route->(ref $_[0] ? shift : [], @_) };
*{"${caller}::get"} = sub { $route->('get', @_) };
*{"${caller}::post"} = sub { $route->('post', @_) };

Expand Down
12 changes: 8 additions & 4 deletions lib/Mojolicious/Script/Generate/App.pm
Expand Up @@ -92,13 +92,17 @@ L<Mojo::Script> and implements the following new ones.
__DATA__
__404__
<!doctype html>
<head><title>Document not found.</title></head>
<body><h2>Document not found.</h2></body>
<head><title>File Not Found</title></head>
<body>
<h2>File Not Found</h2>
</body>
</html>
__500__
<!doctype html>
<head><title>Internal server error.</title></head>
<body><h2>Internal server error.</h2></body>
<head><title>Internal Server Error</title></head>
<body>
<h2>Internal Server Error</h2>
</body>
</html>
__mojo__
% my $class = shift;
Expand Down
122 changes: 115 additions & 7 deletions t/mojolicious/lite_app.t
Expand Up @@ -5,7 +5,7 @@
use strict;
use warnings;

use Test::More tests => 8;
use Test::More tests => 48;

# Wait you're the only friend I have...
# You really want a robot for a friend?
Expand All @@ -19,36 +19,144 @@ use Mojolicious::Lite;
# Silence
app->log->level('error');

# /foo
# GET /foo
get '/foo' => sub {
my $self = shift;
$self->res->code(200);
$self->res->body('Yea baby!');
};

# /template
get '/template' => 'index';
# POST /template
post '/template' => 'index';

# * /something
any '/something' => sub {
my $self = shift;
$self->res->code(200);
$self->res->body('Just works!');
};

# GET|POST /something/else
any [qw/get post/] => '/something/else' => sub {
my $self = shift;
$self->res->code(200);
$self->res->body('Yay!');
};

# GET /regex/*
get '/regex/:test' => [test => qr/\d+/] => sub {
my $self = shift;
$self->res->code(200);
$self->res->body($self->stash('test'));
};

# POST /bar/*
post '/bar/:test' => {test => 'default'} => sub {
my $self = shift;
$self->res->code(200);
$self->res->body($self->stash('test'));
};

# Oh Fry, I love you more than the moon, and the stars,
# and the POETIC IMAGE NUMBER 137 NOT FOUND
my $client = Mojo::Client->new;

# /foo
# GET /foo
my $tx = Mojo::Transaction->new_get('/foo');
$client->process_app('Mojolicious::Lite', $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, 'Yea baby!');

# /template
$tx = Mojo::Transaction->new_get('/template');
# POST /template
$tx = Mojo::Transaction->new_post('/template');
$client->process_app('Mojolicious::Lite', $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, "works!\n");

# GET /something
$tx = Mojo::Transaction->new_get('/something');
$client->process_app('Mojolicious::Lite', $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, 'Just works!');

# POST /something
$tx = Mojo::Transaction->new_post('/something');
$client->process_app('Mojolicious::Lite', $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, 'Just works!');

# DELETE /something
$tx = Mojo::Transaction->new_delete('/something');
$client->process_app('Mojolicious::Lite', $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, 'Just works!');

# GET /something/else
$tx = Mojo::Transaction->new_get('/something/else');
$client->process_app('Mojolicious::Lite', $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, 'Yay!');

# POST /something/else
$tx = Mojo::Transaction->new_post('/something/else');
$client->process_app('Mojolicious::Lite', $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, 'Yay!');

# DELETE /something/else
$tx = Mojo::Transaction->new_delete('/something/else');
$client->process_app('Mojolicious::Lite', $tx);
is($tx->res->code, 404);
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
like($tx->res->body, qr/File Not Found/);

# GET /regex/23
$tx = Mojo::Transaction->new_get('/regex/23');
$client->process_app('Mojolicious::Lite', $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, '23');

# GET /regex/foo
$tx = Mojo::Transaction->new_get('/regex/foo');
$client->process_app('Mojolicious::Lite', $tx);
is($tx->res->code, 404);
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
like($tx->res->body, qr/File Not Found/);

# POST /bar
$tx = Mojo::Transaction->new_post('/bar');
$client->process_app('Mojolicious::Lite', $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, 'default');

# GET /bar/baz
$tx = Mojo::Transaction->new_post('/bar/baz');
$client->process_app('Mojolicious::Lite', $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, 'baz');

__DATA__
__index.html.eplite__
works!

0 comments on commit 228acdd

Please sign in to comment.