File: 03_routes_api.t

package info (click to toggle)
libdancer-perl 1.3521%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,460 kB
  • sloc: perl: 7,436; xml: 2,211; sh: 54; makefile: 32; sql: 5
file content (71 lines) | stat: -rw-r--r-- 1,952 bytes parent folder | download | duplicates (4)
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
use Dancer ':syntax';
use Dancer::Test;
use Test::More 'tests' => 8, import => ['!pass'];

use Dancer::Route;

my $r = Dancer::Route->new(
    method => 'get',
    pattern => '/:var',
    code => sub {
        params->{'var'};
    },
);

isa_ok $r => 'Dancer::Route';

is $r->method  => 'get',   "method is 'get'";
is $r->pattern => '/:var', "pattern is '/:var'";

my $req = Dancer::Request->new_for_request(GET => '/42');
my $expected_match = { var => 42 };
my $match = $r->match($req);
is_deeply $match => $expected_match, "route matched GET /42";

$r->match_data($match);
Dancer::SharedData->request($req);
my $response = $r->run($req);
is $response->{content} => 42, "response looks good";

my $r2 = Dancer::Route->new(method => 'get',
    pattern => '/pass/:var',
    code => sub { pass;
                  # The next line is not executed, as 'pass' breaks the route workflow
                  die },
    prev => $r);

my $r3 = Dancer::Route->new(method => 'get',
    pattern => '/other/path',
    code => sub { "this is r3" },
    prev => $r2);


my $r4 = Dancer::Route->new(method => 'get',
    pattern => '/pass/:var',
    code => sub { "this is r4" },
    prev => $r3);

$req = Dancer::Request->new_for_request(GET => '/pass/42');
$expected_match = { var => 42 };
$match = $r2->match($req);
is_deeply $match => $expected_match, "route matched GET /42";

$r2->match_data($match);
Dancer::SharedData->request($req);
$r2->run($req);

$response = Dancer::SharedData->response;
is $response->{content} => 'this is r4',
    "route 2 passed, r3 skipped (don't match), r4 served the response";

setting 'public' => 't/03_route_handler/public';

my $r5 = Dancer::Route->new(
    method  => 'get',
    pattern => '/error',
    code    => sub { send_error( "no", 404 ) }
);
$req = Dancer::Request->new_for_request( GET => '/error' );
my $res = $r5->run($req);
is( ( grep { /Content-Type/ } @{ $res->headers_to_array } ),
    1, 'only one content-type' );