Skip to content

Commit

Permalink
adding the synopsis test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevan Little committed Dec 9, 2009
1 parent 578932f commit 593549c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Plack/App/Path/Router.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Plack::App::Path::Router - A Plack component for dispatching to Path::Router
}
);
$router->add_route('/:action/?:id' =>
validation => {
validations => {
id => 'Int'
},
target => sub {
Expand All @@ -99,14 +99,14 @@ Plack::App::Path::Router - A Plack component for dispatching to Path::Router
}
);
$router->add_route('/:action/edit/:id' =>
validation => {
validations => {
id => 'Int'
},
target => sub {
my ($r, $action, $id) = @_;
# return a string (we will wrap
# it in a PSGI response for you)
"This is my $action, and I am editing this $id";
"This is my action($action), and I am editing this id($id)";
}
);
Expand Down
75 changes: 75 additions & 0 deletions t/000_load.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,84 @@ use strict;
use warnings;

use Test::More;
use Path::Router;
use Plack::Test;

BEGIN {
use_ok('Plack::App::Path::Router');
}

my $router = Path::Router->new;
$router->add_route('/' =>
target => sub {
my ($request) = @_;
# use the Plack::Request to
# create a Plack::Response ...
my $response = $request->new_response( 200 );
$response->content_type('text/html');
$response->body('<html><body>HELLO WORLD</body></html>');
}
);
$router->add_route('/:action/?:id' =>
validations => {
id => 'Int'
},
target => sub {
my ($request, $action, $id) = @_;
# return a PSGI response ...
[
200,
[ 'Content-Type' => 'text/html' ],
[ '<html><body>', $action, $id, '</body></html>' ]
]
}
);
$router->add_route('/:action/edit/:id' =>
validations => {
id => 'Int'
},
target => sub {
my ($r, $action, $id) = @_;
# return a string (we will wrap
# it in a PSGI response for you)
"This is my action($action), and I am editing this id($id)";
}
);

# now create the Plack app
my $app = Plack::App::Path::Router->new( router => $router );
isa_ok($app, 'Plack::App::Path::Router');
isa_ok($app, 'Plack::Component');

test_psgi
app => $app,
client => sub {
my $cb = shift;
{
my $req = HTTP::Request->new(GET => "http://localhost/");
my $res = $cb->($req);
is($res->content, '<html><body>HELLO WORLD</body></html>', '... got the right value for index');
}
{
my $req = HTTP::Request->new(GET => "http://localhost/testing/10");
my $res = $cb->($req);
is($res->content, '<html><body>testing10</body></html>', '... got the right value for /testing/10');
}
{
my $req = HTTP::Request->new(GET => "http://localhost/testing/stuff");
my $res = $cb->($req);
is($res->content, 'Not Found', '... got the right value for 404 (validation did not pass)');
}
{
my $req = HTTP::Request->new(GET => "http://localhost/testing/edit/10");
my $res = $cb->($req);
is($res->content, 'This is my action(testing), and I am editing this id(10)', '... got the right value for /testing/edit/10');
}
{
my $req = HTTP::Request->new(GET => "http://localhost/foo/bar/baz");
my $res = $cb->($req);
is($res->content, 'Not Found', '... got the right value for 404');
}
};

done_testing;

0 comments on commit 593549c

Please sign in to comment.