1Dancer2::Manual::TestinUgs(e3r)Contributed Perl DocumentDaatnicoenr2::Manual::Testing(3)
2
3
4

NAME

6       Dancer2::Manual::Testing - Writing tests for Dancer2
7

VERSION

9       version 0.301004
10

Basic application testing

12       Since Dancer2 produces PSGI applications, you can easily write tests
13       using Plack::Test and provide your Dancer application as the app for
14       testing.
15
16       A basic test (which we also scaffold with dancer2) looks like this:
17
18           use strict;
19           use warnings;
20
21           use Test::More tests => 4;
22           use Plack::Test;
23           use HTTP::Request::Common;
24
25           use_ok('MyApp');
26
27           # create an application
28           my $app = MyApp->to_app;
29           isa_ok( $app, 'CODE' );
30
31           # create a testing object
32           my $test = Plack::Test->create($app);
33
34           # now you can call requests on it and get responses
35           # requests are of HTTP::Request
36           # responses are of HTTP::Response
37
38           # "GET" from HTTP::Request::Common creates an HTTP::Request object
39           my $response = $test->request( GET '/' );
40
41           # same as:
42           # my $response = $test->request( HTTP::Request->new( GET => '/' ) );
43
44           ok( $response->is_success, 'Successful request' );
45           is( $response->content, 'OK', 'Correct response content' );
46
47       Read the documentation for HTTP::Request and HTTP::Request::Common to
48       see the different options for sending parameters.
49

Cookies

51       If you don't want to use an entire user agent for this test, you can
52       use HTTP::Cookies to store cookies and then retrieve them:
53
54           use strict;
55           use warnings;
56
57           use Test::More tests => 3;
58           use Plack::Test;
59           use HTTP::Request::Common;
60           use HTTP::Cookies;
61
62           use_ok('MyApp');
63
64           my $url  = 'http://localhost';
65           my $jar  = HTTP::Cookies->new();
66           my $test = Plack::Test->create( MyApp->to_app );
67
68           subtest 'Create session' => sub {
69               my $res = $test->request( GET "$url/login" );
70               ok( $res->is_success, 'Successful login' );
71
72               # extract cookies from the response and store in the jar
73               $jar->extract_cookies($res);
74           };
75
76           subtest 'Check session' => sub {
77               my $req = GET "$url/logout";
78
79               # add cookies to the request
80               $jar->add_cookie_header($req);
81
82               my $res = $test->request($req);
83               ok( $res->is_success, 'Successful logout' );
84               like(
85                   $res->content,
86                   'Successfully logged out',
87                   'Got correct log out content',
88               );
89           };
90
91       Please note that the request URL must include scheme and host for the
92       call to "add_cookie_header" in HTTP::Cookies to work.
93

Plugins

95       In order to test plugins, you can create an application on the spot, as
96       part of the test script code, and use the plugin there.
97
98           use strict;
99           use warnings;
100
101           use Test::More tests => 2;
102           use Plack::Test;
103           use HTTP::Request::Common;
104
105           {
106               package MyTestApp;
107               use Dancer2;
108               use Dancer2::Plugin::MyPlugin;
109
110               get '/' => sub { my_keyword };
111           }
112
113           my $test = Plack::Test->create( MyTestApp->to_app );
114           my $res  = $test->request( GET '/' );
115
116           ok( $res->is_success, 'Successful request' );
117           is( $res->content, 'MyPlugin-MyKeyword', 'Correct content' );
118

AUTHOR

120       Dancer Core Developers
121
123       This software is copyright (c) 2021 by Alexis Sukrieh.
124
125       This is free software; you can redistribute it and/or modify it under
126       the same terms as the Perl 5 programming language system itself.
127
128
129
130perl v5.34.0                      2022-01-21       Dancer2::Manual::Testing(3)
Impressum