File: 04_routes_matching.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 (189 lines) | stat: -rw-r--r-- 5,224 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
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
use Dancer ':syntax';
use Dancer::Test;
use Test::More tests => 47, import => ['!pass'];

# regexps
{

    get qr{/hello/([\w]+)} => sub { [splat] };
    get qr{/show/([\d]+)}  => sub { [splat] };
    get qr{/post/([\w\d\-\.]+)/#comment([\d]+)} => sub { [splat] };

    my @tests = (
        {
            path     => '/hello/sukria',
            expected => ['sukria']
        },

        {
            path     => '/show/245',
            expected => ['245']
        },

        {
            path     => '/post/this-how-to-write-smart-webapp/#comment412',
            expected => [ 'this-how-to-write-smart-webapp', '412' ]
        },
    );

    foreach my $test (@tests) {
        my $handle;
        my $path     = $test->{path};
        my $expected = $test->{expected};

        my $request = [ GET => $path ];

        response_status_is         $request => 200,
          "route handler found for path `$path'";
        response_content_is_deeply $request => $expected,
          "match data for path `$path' looks good";
    }

    response_status_is [GET => '/no/hello/bar'] => 404;
}

# passing
{
    get '/say/:char' => sub {
        pass and return false if length( params->{char} ) > 1;
        "char: " . params->{char};
    };

    get '/say/:number' => sub {
        pass and return false if params->{number} !~ /^\d+$/;
        "number: " . params->{number};
    };

    get qr{/say/_(.*)} => sub {
        "underscore: " . params->{splat}[0];
    };

    get '/say/:word' => sub {
        pass and return false if params->{word} =~ /trash/;
        "word: " . params->{word};
    };

    get '/say/*' => sub {
        "trash: " . params->{splat}[0];
    };

    get '/foo/' => sub { pass };

    my @tests = (
        { path => '/say/A',           expected => 'char: A' },
        { path => '/say/24',          expected => 'number: 24' },
        { path => '/say/B',           expected => 'char: B' },
        { path => '/say/Perl',        expected => 'word: Perl' },
        { path => '/say/_stuff',      expected => 'underscore: stuff' },
        { path => '/say/go_to_trash', expected => 'trash: go_to_trash' },
    );

    foreach my $test (@tests) {
        my $path     = $test->{path};
        my $expected = $test->{expected};

        response_status_is [ GET => $path ] => 200,
          "route found for path `$path'";
        response_content_is_deeply [ GET => $path ] => $expected,
          "match data for path `$path' looks good";
    }

    response_status_is [ GET => '/foo' ] => 404,
      "Pass over the last match is 404";
}

# wildcards
{
    my @paths =
      ( '/hi/*', '/hi/*/welcome/*', '/download/*.*', '/optional/?*?' );

    my @tests = (
        {
            path     => '/hi/sukria',
            expected => ['sukria']
        },

        {
            path     => '/hi/alexis/welcome/sukrieh',
            expected => [ 'alexis', 'sukrieh' ]
        },

        {
            path     => '/download/wolverine.pdf',
            expected => [ 'wolverine', 'pdf' ]
        },

        { path => '/optional/alexis', expected => ['alexis'] },
        { path => '/optional/',       expected => [] },
        { path => '/optional',        expected => [] },
    );

    my $nb_tests = ( scalar(@paths) ) + ( scalar(@tests) * 2 );

    get( $_ => sub { [splat] } ) for @paths;

    foreach my $test (@tests) {
        my $path     = $test->{path};
        my $expected = $test->{expected};

        my $response = dancer_response( GET => $path );

        ok( defined($response), "route handler found for path `$path'" );
        is_deeply( $response->content, $expected,
            "match data for path `$path' looks good" );
    }
}

# any routes handler
{
    any [ 'get', 'delete' ] => '/any_1' => sub { "any_1"; };
    any '/any_2' => sub { "any_2"; };

    eval {
        any 'get' => '/any_1' => sub {
            "any_1";
        };
    };
    like $@, qr/Syntax error, methods should be provided as an ARRAY ref/,
      "syntax error caught";

    my @routes = (
        {
            methods  => [ 'get', 'delete' ],
            path     => '/any_1',
            expected => 'any_1',
        },
        {
            methods  => [ 'get', 'delete', 'post', 'put' ],
            path     => '/any_2',
            expected => 'any_2',
        }
    );

    # making sure response are OK
    foreach my $route (@routes) {
        foreach my $method ( @{ $route->{methods} } ) {
            my $response = dancer_response( $method => $route->{path} );
            ok( defined($response),
                "route handler found for method $method, path $route->{path}");
            is $response->content, $route->{expected}, "response content is ok";
        }
    }

    # making sure 404 are thrown for unspecified routes
    my @failed = (
        {
            methods => [ 'post', 'put' ],
            path    => '/any_1',
        },
    );

    foreach my $route (@failed) {
        foreach my $method ( @{ $route->{methods} } ) {
            my $response = dancer_response( $method => $route->{path} );
            is( $response->status, 404,
                "route handler not found for method $method, path "
                  . $route->{path} );
        }
    }
}