Skip to content

Commit

Permalink
fixed usecase and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuke Wada committed Mar 16, 2010
1 parent 8ae4640 commit d283a16
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
35 changes: 16 additions & 19 deletions lib/HTTPx/Dispatcher.pm
Expand Up @@ -8,32 +8,29 @@ use Scalar::Util qw/blessed/;
use Carp;
use base qw/Exporter/;

our @EXPORT = qw/connect match uri_for get post/;
our @EXPORT = qw/connect match uri_for/;

my $rules;

sub connect {
my $pkg = caller(0);
my @args = @_;
push @{ $rules->{$pkg} }, HTTPx::Dispatcher::Rule->new(@args);
}

#XXX
sub method {
my ( $name, $pkg, @args ) = @_;
push @{ $rules->{$pkg} }, HTTPx::Dispatcher::Rule->new(@args, $name );
my @rules;
#XXX
my ($path, $method);
for ( @_ ){
if( ref $_ eq 'HASH' ){
push @rules, [ $path, $_, $method ];
}elsif ( $_ =~ /^(?:get|post)$/ ) {
$method = $_;
}else{
$path = $_;
}
}
for ( @rules ){
push @{ $rules->{$pkg} }, HTTPx::Dispatcher::Rule->new(@$_);
}
}

sub get {
my $pkg = caller(0);
method('get', $pkg, @_ );
};

sub post {
my $pkg = caller(0);
method('post', $pkg, @_ );
};

sub match {
my ( $class, $req ) = @_;
croak "request required" unless blessed $req;
Expand Down
7 changes: 3 additions & 4 deletions lib/HTTPx/Dispatcher/Rule.pm
Expand Up @@ -9,10 +9,9 @@ __PACKAGE__->mk_accessors(qw/re pattern controller action capture requirements c

sub new {
my ($class, $pattern, $args, $method) = @_;
use Data::Dumper;
$args ||= {};
$args->{conditions} ||= {};
$args->{method} = $method || '';
$args->{method} = $method || 'get';
my $self = bless { %$args }, $class;

$self->compile($pattern);
Expand Down Expand Up @@ -46,8 +45,8 @@ sub match {
my ($self, $req) = @_;
croak "request required" unless blessed $req;

if( $self->{method} ){
croak "request method is not match" if $req->method ne uc $self->{method};
if( $self->{method} && $req->method ){
return if $req->method ne uc $self->{method};
}

my $uri = ref($req->uri) ? $req->uri->path : $req->uri;
Expand Down
26 changes: 9 additions & 17 deletions t/06_plack_get_and_post.t
@@ -1,6 +1,6 @@
use strict;
use warnings;
use Test::More tests => 3;
use Test::More tests => 2;
use YAML;
use HTTPx::Dispatcher;
use HTTP::Request;
Expand All @@ -9,38 +9,30 @@ use Test::Requires 'Plack::Request';
{
package MyDispatcher;
use HTTPx::Dispatcher;
get '', { controller => 'Root', action => 'index' };
post '/comment/{id}', { controller => 'Comment', action => 'post' };
connect '/comment',
get => { controller => 'Comment', action => 'show' },
post => { controller => 'Comment', action => 'post' };
}

do {
my $req =
Plack::Request->new( { PATH_INFO => '/', REQUEST_METHOD => 'GET', } );
Plack::Request->new( { PATH_INFO => '/comment', REQUEST_METHOD => 'GET', } );
my $x = MyDispatcher->match($req);
is_deeply $x,
{
'controller' => 'Root',
'controller' => 'Comment',
'args' => {},
'action' => 'index'
'action' => 'show'
};
};

do {
my $req = Plack::Request->new({PATH_INFO => '/comment/1', REQUEST_METHOD => 'POST' });
my $req = Plack::Request->new({PATH_INFO => '/comment', REQUEST_METHOD => 'POST' });
my $x = MyDispatcher->match($req);
is_deeply $x,
{
'controller' => 'Comment',
'args' => { id => 1 },
'args' => {},
'action' => 'post'
};
};

# Error case

do {
my $req =
Plack::Request->new( { PATH_INFO => '/', REQUEST_METHOD => 'POST', } );
my $x = MyDispatcher->match($req);
ok( !$x );
};

0 comments on commit d283a16

Please sign in to comment.