1Dancer::Test(3)       User Contributed Perl Documentation      Dancer::Test(3)
2
3
4

NAME

6       Dancer::Test - Test helpers to test a Dancer application
7

VERSION

9       version 1.3512
10

SYNOPSIS

12           use strict;
13           use warnings;
14           use Test::More tests => 2;
15
16           use MyWebApp;
17           use Dancer::Test;
18
19           response_status_is [GET => '/'], 200, "GET / is found";
20           response_content_like [GET => '/'], qr/hello, world/, "content looks good for /";
21

DESCRIPTION

23       This module provides test helpers for testing Dancer apps.
24
25       Be careful, the module loading order in the example above is very
26       important.  Make sure to use "Dancer::Test" after importing the
27       application package otherwise your appdir will be automatically set to
28       "lib" and your test script won't be able to find views, conffiles and
29       other application content.
30
31       For all test methods, the first argument can be either an array ref of
32       the method and route, or a scalar containing the route (in which case
33       the method is assumed to be "GET"), or a Dancer::Response object.
34
35           # all 3 are equivalent
36           response_status_is [ GET => '/' ], 200, 'GET / status is ok';
37
38           response_status_is '/', 200, 'GET / status is ok';
39
40           my $resp = dancer_response GET => '/';
41           response_status_is $resp => 200, 'GET / status is ok';
42

METHODS

