1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Dancer2;
use Dancer2::Core::App;
use Dancer2::Core::Dispatcher;
use Dancer2::Core::Hook;
use Dancer2::FileUtils;
use File::Spec;
# our app/dispatcher object
my $app = Dancer2::Core::App->new( name => 'main', );
$app->setting( show_stacktrace => 1 ); # enable show stacktrace
my $dispatcher = Dancer2::Core::Dispatcher->new( apps => [$app] );
# first basic tests
isa_ok $app, 'Dancer2::Core::App';
# some routes to play with
my @routes = (
{ method => 'get',
regexp => '/',
code => sub {'/'},
},
{ method => 'get',
regexp => '/blog',
code => sub {'/blog'},
},
);
# testing with and without prefixes
for my $p ( '/', '/mywebsite' ) {
for my $r (@routes) {
$app->prefix($p);
$app->add_route(%$r);
}
}
is $app->environment, 'development';
my $routes_regexps = $app->routes_regexps_for('get');
is( scalar(@$routes_regexps), 4, "route regexps are OK" );
for my $path ( '/', '/blog', '/mywebsite/', '/mywebsite/blog', ) {
my $env = {
REQUEST_METHOD => 'GET',
PATH_INFO => $path
};
my $expected = {
'/' => '/',
'/blog' => '/blog',
'/mywebsite/' => '/',
'/mywebsite/blog' => '/blog',
};
my $resp = $dispatcher->dispatch($env);
is $resp->[0], 200, 'got a 200';
is $resp->[2][0], $expected->{$path}, 'got expected route';
}
note "testing lexical prefixes";
# clear the prefix in $app (and by the way, makes sure it works when prefix is
# undef).
$app->prefix(undef);
# nested prefixes bitches!
$app->lexical_prefix(
'/foo' => sub {
$app->add_route(
method => 'get',
regexp => '/',
code => sub {'/foo/'}
);
$app->add_route(
method => 'get',
regexp => '/second',
code => sub {'/foo/second'}
);
$app->lexical_prefix(
'/bar' => sub {
$app->add_route(
method => 'get',
regexp => '/',
code => sub {'/foo/bar'}
);
$app->add_route(
method => 'get',
regexp => '/second',
code => sub {'/foo/bar/second'}
);
}
);
},
);
# to make sure the lexical prefix did not crash anything
$app->add_route(
method => 'get',
regexp => '/root',
code => sub {'/root'}
);
# make sure a meaningless lexical prefix is ignored
$app->lexical_prefix(
'/' => sub {
$app->add_route(
method => 'get',
regexp => '/somewhere',
code => sub {'/somewhere'},
);
}
);
for
my $path ( '/foo/', '/foo/second', '/foo/bar/second', '/root', '/somewhere' )
{
my $env = {
REQUEST_METHOD => 'GET',
PATH_INFO => $path,
};
my $resp = $dispatcher->dispatch($env);
is $resp->[0], 200, 'got a 200';
is $resp->[2][0], $path, 'got expected route';
}
note "test a failure in the callback of a lexical prefix";
like(
exception {
$app->lexical_prefix( '/test' => sub { Failure->game_over() } );
},
qr{Unable to run the callback for prefix '/test': Can't locate object method "game_over" via package "Failure"},
"caught an exception in the lexical prefix callback",
);
$app->add_hook(
Dancer2::Core::Hook->new(
name => 'before',
code => sub {1},
)
);
$app->add_hook(
Dancer2::Core::Hook->new(
name => 'before',
code => sub { Foo->failure; },
)
);
$app->compile_hooks;
my $env = {
REQUEST_METHOD => 'GET',
PATH_INFO => '/',
};
like(
$dispatcher->dispatch($env)->[2][0],
qr/Exception caught in 'core.app.before_request' filter: Hook error: Can't locate object method "failure"/,
'before filter nonexistent method failure',
);
$app->replace_hook( 'core.app.before_request', [ sub {1} ] );
$app->compile_hooks;
$env = {
REQUEST_METHOD => 'GET',
PATH_INFO => '/',
};
# test duplicate routes when the path is a regex
$app = Dancer2::Core::App->new( name => 'main' );
my $regexp_route = {
method => 'get', 'regexp' => qr!/(\d+)!, code => sub {1}
};
$app->add_route(%$regexp_route);
# try to get an invalid engine
eval {$app->engine('foo')};
like(
$@,
qr/^Engine 'foo' is not supported/,
"Engine 'foo' does not exist",
);
my $tmpl_engine = $app->engine('template');
ok $tmpl_engine, "Template engine is defined";
ok !$app->has_serializer_engine, "Serializer engine does not exist";
is_deeply(
$app->_get_config_for_engine('NonExistent'),
{},
'Empty configuration for nonexistent engine',
);
# TODO: not such an intelligent check, this one...
# set configuration for an engine
$app->config->{'engines'}{'template'}{'Tiny'}{'hello'} = 'world';
is_deeply(
$app->_get_config_for_engine( template => 'Tiny', $app->config ),
{ hello => 'world' },
'_get_config_for_engine can find the right configuration',
);
is(
File::Spec->canonpath( $app->caller ),
File::Spec->catfile(t => 'app.t'),
'Correct caller for app',
);
done_testing;
|