forked from PerlDancer/Dancer
/
Test.pm
408 lines (286 loc) · 11.4 KB
/
Test.pm
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package Dancer::Test;
# test helpers for Dancer apps
use strict;
use warnings;
use Test::More import => ['!pass'];
use Carp;
use HTTP::Headers;
use Dancer ':syntax';
use Dancer::App;
use Dancer::Request;
use Dancer::SharedData;
use Dancer::Renderer;
use Dancer::Config;
use base 'Exporter';
use vars '@EXPORT';
@EXPORT = qw(
route_exists
route_doesnt_exist
response_exists
response_doesnt_exist
response_status_is
response_status_isnt
response_content_is
response_content_isnt
response_content_is_deeply
response_content_like
response_content_unlike
response_is_file
response_headers_are_deeply
dancer_response
get_response
);
sub import {
my ($class, %options) = @_;
$options{appdir} ||= '..';
# mimic PSGI env
$ENV{SERVERNAME} = 'localhost';
$ENV{HTTP_HOST} = 'localhost';
$ENV{SERVER_PORT} = 80;
$ENV{'psgi.url_scheme'} = 'http';
my ($package, $script) = caller;
$class->export_to_level(1, $class, @EXPORT);
# set a default session engine for tests
setting 'session' => 'simple';
Dancer::_init($options{appdir});
Dancer::Config->load;
}
# Route Registry
sub route_exists {
my ($req, $test_name) = @_;
my ($method, $path) = @$req;
$test_name ||= "a route exists for $method $path";
$req = Dancer::Request->new_for_request($method => $path);
ok(Dancer::App->find_route_through_apps($req), $test_name);
}
sub route_doesnt_exist {
my ($req, $test_name) = @_;
my ($method, $path) = @$req;
$test_name ||= "no route exists for $method $path";
$req = Dancer::Request->new_for_request($method => $path);
ok(!defined(Dancer::App->find_route_through_apps($req)), $test_name);
}
# Response status
sub response_exists {
my ($req, $test_name) = @_;
$test_name ||= "a response is found for @$req";
my $response = dancer_response(@$req);
ok(defined($response), $test_name);
}
sub response_doesnt_exist {
my ($req, $test_name) = @_;
$test_name ||= "no response found for @$req";
my $response = dancer_response(@$req);
ok(!defined($response), $test_name);
}
sub response_status_is {
my ($req, $status, $test_name) = @_;
$test_name ||= "response status is $status for @$req";
my $response = dancer_response(@$req);
is $response->{status}, $status, $test_name;
}
sub response_status_isnt {
my ($req, $status, $test_name) = @_;
$test_name ||= "response status is not $status for @$req";
my $response = dancer_response(@$req);
isnt $response->{status}, $status, $test_name;
}
# Response content
sub response_content_is {
my ($req, $matcher, $test_name) = @_;
$test_name ||= "response content looks good for @$req";
my $response = dancer_response(@$req);
is $response->{content}, $matcher, $test_name;
}
sub response_content_isnt {
my ($req, $matcher, $test_name) = @_;
$test_name ||= "response content looks good for @$req";
my $response = dancer_response(@$req);
isnt $response->{content}, $matcher, $test_name;
}
sub response_content_like {
my ($req, $matcher, $test_name) = @_;
$test_name ||= "response content looks good for @$req";
my $response = dancer_response(@$req);
like $response->{content}, $matcher, $test_name;
}
sub response_content_unlike {
my ($req, $matcher, $test_name) = @_;
$test_name ||= "response content looks good for @$req";
my $response = dancer_response(@$req);
unlike $response->{content}, $matcher, $test_name;
}
sub response_content_is_deeply {
my ($req, $matcher, $test_name) = @_;
$test_name ||= "response content looks good for @$req";
my $response = dancer_response(@$req);
is_deeply $response->{content}, $matcher, $test_name;
}
sub response_is_file {
my ($req, $test_name) = @_;
$test_name ||= "a file is returned for @$req";
my $response = _get_file_response($req);
ok(defined($response), $test_name);
}
sub response_headers_are_deeply {
my ($req, $expected, $test_name) = @_;
$test_name ||= "headers are as expected for @$req";
my $response = dancer_response(@$req);
is_deeply($response->headers_to_array, $expected, $test_name);
}
sub dancer_response {
my ($method, $path, $args) = @_;
$args ||= {};
if ($method =~ /^(?:PUT|POST)$/ && $args->{body}) {
my $body = $args->{body};
my $l = length $body;
open my $in, '<', \$body;
$ENV{'CONTENT_LENGTH'} = $l;
$ENV{'psgi.input'} = $in;
}
my ($params, $body, $headers) = @$args{qw(params body headers)};
if ($headers and (my @headers = @$headers)) {
while (my $h = shift @headers) {
if ($h =~ /content-type/i) {
$ENV{'CONTENT_TYPE'} = shift @headers;
}
}
}
my $request = Dancer::Request->new_for_request(
$method => $path,
$params, $body, HTTP::Headers->new(@$headers)
);
Dancer::SharedData->request($request);
return Dancer::Renderer::get_action_response();
}
sub get_response {
carp "get_response() is DEPRECATED. Use dancer_response() instead.";
return dancer_response(@{$_[0]});
}
# private
sub _get_file_response {
my ($req) = @_;
my ($method, $path, $params) = @$req;
my $request = Dancer::Request->new_for_request($method => $path, $params);
Dancer::SharedData->request($request);
return Dancer::Renderer::get_file_response();
}
sub _get_handler_response {
my ($req) = @_;
my ($method, $path, $params) = @$req;
my $request = Dancer::Request->new_for_request($method => $path, $params);
return Dancer::Handler->handle_request($request);
}
1;
__END__
=pod
=head1 NAME
Dancer::Test - Test helpers to test a Dancer application
=head1 SYNOPSYS
use strict;
use warnings;
use Test::More tests => 2;
use MyWebApp;
use Dancer::Test appdir => '..';
response_status_is [GET => '/'], 200, "GET / is found";
response_content_like [GET => '/'], qr/hello, world/, "content looks good for /";
=head1 DESCRIPTION
This module provides test heplers for testing Dancer apps.
=head1 CONFIGURATON
When importing Dancer::Test, the appdir is set by defaut to '..', assuming that
your test script is directly in your t/ directory. If you put your test script
deeper in the 't/' hierarchy (like in 't/routes/01_some_test.t') you'll have to
tell Dancer::Test that the appdir is one step upper.
To do so, you can tell where the appdir is thanks to an import option:
use MyWebApp;
use Dancer::Test appdir => '../..';
Be careful, the order in the example above is very important.
Make sure to use C<Dancer::Test> B<after> importing the application package
otherwise your appdir will be automatically set to C<lib> and your test script
won't be able to find views, conffiles and other application content.
=head1 METHODS
=head2 route_exists([$method, $path], $test_name)
Asserts that the given request matches a route handler in Dancer's
registry.
route_exists [GET => '/'], "GET / is handled";
=head2 route_doesnt_exist([$method, $path], $test_name)
Asserts that the given request does not match any route handler
in Dancer's registry.
route_doesnt_exist [GET => '/bogus_path'], "GET /bogus_path is not handled";
=head2 response_exists([$method, $path], $test_name)
Asserts that a response is found for the given request (note that even though
a route for that path might not exist, a response can be found during request
processing, because of filters).
response_exists [GET => '/path_that_gets_redirected_to_home'],
"response found for unknown path";
=head2 response_doesnt_exist([$method, $path], $test_name)
Asserts that no response is found when processing the given request.
response_doesnt_exist [GET => '/unknown_path'],
"response not found for unknown path";
=head2 response_status_is([$method, $path], $status, $test_name)
Asserts that Dancer's response for the given request has a status equal to the
one given.
response_status_is [GET => '/'], 200, "response for GET / is 200";
=head2 response_status_isnt([$method, $path], $status, $test_name)
Asserts that the status of Dancer's response is not equal to the
one given.
response_status_isnt [GET => '/'], 404, "response for GET / is not a 404";
=head2 response_content_is([$method, $path], $expected, $test_name)
Asserts that the response content is equal to the C<$expected> string.
response_content_is [GET => '/'], "Hello, World",
"got expected response content for GET /";
=head2 response_content_isnt([$method, $path], $not_expected, $test_name)
Asserts that the response content is not equal to the C<$not_expected> string.
response_content_is [GET => '/'], "Hello, World",
"got expected response content for GET /";
=head2 response_content_is_deeply([$method, $path], $expected_struct, $test_name)
Similar to response_content_is(), except that if response content and
$expected_struct are references, it does a deep comparison walking each data
structure to see if they are equivalent.
If the two structures are different, it will display the place where they start
differing.
response_content_is_deeply [GET => '/complex_struct'],
{ foo => 42, bar => 24},
"got expected response structure for GET /complex_struct";
=head2 response_content_like([$method, $path], $regexp, $test_name)
Asserts that the response content for the given request matches the regexp
given.
response_content_like [GET => '/'], qr/Hello, World/,
"response content looks good for GET /";
=head2 response_content_unlike([$method, $path], $regexp, $test_name)
Asserts that the response content for the given request does not match the regexp
given.
response_content_unlike [GET => '/'], qr/Page not found/,
"response content looks good for GET /";
=head2 response_headers_are_deeply([$method, $path], $expected, $test_name)
Asserts that the response headers data structure equals the one given.
response_headers_are_deeply [GET => '/'], [ 'X-Powered-By' => 'Dancer 1.150' ];
=head2 dancer_response($method, $path, { params => $params, body => $body, headers => $headers })
Returns a Dancer::Response object for the given request.
Only $method and $path are required.
$params is a hashref, $body is a string and $headers can be an arrayref or
a HTTP::Headers object.
A good reason to use this function is for
testing POST requests. Since POST requests may not be idempotent, it is
necessary to capture the content and status in one shot. Calling the
response_status_is and response_content_is functions in succession would make
two requests, each of which could alter the state of the application and cause
Schrodinger's cat to die.
my $response = dancer_response POST => '/widgets';
is $response->{status}, 202, "response for POST /widgets is 202";
is $response->{content}, "Widget #1 has been scheduled for creation",
"response content looks good for first POST /widgets";
$response = dancer_response POST => '/widgets';
is $response->{status}, 202, "response for POST /widgets is 202";
is $response->{content}, "Widget #2 has been scheduled for creation",
"response content looks good for second POST /widgets";
=head2 get_response([$method, $path])
This method is B<DEPRECATED>. Use dancer_response() instead.
=head1 LICENSE
This module is free software and is distributed under the same terms as Perl
itself.
=head1 AUTHOR
This module has been written by Alexis Sukrieh <sukria@sukria.net>
=head1 SEE ALSO
L<Test::More>
=cut