Create branch register_actions.
[catagits/Catalyst-Runtime.git] / t / aggregate / live_component_controller_action_go.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin;
7 use lib "$FindBin::Bin/../lib";
8
9 our $iters;
10
11 BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
12
13 use Test::More tests => 54 * $iters;
14 use Catalyst;
15 use Catalyst::Test 'TestApp';
16
17 if ( $ENV{CAT_BENCHMARK} ) {
18     require Benchmark;
19     Benchmark::timethis( $iters, \&run_tests );
20 }
21 else {
22     for ( 1 .. $iters ) {
23         run_tests();
24     }
25 }
26
27 sub run_tests {
28     {
29         # Test go to global private action
30         ok( my $response = request('http://localhost/action/go/global'),
31             'Request' );
32         ok( $response->is_success, 'Response Successful 2xx' );
33         is( $response->content_type, 'text/plain', 'Response Content-Type' );
34         is( $response->header('X-Catalyst-Action'),
35             'action/go/global', 'Main Class Action' );
36     }
37
38     {
39         my @expected = qw[
40           TestApp::Controller::Action::Go->one
41           TestApp::Controller::Action::Go->two
42           TestApp::Controller::Action::Go->three
43           TestApp::Controller::Action::Go->four
44           TestApp::Controller::Action::Go->five
45           TestApp::View::Dump::Request->process
46           TestApp->end
47         ];
48
49         @expected = map { /Action/ ? (_begin($_), $_) : ($_) } @expected;
50         my $expected = join( ", ", @expected );
51
52         # Test go to chain of actions.
53         ok( my $response = request('http://localhost/action/go/one'),
54             'Request' );
55         ok( $response->is_success, 'Response Successful 2xx' );
56         is( $response->content_type, 'text/plain', 'Response Content-Type' );
57         is( $response->header('X-Catalyst-Action'),
58             'action/go/one', 'Test Action' );
59         is(
60             $response->header('X-Test-Class'),
61             'TestApp::Controller::Action::Go',
62             'Test Class'
63         );
64         is( $response->header('X-Catalyst-Executed'),
65             $expected, 'Executed actions' );
66         like(
67             $response->content,
68             qr/^bless\( .* 'Catalyst::Request' \)$/s,
69             'Content is a serialized Catalyst::Request'
70         );
71     }
72
73     {
74         my @expected = qw[
75           TestApp::Controller::Action::Go->go_die
76           TestApp::Controller::Action::Go->args
77           TestApp->end
78         ];
79
80         @expected = map { /Action/ ? (_begin($_), $_) : ($_) } @expected;
81         my $expected = join( ", ", @expected );
82
83         ok( my $response = request('http://localhost/action/go/go_die'),
84             'Request' );
85         ok( $response->is_success, 'Response Successful 2xx' );
86         is( $response->content_type, 'text/plain', 'Response Content-Type' );
87         is( $response->header('X-Catalyst-Action'),
88             'action/go/go_die', 'Test Action'
89         );
90         is(
91             $response->header('X-Test-Class'),
92             'TestApp::Controller::Action::Go',
93             'Test Class'
94         );
95         is( $response->header('X-Catalyst-Executed'),
96             $expected, 'Executed actions' );
97         is( $response->content, $Catalyst::GO, "Go died as expected" );
98     }
99     {
100         ok(
101             my $response = request('http://localhost/action/go/model'),
102             'Request with args'
103         );
104         is( $response->content,
105             q[FATAL ERROR: Couldn't go("Model::Foo"): Action cannot _DISPATCH. Did you try to go() a non-controller action?],
106             q[go('Model::...') test]
107         );
108     }
109     {
110         ok(
111             my $response = request('http://localhost/action/go/view'),
112             'Request with args'
113         );
114         is( $response->content,
115             q[FATAL ERROR: Couldn't go("View::Dump"): Action cannot _DISPATCH. Did you try to go() a non-controller action?],
116             q[go('View::...') test]
117         );
118     }
119     {
120         ok(
121             my $response =
122               request('http://localhost/action/go/with_args/old'),
123             'Request with args'
124         );
125         ok( $response->is_success, 'Response Successful 2xx' );
126         is( $response->content, 'old', 'go() with args (old)' );
127     }
128
129     {
130         ok(
131             my $response = request(
132                 'http://localhost/action/go/with_method_and_args/new'),
133             'Request with args and method'
134         );
135         ok( $response->is_success, 'Response Successful 2xx' );
136         is( $response->content, 'new', 'go() with args (new)' );
137     }
138
139     # test go with embedded args
140     {
141         ok(
142             my $response =
143               request('http://localhost/action/go/args_embed_relative'),
144             'Request'
145         );
146         ok( $response->is_success, 'Response Successful 2xx' );
147         is( $response->content, 'ok', 'go() with args_embed_relative' );
148     }
149
150     {
151         ok(
152             my $response =
153               request('http://localhost/action/go/args_embed_absolute'),
154             'Request'
155         );
156         ok( $response->is_success, 'Response Successful 2xx' );
157         is( $response->content, 'ok', 'go() with args_embed_absolute' );
158     }
159     {
160         my @expected = qw[
161           TestApp::Controller::Action::TestRelative->relative_go
162           TestApp::Controller::Action::Go->one
163           TestApp::Controller::Action::Go->two
164           TestApp::Controller::Action::Go->three
165           TestApp::Controller::Action::Go->four
166           TestApp::Controller::Action::Go->five
167           TestApp::View::Dump::Request->process
168           TestApp->end
169         ];
170
171         @expected = map { /Action/ ? (_begin($_), $_) : ($_) } @expected;
172         my $expected = join( ", ", @expected );
173
174         # Test go to chain of actions.
175         ok( my $response = request('http://localhost/action/relative/relative_go'),
176             'Request' );
177         ok( $response->is_success, 'Response Successful 2xx' );
178         is( $response->content_type, 'text/plain', 'Response Content-Type' );
179         is( $response->header('X-Catalyst-Action'),
180             'action/relative/relative_go', 'Test Action' );
181         is(
182             $response->header('X-Test-Class'),
183             'TestApp::Controller::Action::Go',
184             'Test Class'
185         );
186         is( $response->header('X-Catalyst-Executed'),
187             $expected, 'Executed actions' );
188         like(
189             $response->content,
190             qr/^bless\( .* 'Catalyst::Request' \)$/s,
191             'Content is a serialized Catalyst::Request'
192         );
193     }
194     {
195         my @expected = qw[
196           TestApp::Controller::Action::TestRelative->relative_go_two
197           TestApp::Controller::Action::Go->one
198           TestApp::Controller::Action::Go->two
199           TestApp::Controller::Action::Go->three
200           TestApp::Controller::Action::Go->four
201           TestApp::Controller::Action::Go->five
202           TestApp::View::Dump::Request->process
203           TestApp->end
204         ];
205
206         @expected = map { /Action/ ? (_begin($_), $_) : ($_) } @expected;
207         my $expected = join( ", ", @expected );
208
209         # Test go to chain of actions.
210         ok(
211             my $response =
212               request('http://localhost/action/relative/relative_go_two'),
213             'Request'
214         );
215         ok( $response->is_success, 'Response Successful 2xx' );
216         is( $response->content_type, 'text/plain', 'Response Content-Type' );
217         is(
218             $response->header('X-Catalyst-Action'),
219             'action/relative/relative_go_two',
220             'Test Action'
221         );
222         is(
223             $response->header('X-Test-Class'),
224             'TestApp::Controller::Action::Go',
225             'Test Class'
226         );
227         is( $response->header('X-Catalyst-Executed'),
228             $expected, 'Executed actions' );
229         like(
230             $response->content,
231             qr/^bless\( .* 'Catalyst::Request' \)$/s,
232             'Content is a serialized Catalyst::Request'
233         );
234     }
235
236     # test class go -- MUST FAIL!
237     {
238         ok(
239             my $response = request(
240                 'http://localhost/action/go/class_go_test_action'),
241             'Request'
242         );
243         ok( !$response->is_success, 'Response Fails' );
244         is( $response->content,
245             q(FATAL ERROR: Couldn't go("TestApp"): Action has no namespace: cannot go() to a plain method or component, must be a :Action or some sort.),
246             'Error message'
247         );
248     }
249
250     {
251         my @expected = qw[
252           TestApp::Controller::Action::Go->begin
253           TestApp::Controller::Action::Go->go_chained
254           TestApp::Controller::Action::Chained->begin
255           TestApp::Controller::Action::Chained->foo
256           TestApp::Controller::Action::Chained::Foo->spoon
257           TestApp::Controller::Action::Chained->end
258         ];
259
260         my $expected = join( ", ", @expected );
261
262         ok( my $response = request('http://localhost/action/go/go_chained'), 'go to chained + subcontroller endpoint' );
263         is( $response->header('X-Catalyst-Executed'),
264             $expected, 'Executed actions' );
265         is( $response->content, '; 1', 'Content OK' );
266     }
267
268 }
269
270
271
272 sub _begin {
273     local $_ = shift;
274     s/->(.*)$/->begin/;
275     return $_;
276 }
277