| 1 |
package Dancer2::Manual::Testing; |
| 2 |
# ABSTRACT: Writing tests for Dancer2 |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
|
| 7 |
1; |
| 8 |
|
| 9 |
__END__ |
| 10 |
|
| 11 |
=pod |
| 12 |
|
| 13 |
=encoding UTF-8 |
| 14 |
|
| 15 |
=head1 NAME |
| 16 |
|
| 17 |
Dancer2::Manual::Testing - Writing tests for Dancer2 |
| 18 |
|
| 19 |
=head1 VERSION |
| 20 |
|
| 21 |
version 2.2.2 |
| 22 |
|
| 23 |
=head1 Basic application testing |
| 24 |
|
| 25 |
Since L<Dancer2> produces PSGI applications, you can easily write tests using |
| 26 |
L<Plack::Test> and provide your Dancer application as the app for testing. |
| 27 |
|
| 28 |
A basic test (which we also scaffold with L<dancer2>) looks like this: |
| 29 |
|
| 30 |
use strict; |
| 31 |
use warnings; |
| 32 |
|
| 33 |
use Test::More tests => 4; |
| 34 |
use Plack::Test; |
| 35 |
use HTTP::Request::Common; |
| 36 |
|
| 37 |
use_ok('MyApp'); |
| 38 |
|
| 39 |
# create an application |
| 40 |
my $app = MyApp->to_app; |
| 41 |
isa_ok( $app, 'CODE' ); |
| 42 |
|
| 43 |
# create a testing object |
| 44 |
my $test = Plack::Test->create($app); |
| 45 |
|
| 46 |
# now you can call requests on it and get responses |
| 47 |
# requests are of HTTP::Request |
| 48 |
# responses are of HTTP::Response |
| 49 |
|
| 50 |
# "GET" from HTTP::Request::Common creates an HTTP::Request object |
| 51 |
my $response = $test->request( GET '/' ); |
| 52 |
|
| 53 |
# same as: |
| 54 |
# my $response = $test->request( HTTP::Request->new( GET => '/' ) ); |
| 55 |
|
| 56 |
ok( $response->is_success, 'Successful request' ); |
| 57 |
is( $response->content, 'OK', 'Correct response content' ); |
| 58 |
|
| 59 |
Read the documentation for L<HTTP::Request> and L<HTTP::Request::Common> to |
| 60 |
see the different options for sending parameters. |
| 61 |
|
| 62 |
=head3 Subtests |
| 63 |
|
| 64 |
Tests can be separated using L<Test::More>'s C<subtest> functionality, |
| 65 |
thus creating multiple self-contained tests that don't affect each other. |
| 66 |
|
| 67 |
Assuming we have a different app that we want to test: |
| 68 |
|
| 69 |
# MyApp.pm |
| 70 |
package MyApp; |
| 71 |
use Dancer2; |
| 72 |
set serializer => 'JSON'; |
| 73 |
|
| 74 |
my %users = ( |
| 75 |
jason => { |
| 76 |
name => 'Jason', |
| 77 |
likes => 'planes', |
| 78 |
}, |
| 79 |
yanick => { |
| 80 |
name => 'Yanick', |
| 81 |
likes => 'orchids', |
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
get '/:user' => sub { |
| 86 |
my $user = route_parameters->get('user'); |
| 87 |
|
| 88 |
my $user_data = $users{$user}; |
| 89 |
|
| 90 |
if( $user_data ) { |
| 91 |
return { user => $user_data }; |
| 92 |
} |
| 93 |
|
| 94 |
status 404; |
| 95 |
|
| 96 |
return { |
| 97 |
error => 1, |
| 98 |
message => 'user not found' |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
1; |
| 103 |
|
| 104 |
This is an example of tests for that route ensuring |
| 105 |
that we have the correct behavior with regard to the user |
| 106 |
parameter. |
| 107 |
|
| 108 |
# param.t |
| 109 |
use strict; |
| 110 |
use warnings; |
| 111 |
|
| 112 |
use Test::More; |
| 113 |
use Plack::Test; |
| 114 |
use HTTP::Request::Common; |
| 115 |
use JSON qw/ decode_json /; |
| 116 |
|
| 117 |
use MyApp; |
| 118 |
|
| 119 |
# swap 'null' for 'note' to see the logs in your TAP output |
| 120 |
MyApp::set( logger => 'null' ); |
| 121 |
|
| 122 |
my $test = Plack::Test->create( MyApp->to_app ); |
| 123 |
|
| 124 |
subtest 'An empty request' => sub { |
| 125 |
my $res = $test->request( GET '/' ); |
| 126 |
is $res->code => 404, 'user not provided, so not found'; |
| 127 |
}; |
| 128 |
|
| 129 |
subtest 'Request with invalid user' => sub { |
| 130 |
my $res = $test->request( GET '/sawyer_x' ); |
| 131 |
|
| 132 |
ok !$res->is_success, 'user not found'; |
| 133 |
is $res->code => 404, 'ressource not found'; |
| 134 |
|
| 135 |
is decode_json($res->content)->{message}, 'user not found', 'error message'; |
| 136 |
}; |
| 137 |
|
| 138 |
subtest 'Request with valid user' => sub { |
| 139 |
my $res = $test->request( GET '/jason' ); |
| 140 |
|
| 141 |
ok $res->is_success, 'user found'; |
| 142 |
|
| 143 |
is decode_json($res->content)->{user}{likes}, 'planes', 'data present'; |
| 144 |
}; |
| 145 |
|
| 146 |
done_testing(); |
| 147 |
|
| 148 |
=head1 Cookies |
| 149 |
|
| 150 |
To handle cookies, which are mostly used for maintaining sessions, |
| 151 |
the following modules can be used: |
| 152 |
|
| 153 |
=over 4 |
| 154 |
|
| 155 |
=item * L<Test::WWW::Mechanize::PSGI> |
| 156 |
|
| 157 |
=item * L<LWP::Protocol::PSGI> |
| 158 |
|
| 159 |
=item * L<HTTP::Cookies> |
| 160 |
|
| 161 |
=back |
| 162 |
|
| 163 |
Taking the previous test, assuming it actually creates and uses |
| 164 |
cookies for sessions (see L<cookie|Dancer2::Manual/"Setting and Updating |
| 165 |
Cookies"> and perhaps L<Dancer2::Session::Cookie> for more information on how |
| 166 |
to do that): |
| 167 |
|
| 168 |
# ... all the use statements |
| 169 |
use HTTP::Cookies; |
| 170 |
|
| 171 |
my $jar = HTTP::Cookies->new; |
| 172 |
my $test = Plack::Test->create( MyApp->to_app ); |
| 173 |
|
| 174 |
subtest 'Request with invalid user' => sub { |
| 175 |
my $req = GET '/sawyer_x'; |
| 176 |
$jar->add_cookie_header($req); |
| 177 |
my $res = $test->request($req); |
| 178 |
ok !$res->is_success, 'Request failed'; |
| 179 |
$jar->extract_cookies($res); |
| 180 |
|
| 181 |
ok !$jar->as_string, 'All cookies deleted'; |
| 182 |
}; |
| 183 |
|
| 184 |
subtest 'Request with user' => sub { |
| 185 |
my $req = GET '/jason'; |
| 186 |
$jar->add_cookie_header($req); |
| 187 |
my $res = $test->request($req); |
| 188 |
ok $res->is_success, 'Successful request'; |
| 189 |
$jar->extract_cookies($res); |
| 190 |
|
| 191 |
ok !$jar->as_string, 'All cookies deleted'; |
| 192 |
}; |
| 193 |
|
| 194 |
done_testing(); |
| 195 |
|
| 196 |
If you don't want to use an entire user agent for this test, you can use |
| 197 |
L<HTTP::Cookies> to store cookies and then retrieve them: |
| 198 |
|
| 199 |
use strict; |
| 200 |
use warnings; |
| 201 |
|
| 202 |
use Test::More tests => 3; |
| 203 |
use Plack::Test; |
| 204 |
use HTTP::Request::Common; |
| 205 |
use HTTP::Cookies; |
| 206 |
|
| 207 |
use_ok('MyApp'); |
| 208 |
|
| 209 |
my $url = 'http://localhost'; |
| 210 |
my $jar = HTTP::Cookies->new(); |
| 211 |
my $test = Plack::Test->create( MyApp->to_app ); |
| 212 |
|
| 213 |
subtest 'Create session' => sub { |
| 214 |
my $res = $test->request( GET "$url/login" ); |
| 215 |
ok( $res->is_success, 'Successful login' ); |
| 216 |
|
| 217 |
# extract cookies from the response and store in the jar |
| 218 |
$jar->extract_cookies($res); |
| 219 |
}; |
| 220 |
|
| 221 |
subtest 'Check session' => sub { |
| 222 |
my $req = GET "$url/logout"; |
| 223 |
|
| 224 |
# add cookies to the request |
| 225 |
$jar->add_cookie_header($req); |
| 226 |
|
| 227 |
my $res = $test->request($req); |
| 228 |
ok( $res->is_success, 'Successful logout' ); |
| 229 |
like( |
| 230 |
$res->content, |
| 231 |
'Successfully logged out', |
| 232 |
'Got correct log out content', |
| 233 |
); |
| 234 |
}; |
| 235 |
|
| 236 |
Please note that the request URL must include scheme and host for the call |
| 237 |
to L<HTTP::Cookies/add_cookie_header> to work. |
| 238 |
|
| 239 |
=head3 Accessing the configuration file |
| 240 |
|
| 241 |
By importing Dancer2 in the test files, there is full |
| 242 |
access to the configuration using the imported keywords: |
| 243 |
|
| 244 |
use strict; |
| 245 |
use warnings; |
| 246 |
use Test::More; |
| 247 |
use Plack::Test; |
| 248 |
use HTTP::Request::Common; |
| 249 |
use MyApp; |
| 250 |
use Dancer2; |
| 251 |
|
| 252 |
my $appname = config->{'appname'}; |
| 253 |
diag "Testing $appname"; |
| 254 |
|
| 255 |
# ... |
| 256 |
|
| 257 |
=head1 Plugins |
| 258 |
|
| 259 |
In order to test plugins, you can create an application on the spot, as |
| 260 |
part of the test script code, and use the plugin there. |
| 261 |
|
| 262 |
use strict; |
| 263 |
use warnings; |
| 264 |
|
| 265 |
use Test::More tests => 2; |
| 266 |
use Plack::Test; |
| 267 |
use HTTP::Request::Common; |
| 268 |
|
| 269 |
{ |
| 270 |
package MyTestApp; |
| 271 |
use Dancer2; |
| 272 |
use Dancer2::Plugin::MyPlugin; |
| 273 |
|
| 274 |
get '/' => sub { my_keyword }; |
| 275 |
} |
| 276 |
|
| 277 |
my $test = Plack::Test->create( MyTestApp->to_app ); |
| 278 |
my $res = $test->request( GET '/' ); |
| 279 |
|
| 280 |
ok( $res->is_success, 'Successful request' ); |
| 281 |
is( $res->content, 'MyPlugin-MyKeyword', 'Correct content' ); |
| 282 |
|
| 283 |
=head1 AUTHOR |
| 284 |
|
| 285 |
Dancer Core Developers |
| 286 |
|
| 287 |
=head1 COPYRIGHT AND LICENSE |
| 288 |
|
| 289 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 290 |
|
| 291 |
This is free software; you can redistribute it and/or modify it under |
| 292 |
the same terms as the Perl 5 programming language system itself. |
| 293 |
|
| 294 |
=cut |