From: Arthur Axel 'fREW' Schmidt Date: Thu, 27 Aug 2009 00:47:21 +0000 (-0500) Subject: Merge branch 'master' of git://github.com/bobtfish/catalyst-action-rest X-Git-Tag: 0.77~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Action-REST.git;a=commitdiff_plain;h=29739a599abe110c310ec26dc2b528f773bc579b;hp=c42820ae3a3798274457f39e4d1aaf1d53dd676a Merge branch 'master' of git://github.com/bobtfish/catalyst-action-rest --- diff --git a/lib/Catalyst/Action/REST.pm b/lib/Catalyst/Action/REST.pm index c9d336e..f4e0a2f 100644 --- a/lib/Catalyst/Action/REST.pm +++ b/lib/Catalyst/Action/REST.pm @@ -95,7 +95,10 @@ sub dispatch { my ($code, $name); # Common case, for foo_GET etc - if ($code = $controller->can($rest_method)) { + if ( $code = $controller->action_for($rest_method) ) { + $c->execute( $self->class, $self, @{ $c->req->args } ); + return $c->forward( $code, $c->req->args ); + } elsif ($code = $controller->can($rest_method)) { # Exceute normal action $c->execute( $self->class, $self, @{ $c->req->args } ); $name = $rest_method; diff --git a/t/catalyst-action-rest-action-dispatch.t b/t/catalyst-action-rest-action-dispatch.t new file mode 100644 index 0000000..93ffb17 --- /dev/null +++ b/t/catalyst-action-rest-action-dispatch.t @@ -0,0 +1,61 @@ +use strict; +use warnings; +use Test::More tests => 23; +use FindBin; + +use lib ( "$FindBin::Bin/lib", "$FindBin::Bin/../lib" ); +use Test::Rest; + +# Should use the default serializer, YAML +my $t = Test::Rest->new( 'content_type' => 'text/plain' ); + +use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST'; + +foreach my $method (qw(GET DELETE POST PUT OPTIONS)) { + my $run_method = lc($method); + my $result = "something $method"; + my $res; + if ( grep /$method/, qw(GET DELETE OPTIONS) ) { + $res = request( $t->$run_method( url => '/actions/test' ) ); + } else { + $res = request( + $t->$run_method( + url => '/actions/test', + data => '', + ) + ); + } + ok( $res->is_success, "$method request succeeded" ); + is( + $res->content, + "something $method", + "$method request had proper response" + ); + is( + $res->header('Using-Action'), + 'STATION', + "went through action for dispatching to $method" + ); +} + +my $fail_res = request( $t->delete( url => '/actions/notreally' ) ); +is( $fail_res->code, 405, "Request to bad method gets 405 Not Implemented" ); +is( $fail_res->header('allow'), "GET", "405 allow header properly set." ); + +my $options_res = request( $t->options( url => '/actions/notreally' ) ); +is( $options_res->code, 200, "OPTIONS request handler succeeded" ); +is( $options_res->header('allow'), + "GET", "OPTIONS request allow header properly set." ); + +my $modified_res = request( $t->get( url => '/actions/not_modified' ) ); +is( $modified_res->code, 304, "Not Modified request handler succeeded" ); + +my $ni_res = request( $t->delete( url => '/actions/not_implemented' ) ); +is( $ni_res->code, 200, "Custom not_implemented handler succeeded" ); +is( + $ni_res->content, + "Not Implemented Handler", + "not_implemented handler had proper response" +); + +1; diff --git a/t/lib/Test/Action/Class.pm b/t/lib/Test/Action/Class.pm new file mode 100644 index 0000000..84f651a --- /dev/null +++ b/t/lib/Test/Action/Class.pm @@ -0,0 +1,15 @@ +package Test::Action::Class; +use strict; +use warnings; + +use Moose; +BEGIN { extends 'Catalyst::Action' }; + +before execute => sub { + my ($self, $controller, $c, @args) = @_; + $c->response->header( 'Using-Action' => 'STATION' ); +}; + +no Moose; + +1; diff --git a/t/lib/Test/Catalyst/Action/REST/Controller/Actions.pm b/t/lib/Test/Catalyst/Action/REST/Controller/Actions.pm new file mode 100644 index 0000000..2612a4e --- /dev/null +++ b/t/lib/Test/Catalyst/Action/REST/Controller/Actions.pm @@ -0,0 +1,95 @@ +package Test::Catalyst::Action::REST::Controller::Actions; +use strict; +use warnings; + +use base qw/Catalyst::Controller::REST/; + +__PACKAGE__->config( namespace => 'actions' ); + +sub begin {} # Don't need serialization.. + +sub test : Local : ActionClass('REST') { + my ( $self, $c ) = @_; + $c->stash->{'entity'} = 'something'; +} + +sub test_GET : Private ActionClass('+Test::Action::Class') { + my ( $self, $c ) = @_; + + $c->stash->{'entity'} .= " GET"; + $c->forward('ok'); +} + +sub test_POST : Action ActionClass('+Test::Action::Class') { + my ( $self, $c ) = @_; + + $c->stash->{'entity'} .= " POST"; + $c->forward('ok'); +} + +sub test_PUT :ActionClass('+Test::Action::Class') { + my ( $self, $c ) = @_; + + $c->stash->{'entity'} .= " PUT"; + $c->forward('ok'); +} + +sub test_DELETE :ActionClass('+Test::Action::Class') { + my ( $self, $c ) = @_; + $c->stash->{'entity'} .= " DELETE"; + $c->forward('ok'); +} + +sub test_OPTIONS :ActionClass('+Test::Action::Class') { + my ( $self, $c ) = @_; + + $c->stash->{'entity'} .= " OPTIONS"; + $c->forward('ok'); +} + +sub notreally : Local : ActionClass('REST') { +} + +sub notreally_GET :ActionClass('+Test::Action::Class') { + my ( $self, $c ) = @_; + + $c->stash->{'entity'} = "notreally GET"; + $c->forward('ok'); +} + +sub not_implemented : Local : ActionClass('REST') { +} + +sub not_implemented_GET :ActionClass('+Test::Action::Class') { + my ( $self, $c ) = @_; + + $c->stash->{'entity'} = "not_implemented GET"; + $c->forward('ok'); +} + +sub not_implemented_not_implemented :ActionClass('+Test::Action::Class') { + my ( $self, $c ) = @_; + + $c->stash->{'entity'} = "Not Implemented Handler"; + $c->forward('ok'); +} + +sub not_modified : Local : ActionClass('REST') { } + +sub not_modified_GET { + my ( $self, $c ) = @_; + $c->res->status(304); + return 1; +} + +sub ok : Private { + my ( $self, $c ) = @_; + + $c->res->content_type('text/plain'); + $c->res->body( $c->stash->{'entity'} ); +} + +sub end : Private {} # Don't need serialization.. + +1; +