File: 14_options.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 (53 lines) | stat: -rw-r--r-- 1,480 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
use Test::More;
use Dancer::Test;
use Dancer ':syntax', ':tests';
use Dancer::Route;

plan tests => 20;

{
    get( '/', { agent => 'foo' } => sub {'agent foo'} );

    get('/', { agent => 'bar' }, sub { 'agent bar'} );

    get( '/', sub {'all agents'} );

    get( '/foo', { agent => 'foo' } => sub {'foo only'} );

    get('/welcome', {agent => qr{Mozilla}} => sub { "hey Mozilla!" });

    get('/welcome' => sub { "hello" });
}

eval { get '/fail', { false_option => 42 } => sub { } };
like $@, qr/Not a valid option for route matching: `false_option'/, 
    "only supported options are allowed";

my @tests = (
    {method => 'GET', path => '/',    expected => 'agent foo', agent => 'foo'},
    {method => 'GET', path => '/',    expected => 'agent bar',  agent => 'bar'},
    {method => 'GET', path => '/',    expected => 'all agents'},

    {method => 'GET', path => '/foo', expected => 'foo only',  agent => 'foo'},

    {   method   => 'GET',
        path     => '/welcome',
        expected => 'hey Mozilla!',
        agent    => 'Mozilla/5.0 (X11; U; Linux x86_64; fr; rv:1.9.1.5)'
    },
    {method => 'GET', path => '/welcome', expected => 'hello'},

);

foreach my $test (@tests) {
    $ENV{HTTP_USER_AGENT} = $test->{agent} || undef;
    my $req = [$test->{method} => $test->{path}];

    route_exists $req;
    response_status_is  $req => 200;
    response_content_is $req, $test->{expected};
}

$ENV{HTTP_USER_AGENT} = 'bar';
route_doesnt_exist [GET => '/foo'];