44   route_exists([$method, $path], $test_name)
45       Asserts that the given request matches a route handler in Dancer's
46       registry.
47
48           route_exists [GET => '/'], "GET / is handled";
49
50   route_doesnt_exist([$method, $path], $test_name)
51       Asserts that the given request does not match any route handler in
52       Dancer's registry.
53
54           route_doesnt_exist [GET => '/bogus_path'], "GET /bogus_path is not handled";
55
56   response_exists([$method, $path], $test_name)
57       Deprecated - Use response_status_isnt and check for status 404.
58
59       Asserts that a response is found for the given request (note that even
60       though a route for that path might not exist, a response can be found
61       during request processing, because of filters).
62
63           response_exists [GET => '/path_that_gets_redirected_to_home'],
64               "response found for unknown path";
65
66   response_doesnt_exist([$method, $path], $test_name)
67       Deprecated - Use response_status_is and check for status 404.
68
69       Asserts that no response is found when processing the given request.
70
71           response_doesnt_exist [GET => '/unknown_path'],
72               "response not found for unknown path";
73
74   response_status_is([$method, $path], $status, $test_name)
75       Asserts that Dancer's response for the given request has a status equal
76       to the one given.
77
78           response_status_is [GET => '/'], 200, "response for GET / is 200";
79
80   response_status_isnt([$method, $path], $status, $test_name)
81       Asserts that the status of Dancer's response is not equal to the one
82       given.
83
84           response_status_isnt [GET => '/'], 404, "response for GET / is not a 404";
85
86   response_content_is([$method, $path], $expected, $test_name)
87       Asserts that the response content is equal to the $expected string.
88
89           response_content_is [GET => '/'], "Hello, World",
90               "got expected response content for GET /";
91
92   response_content_isnt([$method, $path], $not_expected, $test_name)
93       Asserts that the response content is not equal to the $not_expected
94       string.
95
96           response_content_isnt [GET => '/'], "Hello, World",
97               "got expected response content for GET /";
98
99   response_content_is_deeply([$method, $path], $expected_struct, $test_name)
100       Similar to response_content_is(), except that if response content and
101       $expected_struct are references, it does a deep comparison walking each
102       data structure to see if they are equivalent.
103
104       If the two structures are different, it will display the place where
105       they start differing.
106
107           response_content_is_deeply [GET => '/complex_struct'],
108               { foo => 42, bar => 24},
109               "got expected response structure for GET /complex_struct";
110
111   response_content_like([$method, $path], $regexp, $test_name)
112       Asserts that the response content for the given request matches the
113       regexp given.
114
115           response_content_like [GET => '/'], qr/Hello, World/,
116               "response content looks good for GET /";
117
118   response_content_unlike([$method, $path], $regexp, $test_name)
119       Asserts that the response content for the given request does not match
120       the regexp given.
121
122           response_content_unlike [GET => '/'], qr/Page not found/,
123               "response content looks good for GET /";
124
125   response_headers_are_deeply([$method, $path], $expected, $test_name)
126       Asserts that the response headers data structure equals the one given.
127
128           response_headers_are_deeply [GET => '/'], [ 'X-Powered-By' => 'Dancer 1.150' ];
129
130   response_headers_include([$method, $path], $expected, $test_name)
131       Asserts that the response headers data structure includes some of the
132       defined ones.
133
134           response_headers_include [GET => '/'], [ 'Content-Type' => 'text/plain' ];
135
136   response_redirect_location_is([$method, $path], $expected, $test_name)
137       Asserts that the location header sent with a 302 redirect is equal to
138       the $expected location.
139
140           response_redirect_location_is [GET => '/'], 'http://localhost/index.html';
141
142   response_redirect_location_like([$method, $path], $regexp, $test_name)
143       Asserts that the location header sent with a 302 redirect matches the
144       $regexp provided. Useful if the redirect location includes a query
145       string.
146
147           response_redirect_location_like [GET => '/'], qr/some_pattern/;
148
149   dancer_response($method, $path, { params => $params, body => $body, headers
150       => $headers, files => [{filename => '/path/to/file', name =>
151       'my_file'}] })
152       Returns a Dancer::Response object for the given request.
153
154       Only $method and $path are required.
155
156       $params is a hashref, $body can be a string or a hashref and $headers
157       can be an arrayref or a HTTP::Headers object, $files is an arrayref of
158       hashref, containing some files to upload.
159
160       $params always populates the query string, even for POST requests.
161       $body always populates the request body.
162
163       Currently, Dancer::Test cannot cope with both body and files passed in
164       the same call.
165
166       A good reason to use this function is for testing POST requests. Since
167       POST requests may not be idempotent, it is necessary to capture the
168       content and status in one shot. Calling the response_status_is and
169       response_content_is functions in succession would make two requests,
170       each of which could alter the state of the application and cause
171       Schrodinger's cat to die.
172
173           my $response = dancer_response POST => '/widgets';
174           is $response->{status}, 202, "response for POST /widgets is 202";
175           is $response->{content}, "Widget #1 has been scheduled for creation",
176               "response content looks good for first POST /widgets";
177
178           $response = dancer_response POST => '/widgets';
179           is $response->{status}, 202, "response for POST /widgets is 202";
180           is $response->{content}, "Widget #2 has been scheduled for creation",
181               "response content looks good for second POST /widgets";
182
183       It's possible to test file uploads:
184
185           post '/upload' => sub { return upload('image')->content };
186
187           $response = dancer_response(POST => '/upload', {files => [{name => 'image', filename => '/path/to/image.jpg'}]});
188
189       In addition, you can supply the file contents as the "data" key:
190
191           my $data  = 'A test string that will pretend to be file contents.';
192           $response = dancer_response(POST => '/upload', {
193               files => [{name => 'test', filename => "filename.ext", data => $data}]
194           });
195
196   read_logs
197           my $logs = read_logs;
198
199       Returns an array ref of all log messages issued by the app since the
200       last call to "read_logs".
201
202       For example:
203
204           warning "Danger!  Warning!";
205           debug   "I like pie.";
206
207           is_deeply read_logs, [
208               { level => "warning", message => "Danger!  Warning!" },
209               { level => "debug",   message => "I like pie.", }
210           ];
211
212           error "Put out the light.";
213
214           is_deeply read_logs, [
215               { level => "error", message => "Put out the light." },
216           ];
217
218       See Dancer::Logger::Capture for more details.
219

LICENSE

221       This module is free software and is distributed under the same terms as
222       Perl itself.
223

AUTHOR

225       This module has been written by Alexis Sukrieh <sukria@sukria.net>
226

SEE ALSO

228       Test::More
229

AUTHOR

231       Dancer Core Developers
232
234       This software is copyright (c) 2010 by Alexis Sukrieh.
235
236       This is free software; you can redistribute it and/or modify it under
237       the same terms as the Perl 5 programming language system itself.
238
239
240
241perl v5.30.0                      2019-07-26                   Dancer::Test(3)
Impressum