Debian Codegraph @

t/classes/Dancer2-Core-Runner/psgi_app.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests => 25;
4 use Plack::Test;
5 use HTTP::Request::Common;
6
7 { package App1; use Dancer2; get '/1' => sub {1}; }
8 { package App2; use Dancer2; get '/2' => sub {2}; }
9 { package App3; use Dancer2; get '/3' => sub {3}; }
10
11 sub is_available {
12 my ( $cb, @apps ) = @_;
13 foreach my $app (@apps) {
14 is( $cb->( GET "/$app" )->content, $app, "App$app available" );
15 }
16 }
17
18 sub isnt_available {
19 my ( $cb, @apps ) = @_;
20 foreach my $app (@apps) {
21 is(
22 $cb->( GET "/$app" )->code,
23 404,
24 "App$app is not available",
25 );
26 }
27 }
28
29 note 'All Apps'; {
30 my $app = Dancer2->psgi_app;
31 isa_ok( $app, 'CODE', 'Got PSGI app' );
32 test_psgi $app, sub {
33 my $cb = shift;
34 is_available( $cb, 1, 2, 3 );
35 };
36 }
37
38 note 'Specific Apps by parameters'; {
39 my @apps = @{ Dancer2->runner->apps }[ 0, 2 ];
40 is( scalar @apps, 2, 'Took two apps from the Runner' );
41 my $app = Dancer2->psgi_app(\@apps);
42 isa_ok( $app, 'CODE', 'Got PSGI app' );
43 test_psgi $app, sub {
44 my $cb = shift;
45 is_available( $cb, 1, 3 );
46 isnt_available( $cb, 2 );
47 };
48 }
49
50 note 'Specific Apps via App objects'; {
51 my $app = App2->psgi_app;
52 isa_ok( $app, 'CODE', 'Got PSGI app' );
53 test_psgi $app, sub {
54 my $cb = shift;
55 is_available( $cb, 2 );
56 isnt_available( $cb, 1, 3 );
57 };
58 };
59
60 note 'Specific apps by App names'; {
61 my $app = Dancer2->psgi_app( [ 'App1', 'App3' ] );
62 isa_ok( $app, 'CODE', 'Got PSGI app' );
63 test_psgi $app, sub {
64 my $cb = shift;
65 isnt_available( $cb, 2 );
66 is_available( $cb, 1, 3 );
67 };
68 }
69
70 note 'Specific apps by App names with regular expression, v1'; {
71 my $app = Dancer2->psgi_app( [ qr/^App1$/, qr/^App3$/ ] );
72 isa_ok( $app, 'CODE', 'Got PSGI app' );
73 test_psgi $app, sub {
74 my $cb = shift;
75 isnt_available( $cb, 2 );
76 is_available( $cb, 1, 3 );
77 };
78 }
79
80 note 'Specific apps by App names with regular expression, v2'; {
81 my $app = Dancer2->psgi_app( [ qr/^App(2|3)$/ ] );
82 isa_ok( $app, 'CODE', 'Got PSGI app' );
83 test_psgi $app, sub {
84 my $cb = shift;
85 isnt_available( $cb, 1 );
86 is_available( $cb, 2, 3 );
87 };
88 }
89