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
|
use strict;
use warnings;
use Test::More import => ['!pass'];
use Plack::Test;
use HTTP::Request::Common;
use JSON::MaybeXS;
use Ref::Util qw<is_coderef>;
subtest 'global and route keywords' => sub {
{
package App1;
use Dancer2;
use lib 't/lib';
use Dancer2::Plugin::FooPlugin;
sub location {'/tmp'}
get '/' => sub {
foo_wrap_request->env->{'PATH_INFO'};
};
get '/app' => sub { app->name };
get '/plugin_setting' => sub { to_json(p_config) };
foo_route;
}
my $app = App1->to_app;
ok( is_coderef($app), 'Got app' );
test_psgi $app, sub {
my $cb = shift;
is(
$cb->( GET '/' )->content,
'/',
'route defined by a plugin',
);
is(
$cb->( GET '/foo' )->content,
'foo',
'DSL keyword wrapped by a plugin',
);
is(
_normalize($cb->( GET '/plugin_setting' )->content),
_normalize(encode_json( { plugin => '42' } )),
'plugin_setting returned the expected config'
);
is(
$cb->( GET '/app' )->content,
'App1',
'app name is correct',
);
};
};
subtest 'plugin old syntax' => sub {
{
package App2;
use Dancer2;
use lib 't/lib';
use Dancer2::Plugin::DancerPlugin;
around_get;
}
my $app = App2->to_app;
ok( is_coderef($app), 'Got app' );
test_psgi $app, sub {
my $cb = shift;
is(
$cb->( GET '/foo/plugin' )->content,
'foo plugin',
'foo plugin',
);
};
};
subtest caller_dsl => sub {
my $app = App1->to_app;
ok( is_coderef($app), 'Got app' );
test_psgi $app, sub {
my $cb = shift;
is(
$cb->( GET '/sitemap' )->content,
'^\/$, ^\/app$, ^\/foo$, ^\/foo\/plugin$, ^\/plugin_setting$, ^\/sitemap$',
'Correct content',
);
};
};
subtest 'hooks in plugins' => sub {
my $counter = 0;
{
package App3;
use Dancer2;
use lib 't/lib';
use Dancer2::Plugin::OnPluginImport;
use Dancer2::Plugin::Hookee;
use Dancer2::Plugin::EmptyPlugin;
hook 'third_hook' => sub {
var( hook => 'third hook' );
};
hook 'start_hookee' => sub {
'this is the start hook';
};
get '/hook_with_var' => sub {
some_other(); # executes 'third_hook'
::is var('hook') => 'third hook', "Vars preserved from hooks";
};
get '/hooks_plugin' => sub {
$counter++;
some_keyword(); # executes 'start_hookee'
'hook for plugin';
};
get '/hook_returns_stuff' => sub {
some_keyword(); # executes 'start_hookee'
};
get '/on_import' => sub {
some_import(); # execute 'plugin_import'
}
}
my $app = App3->to_app;
ok( is_coderef($app), 'Got app' );
test_psgi $app, sub {
my $cb = shift;
is( $counter, 0, 'the hook has not been executed' );
is(
$cb->( GET '/hooks_plugin' )->content,
'hook for plugin',
'... route is rendered',
);
is( $counter, 1, '... and the hook has been executed exactly once' );
is(
$cb->( GET '/hook_returns_stuff' )->content,
'',
'... hook does not influence rendered content by return value',
);
# call the route that has an additional test
$cb->( GET '/hook_with_var' );
is (
$cb->( GET '/on_import' )->content,
Dancer2->VERSION,
'hooks added by on_plugin_import don\'t stop hooks being added later'
);
};
};
sub _normalize {
my ($json) = @_;
my $data = decode_json($json);
foreach (keys %$data) {
$data->{$_} = $data->{$_} * 1 if ($data->{$_} =~ m/^\d+$/);
}
return encode_json($data);
}
done_testing;
|