d2306627ca2bd3d4a214b69a64143ebeb757cc18
[catagits/Catalyst-Action-REST.git] / t / catalyst-action-rest.t
1 package Test::Catalyst::Action::REST;
2
3 use FindBin;
4
5 use lib ("$FindBin::Bin/../lib");
6
7 use strict;
8 use warnings;
9
10 use Catalyst::Runtime '5.70';
11
12 use Catalyst;
13
14 __PACKAGE__->config( name => 'Test::Catalyst::Action::REST' );
15 __PACKAGE__->setup;
16
17 sub test : Local : ActionClass('REST') {
18     my ( $self, $c ) = @_;
19     $c->stash->{'entity'} = 'something';
20 }
21
22 sub test_GET : Local : ActionClass('REST') {
23     my ( $self, $c ) = @_;
24
25     $c->stash->{'entity'} .= " GET";
26     $c->forward('ok');
27 }
28
29 sub test_POST : Local : ActionClass('REST') {
30     my ( $self, $c ) = @_;
31
32     $c->stash->{'entity'} .= " POST";
33     $c->forward('ok');
34 }
35
36 sub test_PUT : Local : ActionClass('REST') {
37     my ( $self, $c ) = @_;
38
39     $c->stash->{'entity'} .= " PUT";
40     $c->forward('ok');
41 }
42
43 sub test_DELETE : Local : ActionClass('REST') {
44     my ( $self, $c ) = @_;
45
46     $c->stash->{'entity'} .= " DELETE";
47     $c->forward('ok');
48 }
49
50 sub test_OPTIONS : Local : ActionClass('REST') {
51     my ( $self, $c ) = @_;
52
53     $c->stash->{'entity'} .= " OPTIONS";
54     $c->forward('ok');
55 }
56
57 sub notreally : Local : ActionClass('REST') {
58 }
59
60 sub notreally_GET {
61     my ( $self, $c ) = @_;
62
63     $c->stash->{'entity'} = "notreally GET";
64     $c->forward('ok');
65 }
66
67 sub not_implemented : Local : ActionClass('REST') {
68 }
69
70 sub not_implemented_GET {
71     my ( $self, $c ) = @_;
72
73     $c->stash->{'entity'} = "not_implemented GET";
74     $c->forward('ok');
75 }
76
77 sub not_implemented_not_implemented {
78     my ( $self, $c ) = @_;
79
80     $c->stash->{'entity'} = "Not Implemented Handler";
81     $c->forward('ok');
82 }
83
84 sub ok : Private {
85     my ( $self, $c ) = @_;
86
87     $c->res->content_type('text/plain');
88     $c->res->body( $c->stash->{'entity'} );
89 }
90
91 package main;
92
93 use strict;
94 use warnings;
95 use Test::More qw(no_plan);
96 use FindBin;
97 use Data::Dump qw(dump);
98
99 use lib ( "$FindBin::Bin/lib", "$FindBin::Bin/../lib" );
100 use Test::Rest;
101
102 # Should use the default serializer, YAML
103 my $t = Test::Rest->new( 'content_type' => 'text/plain' );
104
105 use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST';
106
107 foreach my $method (qw(GET DELETE POST PUT OPTIONS)) {
108     my $run_method = lc($method);
109     my $result     = "something $method";
110     my $res;
111     if ( grep /$method/, qw(GET DELETE OPTIONS) ) {
112         $res = request( $t->$run_method( url => '/test' ) );
113     } else {
114         $res = request(
115             $t->$run_method(
116                 url  => '/test',
117                 data => { foo => 'bar' }
118             )
119         );
120     }
121     ok( $res->is_success, "$method request succeeded" );
122     is(
123         $res->content,
124         "something $method",
125         "$method request had proper response"
126     );
127 }
128
129 my $fail_res = request( $t->delete( url => '/notreally' ) );
130 is( $fail_res->code, 405, "Request to bad method gets 405 Not Implemented" );
131 is( $fail_res->header('allow'), "GET", "405 allow header properly set." );
132
133 my $options_res = request( $t->options( url => '/notreally' ) );
134 is( $options_res->code, 200, "OPTIONS request handler succeeded" );
135 is( $options_res->header('allow'),
136     "GET", "OPTIONS request allow header properly set." );
137
138 my $ni_res = request( $t->delete( url => '/not_implemented' ) );
139 is( $ni_res->code, 200, "Custom not_implemented handler succeeded" );
140 is(
141     $ni_res->content,
142     "Not Implemented Handler",
143     "not_implemented handler had proper response"
144 );
145
146 1;