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
|
use strict;
use warnings;
use Test::More import => ['!pass'];
use Plack::Test;
use HTTP::Request::Common;
use JSON;
subtest 'global and route keywords' => sub {
{
package App1;
use Dancer2;
use t::lib::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;
is( ref $app, 'CODE', '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(
$cb->( GET '/plugin_setting' )->content,
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 t::lib::DancerPlugin;
around_get;
}
my $app = App2->to_app;
is( ref $app, 'CODE', '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;
is( ref $app, 'CODE', '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 t::lib::Hookee;
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'
};
}
my $app = App3->to_app;
is( ref $app, 'CODE', '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' );
};
};
done_testing;
|