1 package Test::Catalyst::Action::REST;
5 use lib ("$FindBin::Bin/../lib");
10 use Catalyst::Runtime '5.70';
14 __PACKAGE__->config( name => 'Test::Catalyst::Action::REST' );
17 sub test : Local : ActionClass('REST') {
18 my ( $self, $c ) = @_;
19 $c->stash->{'entity'} = 'something';
22 sub test_GET : Local : ActionClass('REST') {
23 my ( $self, $c ) = @_;
25 $c->stash->{'entity'} .= " GET";
29 sub test_POST : Local : ActionClass('REST') {
30 my ( $self, $c ) = @_;
32 $c->stash->{'entity'} .= " POST";
36 sub test_PUT : Local : ActionClass('REST') {
37 my ( $self, $c ) = @_;
39 $c->stash->{'entity'} .= " PUT";
43 sub test_DELETE : Local : ActionClass('REST') {
44 my ( $self, $c ) = @_;
46 $c->stash->{'entity'} .= " DELETE";
50 sub test_OPTIONS : Local : ActionClass('REST') {
51 my ( $self, $c ) = @_;
53 $c->stash->{'entity'} .= " OPTIONS";
57 sub notreally : Local : ActionClass('REST') {
61 my ( $self, $c ) = @_;
63 $c->stash->{'entity'} = "notreally GET";
67 sub not_implemented : Local : ActionClass('REST') {
70 sub not_implemented_GET {
71 my ( $self, $c ) = @_;
73 $c->stash->{'entity'} = "not_implemented GET";
77 sub not_implemented_not_implemented {
78 my ( $self, $c ) = @_;
80 $c->stash->{'entity'} = "Not Implemented Handler";
84 sub not_modified : Local : ActionClass('REST') { }
86 sub not_modified_GET {
87 my ( $self, $c ) = @_;
94 my ( $self, $c ) = @_;
96 $c->res->content_type('text/plain');
97 $c->res->body( $c->stash->{'entity'} );
104 use Test::More tests => 18;
106 use Data::Dump qw(dump);
108 use lib ( "$FindBin::Bin/lib", "$FindBin::Bin/../lib" );
111 # Should use the default serializer, YAML
112 my $t = Test::Rest->new( 'content_type' => 'text/plain' );
114 use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST';
116 foreach my $method (qw(GET DELETE POST PUT OPTIONS)) {
117 my $run_method = lc($method);
118 my $result = "something $method";
120 if ( grep /$method/, qw(GET DELETE OPTIONS) ) {
121 $res = request( $t->$run_method( url => '/test' ) );
126 data => { foo => 'bar' }
130 ok( $res->is_success, "$method request succeeded" );
134 "$method request had proper response"
138 my $fail_res = request( $t->delete( url => '/notreally' ) );
139 is( $fail_res->code, 405, "Request to bad method gets 405 Not Implemented" );
140 is( $fail_res->header('allow'), "GET", "405 allow header properly set." );
142 my $options_res = request( $t->options( url => '/notreally' ) );
143 is( $options_res->code, 200, "OPTIONS request handler succeeded" );
144 is( $options_res->header('allow'),
145 "GET", "OPTIONS request allow header properly set." );
147 my $modified_res = request( $t->get( url => '/not_modified' ) );
148 is( $modified_res->code, 304, "Not Modified request handler succeeded" );
150 my $ni_res = request( $t->delete( url => '/not_implemented' ) );
151 is( $ni_res->code, 200, "Custom not_implemented handler succeeded" );
154 "Not Implemented Handler",
155 "not_implemented handler had proper response"