Debian Codegraph @

t/dsl/path.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests => 5;
4 use Plack::Test;
5 use Plack::Request;
6 use Plack::Builder;
7 use HTTP::Request::Common;
8
9 {
10 package App;
11 use Dancer2;
12 get '/' => sub {
13 my $dancer_req = request;
14 my $env = $dancer_req->env;
15 my $plack_req = Plack::Request->new($env);
16
17 ::like(
18 $env->{'PATH_INFO'},
19 qr{^/?$},
20 'PATH_INFO empty or /',
21 );
22
23 ::is(
24 $dancer_req->path_info,
25 $env->{'PATH_INFO'},
26 'D2 path_info matches $env',
27 );
28
29 ::is(
30 $dancer_req->path_info,
31 $plack_req->path_info,
32 'D2 path_info matches Plack path_info',
33 );
34
35 ::is( $dancer_req->path, '/', 'D2 path is /' );
36 ::is( $plack_req->path, '/', 'Plack path is /' );
37
38 return $dancer_req->script_name;
39 };
40
41 get '/endpoint' => sub {
42 my $dancer_req = request;
43 my $env = $dancer_req->env;
44 my $plack_req = Plack::Request->new($env);
45
46 ::is(
47 $env->{'PATH_INFO'},
48 '/endpoint',
49 'PATH_INFO /endpoint',
50 );
51
52 ::is(
53 $dancer_req->path_info,
54 $env->{'PATH_INFO'},
55 'D2 path_info matches $env',
56 );
57
58 ::is(
59 $dancer_req->path_info,
60 $plack_req->path_info,
61 'D2 path_info matches Plack path_info',
62 );
63
64 ::is( $dancer_req->path, '/endpoint', 'D2 path is /' );
65 ::is( $plack_req->path, '/endpoint', 'Plack path is /' );
66
67 return $dancer_req->script_name;
68 };
69 }
70
71 subtest '/' => sub {
72 my $test = Plack::Test->create( App->to_app );
73 my $res = $test->request( GET '/' );
74 ok( $res->is_success, 'Result successful' );
75 is( $res->content, '', 'script_name is empty' );
76 };
77
78 subtest '/endpoint' => sub {
79 my $test = Plack::Test->create( App->to_app );
80 my $res = $test->request( GET '/endpoint' );
81 ok( $res->is_success, 'Result successful' );
82 is( $res->content, '', 'script_name is empty' );
83 };
84
85 subtest '/mounted/' => sub {
86 my $app = builder {
87 mount '/' => sub { [200,[],['OK']] };
88 mount '/mounted' => App->to_app;
89 };
90
91 my $test = Plack::Test->create($app);
92
93 my $res = $test->request( GET '/mounted/' );
94 ok( $res->is_success, 'Result successful' );
95 is( $res->content, '/mounted', 'script_name is /mounted' );
96 };
97
98 subtest '/mounted/endpoint' => sub {
99 my $app = builder {
100 mount '/' => sub { [200,[],['OK']] };
101 mount '/mounted' => App->to_app;
102 };
103
104 my $test = Plack::Test->create($app);
105
106 my $res = $test->request( GET '/mounted/endpoint' );
107 ok( $res->is_success, 'Result successful' );
108 is( $res->content, '/mounted', 'script_name is /mounted' );
109 };
110
111 # Tests behaviour when SCRIPT_NAME is also the beginning of PATH_INFO
112 # See the discussion in #1288.
113 subtest '/endpoint/endpoint' => sub {
114 my $app = builder {
115 mount '/' => sub { [200,[],['OK']] };
116 mount '/endpoint' => App->to_app;
117 };
118
119 my $test = Plack::Test->create($app);
120
121 my $res = $test->request( GET '/endpoint/endpoint' );
122 ok( $res->is_success, 'Result successful' );
123 is( $res->content, '/endpoint', 'script_name is /endpoint' );
124 };
125