Debian Codegraph @

t/named_routes.t

raw
1 use strict;
2 use warnings;
3 use Test::More 'tests' => 5;
4 use Plack::Test;
5 use HTTP::Request::Common;
6
7 {
8 package MyApp;
9
10 use Dancer2;
11
12 # Name, Regexp, Code
13 get 'view_static', '/view' => sub {
14 'View Static';
15 };
16
17 get 'view_regex', qr{^/view_r$} => sub {
18 'View Regex'
19 };
20
21 # Name, Regexp, Options, Code
22 get 'base_static', '/' => { 'user_agent' => 'XX' }, sub {
23 'Base Static';
24 };
25
26 # Name, Regexp, Options, Code
27 get 'base_regex', qr{^/r$}, {}, sub {
28 'Base Regex';
29 };
30
31 get '/ignore1' => sub {1};
32 get '/ignore2' => sub {1};
33 get '/ignore3' => sub {1};
34 }
35
36 my $test = Plack::Test->create( MyApp->to_app );
37
38 subtest 'Named static route' => sub {
39 plan 'tests' => 2;
40
41 my $response = $test->request( GET '/view' );
42 ok( $response->is_success, 'Successfully reached /view' );
43 is( $response->content, 'View Static', 'Static route with name' );
44 };
45
46 subtest 'Named regex route' => sub {
47 plan 'tests' => 2;
48
49 my $response = $test->request( GET '/view_r' );
50 ok( $response->is_success, 'Successfully reached /view_r' );
51 is( $response->content, 'View Regex', 'Regex route with name' );
52 };
53
54 subtest 'Named static route with options' => sub {
55 plan 'tests' => 2;
56
57 my $response = $test->request( GET '/', 'User-Agent' => 'XX' );
58 ok( $response->is_success, 'Successfully reached /' );
59 is($response->content, 'Base Static',
60 'Static route with name and options');
61 };
62
63 subtest 'Named regex route with options' => sub {
64 plan 'tests' => 2;
65
66 my $response = $test->request( GET '/r', 'User-Agent' => 'XX' );
67 ok( $response->is_success, 'Successfully reached /r' );
68 is($response->content, 'Base Regex', 'Regex route with name and options');
69 };
70
71 subtest 'Route objects' => sub {
72 plan 'tests' => 3;
73
74 my @apps = @{ Dancer2::runner->apps };
75 is( scalar @apps, 1, 'Only one app exists' );
76
77 my %routes = %{ $apps[0]->route_names() };
78 is( scalar keys %routes, 4, 'Four named routes registered' );
79
80 is_deeply(
81 [ sort keys %routes ],
82 [
83 'base_regex',
84 'base_static',
85 'view_regex',
86 'view_static',
87 ],
88 'All the right route names',
89 );
90 };