Commit | Line | Data |
d34c067a |
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 | |
89b766ab |
17 | sub test : Local : ActionClass('REST') { |
18 | my ( $self, $c ) = @_; |
d34c067a |
19 | $c->stash->{'entity'} = 'something'; |
20 | } |
21 | |
89b766ab |
22 | sub test_GET : Local : ActionClass('REST') { |
23 | my ( $self, $c ) = @_; |
24 | |
25 | $c->stash->{'entity'} .= " GET"; |
d34c067a |
26 | $c->forward('ok'); |
27 | } |
28 | |
89b766ab |
29 | sub test_POST : Local : ActionClass('REST') { |
30 | my ( $self, $c ) = @_; |
31 | |
32 | $c->stash->{'entity'} .= " POST"; |
d34c067a |
33 | $c->forward('ok'); |
34 | } |
35 | |
89b766ab |
36 | sub test_PUT : Local : ActionClass('REST') { |
37 | my ( $self, $c ) = @_; |
38 | |
39 | $c->stash->{'entity'} .= " PUT"; |
d34c067a |
40 | $c->forward('ok'); |
41 | } |
42 | |
89b766ab |
43 | sub test_DELETE : Local : ActionClass('REST') { |
44 | my ( $self, $c ) = @_; |
45 | |
46 | $c->stash->{'entity'} .= " DELETE"; |
d34c067a |
47 | $c->forward('ok'); |
48 | } |
49 | |
89b766ab |
50 | sub test_OPTIONS : Local : ActionClass('REST') { |
51 | my ( $self, $c ) = @_; |
52 | |
53 | $c->stash->{'entity'} .= " OPTIONS"; |
d34c067a |
54 | $c->forward('ok'); |
55 | } |
56 | |
89b766ab |
57 | sub notreally : Local : ActionClass('REST') { |
58 | } |
d34c067a |
59 | |
60 | sub notreally_GET { |
89b766ab |
61 | my ( $self, $c ) = @_; |
d34c067a |
62 | |
63 | $c->stash->{'entity'} = "notreally GET"; |
64 | $c->forward('ok'); |
65 | } |
66 | |
89b766ab |
67 | sub not_implemented : Local : ActionClass('REST') { |
68 | } |
d34c067a |
69 | |
70 | sub not_implemented_GET { |
89b766ab |
71 | my ( $self, $c ) = @_; |
d34c067a |
72 | |
73 | $c->stash->{'entity'} = "not_implemented GET"; |
74 | $c->forward('ok'); |
75 | } |
76 | |
77 | sub not_implemented_not_implemented { |
89b766ab |
78 | my ( $self, $c ) = @_; |
d34c067a |
79 | |
80 | $c->stash->{'entity'} = "Not Implemented Handler"; |
81 | $c->forward('ok'); |
82 | } |
83 | |
fec6d454 |
84 | sub not_modified : Local : ActionClass('REST') { } |
85 | |
86 | sub not_modified_GET { |
87 | my ( $self, $c ) = @_; |
88 | $c->res->status(304); |
89 | return 1; |
90 | } |
91 | |
92 | |
89b766ab |
93 | sub ok : Private { |
94 | my ( $self, $c ) = @_; |
d34c067a |
95 | |
96 | $c->res->content_type('text/plain'); |
89b766ab |
97 | $c->res->body( $c->stash->{'entity'} ); |
d34c067a |
98 | } |
99 | |
100 | package main; |
101 | |
102 | use strict; |
103 | use warnings; |
6646fdc2 |
104 | use Test::More tests => 17; |
d34c067a |
105 | use FindBin; |
106 | use Data::Dump qw(dump); |
107 | |
89b766ab |
108 | use lib ( "$FindBin::Bin/lib", "$FindBin::Bin/../lib" ); |
d34c067a |
109 | use Test::Rest; |
110 | |
111 | # Should use the default serializer, YAML |
89b766ab |
112 | my $t = Test::Rest->new( 'content_type' => 'text/plain' ); |
d34c067a |
113 | |
114 | use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST'; |
115 | |
116 | foreach my $method (qw(GET DELETE POST PUT OPTIONS)) { |
117 | my $run_method = lc($method); |
89b766ab |
118 | my $result = "something $method"; |
d34c067a |
119 | my $res; |
89b766ab |
120 | if ( grep /$method/, qw(GET DELETE OPTIONS) ) { |
121 | $res = request( $t->$run_method( url => '/test' ) ); |
d34c067a |
122 | } else { |
89b766ab |
123 | $res = request( |
124 | $t->$run_method( |
125 | url => '/test', |
126 | data => { foo => 'bar' } |
d34c067a |
127 | ) |
128 | ); |
129 | } |
130 | ok( $res->is_success, "$method request succeeded" ); |
89b766ab |
131 | is( |
132 | $res->content, |
133 | "something $method", |
134 | "$method request had proper response" |
135 | ); |
d34c067a |
136 | } |
137 | |
89b766ab |
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." ); |
141 | |
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." ); |
146 | |
fec6d454 |
147 | my $modified_res = request( $t->get( url => '/not_modified' ) ); |
148 | is( $modified_res->code, 304, "Not Modified request handler succeeded" ); |
149 | |
89b766ab |
150 | my $ni_res = request( $t->delete( url => '/not_implemented' ) ); |
151 | is( $ni_res->code, 200, "Custom not_implemented handler succeeded" ); |
152 | is( |
153 | $ni_res->content, |
154 | "Not Implemented Handler", |
155 | "not_implemented handler had proper response" |
156 | ); |
d34c067a |
157 | |
158 | 1; |