.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Dancer2::Manual::Testing 3pm" .TH Dancer2::Manual::Testing 3pm "2016-12-23" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Dancer2::Manual::Testing \- Writing tests for Dancer2 .SH "VERSION" .IX Header "VERSION" version 0.204002 .SH "Basic application testing" .IX Header "Basic application testing" Since Dancer2 produces \s-1PSGI\s0 applications, you can easily write tests using Plack::Test and provide your Dancer application as the app for testing. .PP A basic test (which we also scaffold with dancer2) looks like this: .PP .Vb 2 \& use strict; \& use warnings; \& \& use Test::More tests => 4; \& use Plack::Test; \& use HTTP::Request::Common; \& \& use_ok(\*(AqMyApp\*(Aq); \& \& # create an application \& my $app = MyApp\->to_app; \& isa_ok( $app, \*(AqCODE\*(Aq ); \& \& # create a testing object \& my $test = Plack::Test\->create($app); \& \& # now you can call requests on it and get responses \& # requests are of HTTP::Request \& # responses are of HTTP::Response \& \& # "GET" from HTTP::Request::Common creates an HTTP::Request object \& my $response = $test\->request( GET \*(Aq/\*(Aq ); \& \& # same as: \& # my $response = $test\->request( HTTP::Request\->new( GET => \*(Aq/\*(Aq ) ); \& \& ok( $response\->is_success, \*(AqSuccessful request\*(Aq ); \& is( $response\->content, \*(AqOK\*(Aq, \*(AqCorrect response content\*(Aq ); .Ve .PP Read the documentation for HTTP::Request and HTTP::Request::Common to see the different options for sending parameters. .SH "Cookies" .IX Header "Cookies" If you don't want to use an entire user agent for this test, you can use HTTP::Cookies to store cookies and then retrieve them: .PP .Vb 2 \& use strict; \& use warnings; \& \& use Test::More tests => 3; \& use Plack::Test; \& use HTTP::Request::Common; \& use HTTP::Cookies; \& \& use_ok(\*(AqMyApp\*(Aq); \& \& my $url = \*(Aqhttp://localhost\*(Aq; \& my $jar = HTTP::Cookies\->new(); \& my $test = Plack::Test\->create( MyApp\->to_app ); \& \& subtest \*(AqCreate session\*(Aq => sub { \& my $res = $test\->request( GET "$url/login" ); \& ok( $res\->is_success, \*(AqSuccessful login\*(Aq ); \& \& # extract cookies from the response and store in the jar \& $jar\->extract_cookies($res); \& }; \& \& subtest \*(AqCheck session\*(Aq => sub { \& my $req = GET "$url/logout"; \& \& # add cookies to the request \& $jar\->add_cookie_header($req); \& \& my $res = $test\->request($req); \& ok( $res\->is_success, \*(AqSuccessful logout\*(Aq ); \& like( \& $res\->content, \& \*(AqSuccessfully logged out\*(Aq, \& \*(AqGot correct log out content\*(Aq, \& ); \& }; .Ve .PP Please note that the request \s-1URL\s0 must include scheme and host for the call to \*(L"add_cookie_header\*(R" in HTTP::Cookies to work. .SH "Plugins" .IX Header "Plugins" In order to test plugins, you can create an application on the spot, as part of the test script code, and use the plugin there. .PP .Vb 2 \& use strict; \& use warnings; \& \& use Test::More tests => 2; \& use Plack::Test; \& use HTTP::Request::Common; \& \& { \& package MyTestApp; \& use Dancer2; \& use Dancer2::Plugin::MyPlugin; \& \& get \*(Aq/\*(Aq => sub { my_keyword }; \& } \& \& my $test = Plack::Test\->create( MyTestApp\->to_app ); \& my $res = $test\->request( GET \*(Aq/\*(Aq ); \& \& ok( $res\->is_success, \*(AqSuccessful request\*(Aq ); \& is( $res\->content, \*(AqMyPlugin\-MyKeyword\*(Aq, \*(AqCorrect content\*(Aq ); .Ve .SH "AUTHOR" .IX Header "AUTHOR" Dancer Core Developers .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2016 by Alexis Sukrieh. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.