Debian Codegraph @

t/psgi_app.t

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