File: 02_params.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 (75 lines) | stat: -rw-r--r-- 2,191 bytes parent folder | download | duplicates (6)
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
use Dancer ':syntax', ':tests';
use Dancer::Test;
use Test::More;

plan tests => 28;

# multiple token
{
    get '/:resource/:id.:format' => sub {
        [ params->{'resource'},
          params->{'id'},
          params->{'format'} ];
    };

    my $response = dancer_response(GET => '/user/42.json');
    ok( defined($response), "response found for '/user/42.json'" );

    is_deeply( $response->content, ['user', '42', 'json'],
    "params are parsed as expected" );
}

{
    ok( get( '/'               => sub { 'index' } ),        'first route set' );
    ok( get( '/hello/:name'    => sub { param 'name' } ), 'second route set' );
    ok( get( '/hello/:foo/bar' => sub { param 'foo' } ), 'third route set' );
    ok( post( '/new/:stuff'    => sub { param 'stuff'} ), 'post 1 route set' );
    ok( post( '/allo'          => sub { request->body } ),   'post 2 route set' );

    get '/opt/:name?/?:lastname?' => sub {
        [ params->{name}, params->{lastname} ];
    };

    my @tests = (
        { method => 'GET',  path => '/',              expected => 'index' },
        { method => 'GET',  path => '/hello/sukria',  expected => 'sukria' },
        { method => 'GET',  path => '/hello/joe/bar', expected => 'joe' },
        { method => 'POST', path => '/new/wine',      expected => 'wine' },

        {
            method   => 'GET',
            path     => '/opt/',
            expected => [ undef, undef ]
        },

        {
            method   => 'GET',
            path     => '/opt/placeholder',
            expected => [ 'placeholder', undef ]
        },

        {
            method   => 'GET',
            path     => '/opt/alexis/sukrieh',
            expected => [ "alexis", "sukrieh" ]
        },
    );

    foreach my $test (@tests) {
        my $req = [ $test->{method}, $test->{path} ];

        route_exists $req;

        if ( ref( $test->{expected} ) ) {
            response_content_is_deeply $req => $test->{expected};
        }
        else {
            response_content_is $req => $test->{expected};
        }

        # splat should not be set
        ok( !exists( params->{'splat'} ),
            "splat not defined for " . $test->{path} );
    }

}