summaryrefslogtreecommitdiff
path: root/lib/Dancer2/Manual/Testing.pod
blob: 5d0f8d480b2c731b4c6cf74ff2ab0ff6a530deab (plain)
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
package Dancer2::Manual::Testing;
# ABSTRACT: Writing tests for Dancer2

use strict;
use warnings;

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

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

=head1 VERSION

version 1.0.0

=head1 Basic application testing

Since L<Dancer2> produces PSGI applications, you can easily write tests using
L<Plack::Test> and provide your Dancer application as the app for testing.

A basic test (which we also scaffold with L<dancer2>) looks like this:

    use strict;
    use warnings;

    use Test::More tests => 4;
    use Plack::Test;
    use HTTP::Request::Common;

    use_ok('MyApp');

    # create an application
    my $app = MyApp->to_app;
    isa_ok( $app, 'CODE' );

    # 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 '/' );

    # same as:
    # my $response = $test->request( HTTP::Request->new( GET => '/' ) );

    ok( $response->is_success, 'Successful request' );
    is( $response->content, 'OK', 'Correct response content' );

Read the documentation for L<HTTP::Request> and L<HTTP::Request::Common> to
see the different options for sending parameters.

=head1 Cookies

If you don't want to use an entire user agent for this test, you can use
L<HTTP::Cookies> to store cookies and then retrieve them:

    use strict;
    use warnings;

    use Test::More tests => 3;
    use Plack::Test;
    use HTTP::Request::Common;
    use HTTP::Cookies;

    use_ok('MyApp');

    my $url  = 'http://localhost';
    my $jar  = HTTP::Cookies->new();
    my $test = Plack::Test->create( MyApp->to_app );

    subtest 'Create session' => sub {
        my $res = $test->request( GET "$url/login" );
        ok( $res->is_success, 'Successful login' );

        # extract cookies from the response and store in the jar
        $jar->extract_cookies($res);
    };

    subtest 'Check session' => 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, 'Successful logout' );
        like(
            $res->content,
            'Successfully logged out',
            'Got correct log out content',
        );
    };

Please note that the request URL must include scheme and host for the call
to L<HTTP::Cookies/add_cookie_header> to work.

=head1 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.

    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 '/' => sub { my_keyword };
    }

    my $test = Plack::Test->create( MyTestApp->to_app );
    my $res  = $test->request( GET '/' );

    ok( $res->is_success, 'Successful request' );
    is( $res->content, 'MyPlugin-MyKeyword', 'Correct content' );

=head1 AUTHOR

Dancer Core Developers

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut