| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More import => ['!pass']; |
| 4 |
use Plack::Test; |
| 5 |
use HTTP::Request::Common; |
| 6 |
use JSON::MaybeXS; |
| 7 |
use Ref::Util qw<is_coderef>; |
| 8 |
|
| 9 |
subtest 'global and route keywords' => sub { |
| 10 |
{ |
| 11 |
package App1; |
| 12 |
use Dancer2; |
| 13 |
use lib 't/lib'; |
| 14 |
use Dancer2::Plugin::FooPlugin; |
| 15 |
|
| 16 |
sub location {'/tmp'} |
| 17 |
|
| 18 |
get '/' => sub { |
| 19 |
foo_wrap_request->env->{'PATH_INFO'}; |
| 20 |
}; |
| 21 |
|
| 22 |
get '/app' => sub { app->name }; |
| 23 |
|
| 24 |
get '/plugin_setting' => sub { to_json(p_config) }; |
| 25 |
|
| 26 |
foo_route; |
| 27 |
} |
| 28 |
|
| 29 |
my $app = App1->to_app; |
| 30 |
ok( is_coderef($app), 'Got app' ); |
| 31 |
|
| 32 |
test_psgi $app, sub { |
| 33 |
my $cb = shift; |
| 34 |
|
| 35 |
is( |
| 36 |
$cb->( GET '/' )->content, |
| 37 |
'/', |
| 38 |
'route defined by a plugin', |
| 39 |
); |
| 40 |
|
| 41 |
is( |
| 42 |
$cb->( GET '/foo' )->content, |
| 43 |
'foo', |
| 44 |
'DSL keyword wrapped by a plugin', |
| 45 |
); |
| 46 |
|
| 47 |
is( |
| 48 |
_normalize($cb->( GET '/plugin_setting' )->content), |
| 49 |
_normalize(encode_json( { plugin => '42' } )), |
| 50 |
'plugin_setting returned the expected config' |
| 51 |
); |
| 52 |
|
| 53 |
is( |
| 54 |
$cb->( GET '/app' )->content, |
| 55 |
'App1', |
| 56 |
'app name is correct', |
| 57 |
); |
| 58 |
}; |
| 59 |
}; |
| 60 |
|
| 61 |
subtest 'plugin old syntax' => sub { |
| 62 |
{ |
| 63 |
package App2; |
| 64 |
use Dancer2; |
| 65 |
use lib 't/lib'; |
| 66 |
use Dancer2::Plugin::DancerPlugin; |
| 67 |
|
| 68 |
around_get; |
| 69 |
} |
| 70 |
|
| 71 |
my $app = App2->to_app; |
| 72 |
ok( is_coderef($app), 'Got app' ); |
| 73 |
|
| 74 |
test_psgi $app, sub { |
| 75 |
my $cb = shift; |
| 76 |
|
| 77 |
is( |
| 78 |
$cb->( GET '/foo/plugin' )->content, |
| 79 |
'foo plugin', |
| 80 |
'foo plugin', |
| 81 |
); |
| 82 |
}; |
| 83 |
}; |
| 84 |
|
| 85 |
subtest caller_dsl => sub { |
| 86 |
my $app = App1->to_app; |
| 87 |
ok( is_coderef($app), 'Got app' ); |
| 88 |
|
| 89 |
test_psgi $app, sub { |
| 90 |
my $cb = shift; |
| 91 |
|
| 92 |
is( |
| 93 |
$cb->( GET '/sitemap' )->content, |
| 94 |
'^\/$, ^\/app$, ^\/foo$, ^\/foo\/plugin$, ^\/plugin_setting$, ^\/sitemap$', |
| 95 |
'Correct content', |
| 96 |
); |
| 97 |
}; |
| 98 |
}; |
| 99 |
|
| 100 |
subtest 'hooks in plugins' => sub { |
| 101 |
my $counter = 0; |
| 102 |
|
| 103 |
{ |
| 104 |
package App3; |
| 105 |
use Dancer2; |
| 106 |
use lib 't/lib'; |
| 107 |
use Dancer2::Plugin::OnPluginImport; |
| 108 |
use Dancer2::Plugin::Hookee; |
| 109 |
use Dancer2::Plugin::EmptyPlugin; |
| 110 |
|
| 111 |
hook 'third_hook' => sub { |
| 112 |
var( hook => 'third hook' ); |
| 113 |
}; |
| 114 |
|
| 115 |
hook 'start_hookee' => sub { |
| 116 |
'this is the start hook'; |
| 117 |
}; |
| 118 |
|
| 119 |
get '/hook_with_var' => sub { |
| 120 |
some_other(); # executes 'third_hook' |
| 121 |
::is var('hook') => 'third hook', "Vars preserved from hooks"; |
| 122 |
}; |
| 123 |
|
| 124 |
get '/hooks_plugin' => sub { |
| 125 |
$counter++; |
| 126 |
some_keyword(); # executes 'start_hookee' |
| 127 |
'hook for plugin'; |
| 128 |
}; |
| 129 |
|
| 130 |
get '/hook_returns_stuff' => sub { |
| 131 |
some_keyword(); # executes 'start_hookee' |
| 132 |
}; |
| 133 |
|
| 134 |
get '/on_import' => sub { |
| 135 |
some_import(); # execute 'plugin_import' |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
my $app = App3->to_app; |
| 141 |
ok( is_coderef($app), 'Got app' ); |
| 142 |
|
| 143 |
test_psgi $app, sub { |
| 144 |
my $cb = shift; |
| 145 |
|
| 146 |
is( $counter, 0, 'the hook has not been executed' ); |
| 147 |
|
| 148 |
is( |
| 149 |
$cb->( GET '/hooks_plugin' )->content, |
| 150 |
'hook for plugin', |
| 151 |
'... route is rendered', |
| 152 |
); |
| 153 |
|
| 154 |
is( $counter, 1, '... and the hook has been executed exactly once' ); |
| 155 |
|
| 156 |
is( |
| 157 |
$cb->( GET '/hook_returns_stuff' )->content, |
| 158 |
'', |
| 159 |
'... hook does not influence rendered content by return value', |
| 160 |
); |
| 161 |
|
| 162 |
# call the route that has an additional test |
| 163 |
$cb->( GET '/hook_with_var' ); |
| 164 |
|
| 165 |
is ( |
| 166 |
$cb->( GET '/on_import' )->content, |
| 167 |
Dancer2->VERSION, |
| 168 |
'hooks added by on_plugin_import don\'t stop hooks being added later' |
| 169 |
); |
| 170 |
}; |
| 171 |
}; |
| 172 |
|
| 173 |
sub _normalize { |
| 174 |
my ($json) = @_; |
| 175 |
|
| 176 |
my $data = decode_json($json); |
| 177 |
foreach (keys %$data) { |
| 178 |
$data->{$_} = $data->{$_} * 1 if ($data->{$_} =~ m/^\d+$/); |
| 179 |
} |
| 180 |
|
| 181 |
return encode_json($data); |
| 182 |
} |
| 183 |
|
| 184 |
done_testing; |