r12983@zaphod: kd | 2008-04-28 18:10:27 +1000
[catagits/Catalyst-Runtime.git] / t / 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 => 50 * $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     {
100         ok(
101             my $response =
102               request('http://localhost/action/go/with_args/old'),
103             'Request with args'
104         );
105         ok( $response->is_success, 'Response Successful 2xx' );
106         is( $response->content, 'old' );
107     }
108
109     {
110         ok(
111             my $response = request(
112                 'http://localhost/action/go/with_method_and_args/new'),
113             'Request with args and method'
114         );
115         ok( $response->is_success, 'Response Successful 2xx' );
116         is( $response->content, 'new' );
117     }
118
119     # test go with embedded args
120     {
121         ok(
122             my $response =
123               request('http://localhost/action/go/args_embed_relative'),
124             'Request'
125         );
126         ok( $response->is_success, 'Response Successful 2xx' );
127         is( $response->content, 'ok' );
128     }
129
130     {
131         ok(
132             my $response =
133               request('http://localhost/action/go/args_embed_absolute'),
134             'Request'
135         );
136         ok( $response->is_success, 'Response Successful 2xx' );
137         is( $response->content, 'ok' );
138     }
139     {
140         my @expected = qw[
141           TestApp::Controller::Action::TestRelative->relative_go
142           TestApp::Controller::Action::Go->one
143           TestApp::Controller::Action::Go->two
144           TestApp::Controller::Action::Go->three
145           TestApp::Controller::Action::Go->four
146           TestApp::Controller::Action::Go->five
147           TestApp::View::Dump::Request->process
148           TestApp->end
149         ];
150
151         @expected = map { /Action/ ? (_begin($_), $_) : ($_) } @expected;
152         my $expected = join( ", ", @expected );
153
154         # Test go to chain of actions.
155         ok( my $response = request('http://localhost/action/relative/relative_go'),
156             'Request' );
157         ok( $response->is_success, 'Response Successful 2xx' );
158         is( $response->content_type, 'text/plain', 'Response Content-Type' );
159         is( $response->header('X-Catalyst-Action'),
160             'action/relative/relative_go', 'Test Action' );
161         is(
162             $response->header('X-Test-Class'),
163             'TestApp::Controller::Action::Go',
164             'Test Class'
165         );
166         is( $response->header('X-Catalyst-Executed'),
167             $expected, 'Executed actions' );
168         like(
169             $response->content,
170             qr/^bless\( .* 'Catalyst::Request' \)$/s,
171             'Content is a serialized Catalyst::Request'
172         );
173     }
174     {
175         my @expected = qw[
176           TestApp::Controller::Action::TestRelative->relative_go_two
177           TestApp::Controller::Action::Go->one
178           TestApp::Controller::Action::Go->two
179           TestApp::Controller::Action::Go->three
180           TestApp::Controller::Action::Go->four
181           TestApp::Controller::Action::Go->five
182           TestApp::View::Dump::Request->process
183           TestApp->end
184         ];
185
186         @expected = map { /Action/ ? (_begin($_), $_) : ($_) } @expected;
187         my $expected = join( ", ", @expected );
188
189         # Test go to chain of actions.
190         ok(
191             my $response =
192               request('http://localhost/action/relative/relative_go_two'),
193             'Request'
194         );
195         ok( $response->is_success, 'Response Successful 2xx' );
196         is( $response->content_type, 'text/plain', 'Response Content-Type' );
197         is(
198             $response->header('X-Catalyst-Action'),
199             'action/relative/relative_go_two',
200             'Test Action'
201         );
202         is(
203             $response->header('X-Test-Class'),
204             'TestApp::Controller::Action::Go',
205             'Test Class'
206         );
207         is( $response->header('X-Catalyst-Executed'),
208             $expected, 'Executed actions' );
209         like(
210             $response->content,
211             qr/^bless\( .* 'Catalyst::Request' \)$/s,
212             'Content is a serialized Catalyst::Request'
213         );
214     }
215
216     # test class go -- MUST FAIL!
217     {
218         ok(
219             my $response = request(
220                 'http://localhost/action/go/class_go_test_action'),
221             'Request'
222         );
223         ok( !$response->is_success, 'Response Fails' );
224         is( $response->content, q(FATAL ERROR: Couldn't go to command "TestApp": Invalid action or component.), 'Error message' );
225     }
226
227     {
228         my @expected = qw[
229           TestApp::Controller::Action::Go->begin
230           TestApp::Controller::Action::Go->go_chained
231           TestApp::Controller::Action::Chained->begin
232           TestApp::Controller::Action::Chained->foo
233           TestApp::Controller::Action::Chained::Foo->spoon
234           TestApp::Controller::Action::Chained->end
235         ];
236
237         my $expected = join( ", ", @expected );
238
239         ok( my $response = request('http://localhost/action/go/go_chained'), 'go to chained + subcontroller endpoint' );
240         is( $response->header('X-Catalyst-Executed'),
241             $expected, 'Executed actions' );
242         is( $response->content, '; 1', 'Content OK' );
243     }
244
245 }
246
247
248
249 sub _begin {
250     local $_ = shift;
251     s/->(.*)$/->begin/;
252     return $_;
253 }
254