File: 02_before.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 (72 lines) | stat: -rw-r--r-- 1,537 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
use strict;
use warnings;
use Test::More import => ['!pass'];

use Dancer ':syntax';
use Dancer::Test;

plan tests => 15;

my $i = 0;

ok(
   hook (before =>
        sub {
            content_type('text/xhtml');
        }
    )
);

ok(
   hook(before=>
        sub {
            if ( request->path_info eq '/redirect_from' ) {
                redirect('/redirect_to');
            }
            else {
                params->{number} = 42;
                var notice => "I am here";
                request->path_info('/');
            }
        }
    ),
    'before filter is defined'
);

get(
    '/' => sub {
        is( params->{number}, 42,             "params->{number} is set" );
        is( "I am here",      vars->{notice}, "vars->{notice} is set" );
        return 'index';
    }
);

get(
    '/redirect_from' => sub {
        $i++;
    }
);

route_exists       [ GET => '/' ];
response_status_is [ GET => '/' ] => 200;

my $path = '/somewhere';
my $request = [ GET => $path ];

route_doesnt_exist $request, "there is no route handler for $path...";

response_status_is $request => 200, "...but a response is returned though";

response_content_is $request, 'index',
  "which is the result of a redirection to /";

response_headers_are_deeply [ GET => '/redirect_from' ],
  [
    'Location'     => 'http://localhost/redirect_to',
    'Content-Type' => 'text/xhtml',
    'Server'       => "Perl Dancer ${Dancer::VERSION}",
    'X-Powered-By' => "Perl Dancer ${Dancer::VERSION}",
  ];

is $i, 0, 'never gone to redirect_from';