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