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