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