Debian Codegraph @

t/dispatcher.t

raw
1 use strict;
2 use warnings;
3 use Test::More import => ['!pass'];
4 use Carp 'croak';
5 use Ref::Util qw<is_regexpref>;
6
7 use Dancer2;
8 use Dancer2::Core::App;
9 use Dancer2::Core::Route;
10 use Dancer2::Core::Dispatcher;
11 use Dancer2::Core::Hook;
12 use Dancer2::Core::Response;
13
14 set logger => 'Null';
15
16 # init our test fixture
17 my $buffer = {};
18 my $app = Dancer2::Core::App->new( name => 'main' );
19
20 $app->setting( logger => engine('logger') );
21 $app->setting( show_stacktrace => 1 );
22
23 # a simple / route
24 my $simple_route = $app->add_route(
25 method => 'get',
26 regexp => '/',
27 code => sub {"home"},
28 );
29
30 # an error route
31 my $error_route = $app->add_route(
32 method => 'get',
33 regexp => '/error',
34 code => sub { Fail->fail },
35 );
36
37 # A chain of two route for /user/$foo
38 my $user_name_route = $app->add_route(
39 method => 'get',
40 regexp => '/user/:name',
41 code => sub {
42 my $app = shift;
43 $buffer->{user} = $app->request->params->{'name'};
44 $app->response->has_passed(1);
45 },
46 );
47
48 my $user_splat_route = $app->add_route(
49 method => 'get',
50 regexp => '/user/*?',
51 code => sub {
52 my $app = shift;
53 "Hello " . $app->request->params->{'name'};
54 },
55 );
56
57 # a route with a 204 response
58 my $removed_content_route = $app->add_route(
59 method => 'get',
60 regexp => '/twoohfour',
61 code => sub {
62 my $app = shift;
63 $app->response->status(204);
64 "This content should be removed";
65 },
66 );
67
68 my $route_from_request;
69
70 # simulates a redirect with halt
71 $app->add_hook(
72 Dancer2::Core::Hook->new(
73 name => 'before',
74 code => sub {
75 my $app = shift;
76 $route_from_request = $app->request->route;
77 if ( $app->request->path_info eq '/haltme' ) {
78 $app->response->header( Location => 'http://perldancer.org' );
79 $app->response->status(302);
80 $app->response->is_halted(1);
81 }
82 },
83 )
84 );
85
86 my $was_in_second_filter = 0;
87 $app->add_hook(
88 Dancer2::Core::Hook->new(
89 name => 'before',
90 code => sub {
91 my $app = shift;
92 if ( $app->request->path_info eq '/haltme' ) {
93 $was_in_second_filter =
94 1; # should not happen because first filter halted the flow
95 }
96 },
97 )
98 );
99
100 my $halt_route = $app->add_route(
101 method => 'get',
102 regexp => '/haltme',
103 code => sub {"should not get there"},
104 );
105 # the tests
106 my @tests = (
107 { env => {
108 REQUEST_METHOD => 'GET',
109 PATH_INFO => '/',
110 },
111 expected => [
112 200,
113 [ 'Content-Length' => 4,
114 'Content-Type' => 'text/html; charset=utf-8',
115 ],
116 ["home"],
117 $simple_route,
118 ]
119 },
120 { env => {
121 REQUEST_METHOD => 'GET',
122 PATH_INFO => '/user/Johnny',
123 },
124 expected => [
125 200,
126 [ 'Content-Length' => 12,
127 'Content-Type' => 'text/html; charset=utf-8',
128 ],
129 ["Hello Johnny"],
130 $user_splat_route, # the second, after the first pass()es
131 ]
132 },
133 { env => {
134 REQUEST_METHOD => 'GET',
135 PATH_INFO => '/twoohfour',
136 },
137 expected => [
138 204,
139 [ 'Content-Type' => 'text/html; charset=utf-8' ],
140 [],
141 $removed_content_route,
142 ]
143 },
144 { env => {
145 REQUEST_METHOD => 'GET',
146 PATH_INFO => '/haltme',
147 },
148 expected => [
149 302,
150 [ 'Location' => 'http://perldancer.org',
151 'Content-Length' => '305',
152 'Content-Type' => 'text/html; charset=utf-8',
153 ],
154 qr/This item has moved/,
155 $halt_route,
156 ]
157 },
158
159 # NOT SUPPORTED YET
160 # { env => {
161 # REQUEST_METHOD => 'GET',
162 # PATH_INFO => '/admin',
163 # },
164 # expected => [200, [], ["home"]]
165 # },
166
167
168 );
169
170 $app->compile_hooks;
171
172 plan tests => 20;
173
174 my $dispatcher = Dancer2::Core::Dispatcher->new( apps => [$app] );
175 my $counter = 0;
176 foreach my $test (@tests) {
177 my $env = $test->{env};
178 my $expected = $test->{expected};
179 my $path = $env->{'PATH_INFO'};
180
181 $route_from_request = undef;
182
183 diag sprintf "Dispatch test %d, for %s %s",
184 $counter,
185 $test->{env}{REQUEST_METHOD},
186 $test->{env}{PATH_INFO};
187
188 my $resp = $dispatcher->dispatch($env);
189
190 is( $resp->[0], $expected->[0], "[$path] Return code ok" );
191
192 my %got_headers = @{ $resp->[1] };
193 my %exp_headers = @{ $expected->[1] };
194 is_deeply( \%got_headers, \%exp_headers, "[$path] Correct headers" );
195
196 if ( is_regexpref( $expected->[2] ) ) {
197 like $resp->[2][0] => $expected->[2], "[$path] Contents ok. (test $counter)";
198 }
199 else {
200 is_deeply $resp->[2] => $expected->[2], "[$path] Contents ok. (test $counter)";
201 }
202
203
204 is(
205 $route_from_request, # squirreled away by before hook,
206 $expected->[3],
207 "Expected route is stored in request (test $counter)",
208 );
209
210 $counter++;
211 }
212
213 foreach my $test (
214 { env => {
215 REQUEST_METHOD => 'GET',
216 PATH_INFO => '/error',
217 'psgi.uri_scheme' => 'http',
218 SERVER_NAME => 'localhost',
219 SERVER_PORT => 5000,
220 SERVER_PROTOCOL => 'HTTP/1.1',
221 },
222 expected => [
223 500,
224 [ 'Content-Length', "Content-Type", 'text/html' ],
225 qr!Internal Server Error.*Can&#39;t locate object method &quot;fail&quot; via package &quot;Fail&quot; \(perhaps you forgot to load &quot;Fail&quot;\?\) at t[\\/]dispatcher\.t line \d+\.!s
226 ]
227 }
228 )
229 {
230 my $env = $test->{env};
231 my $expected = $test->{expected};
232
233 my $psgi_response = $dispatcher->dispatch($env);
234 my $resp = Dancer2::Core::Response->new(
235 status => $psgi_response->[0],
236 headers => $psgi_response->[1],
237 content => $psgi_response->[2][0],
238 );
239
240 is $resp->status => $expected->[0], "Return code ok.";
241 ok( $resp->header('Content-Length') >= 140, "Length ok." );
242 like $resp->content, $expected->[2], "contents ok";
243 }
244
245
246 is $was_in_second_filter, 0, "didn't enter the second filter, because of halt";