Debian Codegraph @

t/app.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Fatal;
5 use Dancer2;
6 use Dancer2::Core::App;
7 use Dancer2::Core::Dispatcher;
8 use Dancer2::Core::Hook;
9 use Path::Tiny qw< path >;
10
11 # our app/dispatcher object
12 my $app = Dancer2::Core::App->new( name => 'main', );
13 $app->setting( show_stacktrace => 1 ); # enable show stacktrace
14 my $dispatcher = Dancer2::Core::Dispatcher->new( apps => [$app] );
15
16 # first basic tests
17 isa_ok $app, 'Dancer2::Core::App';
18
19 # some routes to play with
20 my @routes = (
21 { method => 'get',
22 regexp => '/',
23 code => sub {'/'},
24 },
25 { method => 'get',
26 regexp => '/blog',
27 code => sub {'/blog'},
28 },
29 );
30
31 # testing with and without prefixes
32 for my $p ( '/', '/mywebsite' ) {
33 for my $r (@routes) {
34 $app->prefix($p);
35 $app->add_route(%$r);
36 }
37 }
38
39 is $app->environment, 'development';
40
41 my $routes_regexps = $app->routes_regexps_for('get');
42 is( scalar(@$routes_regexps), 4, "route regexps are OK" );
43
44 for my $path ( '/', '/blog', '/mywebsite/', '/mywebsite/blog', ) {
45 my $env = {
46 REQUEST_METHOD => 'GET',
47 PATH_INFO => $path
48 };
49
50 my $expected = {
51 '/' => '/',
52 '/blog' => '/blog',
53 '/mywebsite/' => '/',
54 '/mywebsite/blog' => '/blog',
55 };
56
57 my $resp = $dispatcher->dispatch($env);
58 is $resp->[0], 200, 'got a 200';
59 is $resp->[2][0], $expected->{$path}, 'got expected route';
60 }
61
62 note "testing lexical prefixes";
63
64 # clear the prefix in $app (and by the way, makes sure it works when prefix is
65 # undef).
66 $app->prefix(undef);
67
68 # nested prefixes bitches!
69 $app->lexical_prefix(
70 '/foo' => sub {
71 $app->add_route(
72 method => 'get',
73 regexp => '/',
74 code => sub {'/foo/'}
75 );
76
77 $app->add_route(
78 method => 'get',
79 regexp => '/second',
80 code => sub {'/foo/second'}
81 );
82
83 $app->lexical_prefix(
84 '/bar' => sub {
85 $app->add_route(
86 method => 'get',
87 regexp => '/',
88 code => sub {'/foo/bar'}
89 );
90 $app->add_route(
91 method => 'get',
92 regexp => '/second',
93 code => sub {'/foo/bar/second'}
94 );
95 }
96 );
97 },
98 );
99
100 # to make sure the lexical prefix did not crash anything
101 $app->add_route(
102 method => 'get',
103 regexp => '/root',
104 code => sub {'/root'}
105 );
106
107 # make sure a meaningless lexical prefix is ignored
108 $app->lexical_prefix(
109 '/' => sub {
110 $app->add_route(
111 method => 'get',
112 regexp => '/somewhere',
113 code => sub {'/somewhere'},
114 );
115 }
116 );
117
118 for
119 my $path ( '/foo/', '/foo/second', '/foo/bar/second', '/root', '/somewhere' )
120 {
121 my $env = {
122 REQUEST_METHOD => 'GET',
123 PATH_INFO => $path,
124 };
125
126 my $resp = $dispatcher->dispatch($env);
127 is $resp->[0], 200, 'got a 200';
128 is $resp->[2][0], $path, 'got expected route';
129 }
130
131 note 'Check to ensure that add_route can override a prefix, even with undef. (gh-1663)';
132
133 my @more_routes = (
134 { method => 'get',
135 regexp => '/prefix_test',
136 code => sub {'/prefix_test'},
137 },
138 { method => 'get',
139 regexp => '/prefix_override_test',
140 prefix => '/prefixtest2',
141 code => sub {'/prefix_override_test'},
142 },
143 { method => 'get',
144 regexp => '/noprefix_test',
145 code => sub {'/noprefix_test'},
146 prefix => undef,
147 },
148 );
149 $app->prefix('/prefixtest');
150
151 for my $r (@more_routes) {
152 $app->add_route(%$r);
153 }
154
155 my $expected_retvals = {
156 '/prefix_test' => 404,
157 '/prefixtest/prefix_test' => 200,
158 '/prefixtest2/prefix_test' => 404,
159 '/prefix_override_test' => 404,
160 '/prefixtest/prefix_override_test' => 404,
161 '/prefixtest2/prefix_override_test' => 200,
162 '/noprefix_test' => 200,
163 '/prefixtest/noprefix_test' => 404,
164 '/prefixtest2/noprefix_test' => 404,
165 };
166
167 my $expected = {
168 '/prefix_test' => undef,
169 '/prefixtest/prefix_test' => '/prefix_test',
170 '/prefixtest2/prefix_test' => undef,
171 '/prefix_override_test' => undef,
172 '/prefixtest/prefix_override_test' => undef,
173 '/prefixtest2/prefix_override_test' => '/prefix_override_test',
174 '/noprefix_test' => '/noprefix_test',
175 '/prefixtest/noprefix_test' => undef,
176 '/prefixtest2/noprefix_test' => undef,
177 };
178
179 for my $path ( sort keys %$expected_retvals ) {
180 my $env = {
181 SERVER_PORT => 5000,
182 SERVER_NAME => 'test.local',
183 REQUEST_METHOD => 'GET',
184 PATH_INFO => $path
185 };
186
187 my $resp = $dispatcher->dispatch($env);
188 is $resp->[0], $expected_retvals->{$path}, "got expected return value on $path";
189 next if $expected_retvals->{$path} == 404;
190 is $resp->[2][0], $expected->{$path}, 'got expected route';
191 }
192
193 note "test a failure in the callback of a lexical prefix";
194 like(
195 exception {
196 $app->lexical_prefix( '/test' => sub { Failure->game_over() } );
197 },
198 qr{Unable to run the callback for prefix '/test': Can't locate object method "game_over" via package "Failure"},
199 "caught an exception in the lexical prefix callback",
200 );
201
202 $app->add_hook(
203 Dancer2::Core::Hook->new(
204 name => 'before',
205 code => sub {1},
206 )
207 );
208
209 $app->add_hook(
210 Dancer2::Core::Hook->new(
211 name => 'before',
212 code => sub { Foo->failure; },
213 )
214 );
215
216 $app->compile_hooks;
217 my $env = {
218 REQUEST_METHOD => 'GET',
219 PATH_INFO => '/',
220 };
221
222 like(
223 $dispatcher->dispatch($env)->[2][0],
224 qr/Exception caught in &#39;core.app.before_request&#39; filter: Hook error: Can&#39;t locate object method &quot;failure&quot;/,
225 'before filter nonexistent method failure',
226 );
227
228 $app->replace_hook( 'core.app.before_request', [ sub {1} ] );
229 $app->compile_hooks;
230 $env = {
231 REQUEST_METHOD => 'GET',
232 PATH_INFO => '/',
233 };
234
235 # test duplicate routes when the path is a regex
236 $app = Dancer2::Core::App->new( name => 'main' );
237 my $regexp_route = {
238 method => 'get', 'regexp' => qr!/(\d+)!, code => sub {1}
239 };
240 $app->add_route(%$regexp_route);
241
242 # try to get an invalid engine
243 eval {$app->engine('foo')};
244 like(
245 $@,
246 qr/^Engine 'foo' is not supported/,
247 "Engine 'foo' does not exist",
248 );
249
250 my $tmpl_engine = $app->engine('template');
251 ok $tmpl_engine, "Template engine is defined";
252
253 ok !$app->has_serializer_engine, "Serializer engine does not exist";
254
255 is_deeply(
256 $app->_get_config_for_engine('NonExistent'),
257 {},
258 'Empty configuration for nonexistent engine',
259 );
260
261 # TODO: not such an intelligent check, these ones...
262 # set configuration for an engine
263 $app->config->{'engines'}{'template'}{'Tiny'}{'hello'} = 'world';
264 $app->config->{'engines'}{'template'}{'Some::Other::Template::Namespace'}{'hello'} = 'world';
265
266 is_deeply(
267 $app->_get_config_for_engine( template => 'Tiny', $app->config ),
268 { hello => 'world' },
269 '_get_config_for_engine can find the right configuration',
270 );
271
272 is_deeply(
273 $app->_get_config_for_engine( template => '+Some::Other::Template::Namespace', $app->config ),
274 { hello => 'world' },
275 '_get_config_for_engine can find the right configuration',
276 );
277
278 is(
279 path( $app->caller ),
280 path( qw< t app.t > ),
281 'Correct caller for app',
282 );
283
284 done_testing;