File Coverage

blib/lib/Mason/t/ResolveURI.pm
Criterion Covered Total %
statement 108 108 100.0
branch 10 10 100.0
condition 4 4 100.0
subroutine 7 7 100.0
pod 0 2 0.0
total 129 131 98.4


line stmt bran cond sub pod time code
1             package Mason::t::ResolveURI;
2             $Mason::t::ResolveURI::VERSION = '2.23';
3 1     1   1020 use Test::Class::Most parent => 'Mason::Test::Class';
  1         35311  
  1         8  
4              
5             sub test_resolve : Tests {
6 1     1 0 314 my $self = shift;
7              
8 1         3 my @interp_params = ();
9             my $try = sub {
10 47     47   149 my ( $run_path, $existing_paths, $resolve_path, $path_info ) = @_;
11 47   100     295 $path_info ||= '';
12              
13 47         424 $self->setup_dirs(@interp_params);
14 47         1333 foreach my $existing_path (@$existing_paths) {
15 53         101 my $allow_path_info = 0;
16 53 100       248 if ( $existing_path =~ /=1$/ ) {
17 9         28 substr( $existing_path, -2, 2 ) = '';
18 9         17 $allow_path_info = 1;
19             }
20             $self->add_comp(
21 53 100       557 path => $existing_path,
22             src => join( "",
23             ( $allow_path_info ? "<%class>method allow_path_info { 1 }</%class>\n" : "" ),
24             "path: <% \$self->cmeta->path %>; path_info: <% \$m->path_info %>" )
25             );
26             }
27 47         398 my $desc = sprintf( "run %s against %s", $run_path, join( ",", @$existing_paths ) );
28 47 100       235 if ( defined($resolve_path) ) {
29 31         107 my $good = "path: $resolve_path; path_info: $path_info";
30 31         187 is( $self->interp->run($run_path)->output, $good, "$desc = matched $good" );
31             }
32             else {
33 16         1694 throws_ok { $self->interp->run($run_path)->output }
34 16         239 qr/could not resolve request path/,
35             "$desc = failed to match";
36             }
37 1         7 };
38              
39 1         3 my $run_path = '/foo/bar/baz';
40              
41 1         4 $try->( $run_path, ['/foo/bar/baz.mc'], '/foo/bar/baz.mc', '' );
42 1         698 $try->( $run_path, ['/foo/bar/baz/dhandler.mc'], '/foo/bar/baz/dhandler.mc', '' );
43 1         576 $try->( $run_path, ['/foo/bar/baz/index.mc'], '/foo/bar/baz/index.mc', '' );
44 1         359 $try->( $run_path, ['/foo/bar.mc=1'], '/foo/bar.mc', 'baz' );
45 1         357 $try->( $run_path, ['/foo/bar/dhandler.mc'], '/foo/bar/dhandler.mc', 'baz' );
46 1         372 $try->( $run_path, ['/foo.mc=1'], '/foo.mc', 'bar/baz' );
47 1         430 $try->( $run_path, ['/foo/dhandler.mc'], '/foo/dhandler.mc', 'bar/baz' );
48 1         365 $try->( $run_path, ['/dhandler.mc'], '/dhandler.mc', 'foo/bar/baz' );
49 1         361 $try->( $run_path, [ '/dhandler.mc', '/foo/dhandler.mc' ], '/foo/dhandler.mc', 'bar/baz' );
50 1         628 $try->( $run_path, [ '/foo/dhandler.mc', '/foo/bar.mc=1' ], '/foo/bar.mc', 'baz' );
51 1         628 $try->( $run_path, [ '/foo/dhandler.mc', '/foo/bar.mc' ], '/foo/dhandler.mc', 'bar/baz' );
52              
53             # Not found
54 1         637 $try->( $run_path, ['/foo/bar.mc'], undef );
55 1         2382 $try->( $run_path, ['/foo.mc'], undef );
56 1         2565 $try->( $run_path, ['/foo/bar/baz/blarg.mc'], undef );
57 1         2329 $try->( $run_path, ['/foo/bar/baz/blarg/dhandler.mc'], undef );
58 1         3828 $try->( $run_path, ['/foo/bar/baz'], undef );
59 1         3445 $try->( $run_path, ['/foo/dhandler'], undef );
60 1         2851 $try->( $run_path, ['/foo/bar/index.mc'], undef );
61 1         2997 $try->( $run_path, ['/foo/blarg.mc'], undef );
62 1         2966 $try->( $run_path, ['/foo/blarg/dhandler.mc'], undef );
63              
64             # Can't access autobase or dhandler directly, but can access index
65 1         3419 $try->( '/foo/Base', ['/foo/Base.mc'], undef );
66 1         3027 $try->( '/foo/dhandler', ['/foo/dhandler.mc'], '/foo/dhandler.mc', 'dhandler' );
67 1         675 $try->( '/foo/index', ['/foo/index.mc'], '/foo/index.mc' );
68              
69             # no autoextend_run_path
70 1         707 @interp_params = ( autoextend_request_path => 0, top_level_extensions => ['.html'] );
71 1         4 $try->( '/foo/bar/baz.html', ['/foo/bar/baz.html'], '/foo/bar/baz.html', '' );
72 1         820 $try->( '/foo/bar/baz.html', ['/foo/bar/baz.html.mc'], undef );
73 1         2564 $try->( "/foo.mc/bar.mi", ['/foo.mc/bar.mi'], undef );
74 1         2571 @interp_params = ( autoextend_request_path => 0, top_level_extensions => [] );
75 1         4 $try->( '/foo/bar/baz.html', ['/foo/bar/baz.html'], '/foo/bar/baz.html', '' );
76 1         751 $try->( "/foo.mc/bar.mi", ['/foo.mc/bar.mi'], '/foo.mc/bar.mi', '' );
77              
78             # dhandler_names
79 1         711 @interp_params = ( dhandler_names => ['dhandler'] );
80 1         3 $try->( $run_path, ['/foo/bar/baz/dhandler.mc'], undef );
81 1         2466 $try->( $run_path, ['/foo/bar/baz/dhandler'], '/foo/bar/baz/dhandler', '' );
82 1         938 $try->( $run_path, ['/foo/bar/dhandler'], '/foo/bar/dhandler', 'baz' );
83              
84             # index_names
85 1         780 @interp_params = ( index_names => [ 'index', 'index2' ] );
86 1         4 $try->( $run_path, ['/foo/bar/baz/index.mc'], undef );
87 1         2190 $try->( $run_path, ['/foo/bar/baz/index'], '/foo/bar/baz/index', '' );
88 1         663 $try->( $run_path, ['/foo/bar/baz/index2'], '/foo/bar/baz/index2', '' );
89 1         660 $try->( $run_path, [ '/foo/bar/baz/index2', '/foo/bar/baz/index' ], '/foo/bar/baz/index', '' );
90              
91             # trailing slashes
92 1         712 $try->( '/foo', ['/foo.mc=1'], '/foo.mc', '' );
93 1         645 $try->( '/foo/', ['/foo.mc=1'], '/foo.mc', '/' );
94 1         656 $try->( '/foo/bar', ['/foo.mc=1'], '/foo.mc', 'bar' );
95 1         709 $try->( '/foo/bar/', ['/foo.mc=1'], '/foo.mc', 'bar/' );
96 1         663 $try->( '/foo/', ['/foo.mc'], undef );
97 1         2337 @interp_params = ( dhandler_names => ['dhandler'] );
98 1         5 $try->( '/foo/', ['/foo/dhandler'], '/foo/dhandler', '/' );
99 1         654 $try->( '/foo/bar', ['/foo/dhandler'], '/foo/dhandler', 'bar' );
100 1         742 $try->( '/foo/bar/', ['/foo/dhandler'], '/foo/dhandler', 'bar/' );
101 1         967 @interp_params = ( index_names => ['index'] );
102 1         4 $try->( '/foo/', ['/foo/index'], undef );
103 1         2427 $try->( '/foo/', ['/foo/index=1'], '/foo/index', '/' );
104 1         664 @interp_params = ( dhandler_names => ['dhandler'], index_names => ['index'] );
105 1         4 $try->( '/foo/', [ '/foo/dhandler', '/foo/index' ], '/foo/dhandler', '/' );
106 1         676 $try->( '/foo/', [ '/foo/dhandler', '/foo/index=1' ], '/foo/index', '/' );
107 1     1   638 }
  1         1  
  1         7  
108              
109             sub test_decline : Tests {
110 1     1 0 777 my $self = shift;
111              
112 1         5 my @existing_paths =
113             qw(/foo/bar.mc /foo/bar/dhandler.mc /foo/bar/index.mc /foo.mc /foo/dhandler.mc /dhandler.mc);
114 1         3 my @paths_to_decline = ();
115 1         2 my $run_path = '/foo/bar';
116              
117             my $try = sub {
118 7     7   17 my ( $resolve_path, $path_info ) = @_;
119 7         15 my %paths_to_decline_hash = map { ( $_, 1 ) } @paths_to_decline;
  21         44  
120              
121 7         45 $self->setup_dirs();
122 7         177 foreach my $existing_path (@existing_paths) {
123 42 100       118 my $component =
124             $paths_to_decline_hash{$existing_path}
125             ? '<%perl>$m->decline();</%perl>'
126             : 'path: <% $self->cmeta->path %>; path_info: <% $m->path_info %>';
127 42         135 $self->add_comp(
128             path => $existing_path,
129             src => $component,
130             );
131 42         120 $self->add_comp( path => '/Base.mp', src => 'method allow_path_info { 1 }' );
132             }
133 7   100     72 my $desc = sprintf( "declining: %s", join( ",", @paths_to_decline ) || '<nothing>' );
134 7 100       17 if ( defined($resolve_path) ) {
135 6         33 is( $self->interp->run($run_path)->output,
136             "path: $resolve_path; path_info: $path_info", $desc );
137             }
138             else {
139 1         74 throws_ok { $self->interp->run($run_path)->output }
140 1         16 qr/could not resolve request path/,
141             $desc;
142             }
143 7         5654 push( @paths_to_decline, $resolve_path );
144 1         6 };
145              
146             # Repeatedly try /foo/bar, test the expected page component, then add
147             # that component to the decline list and try again.
148             #
149 1         3 $try->( '/foo/bar.mc', '' );
150 1         3 $try->( '/foo/bar/index.mc', '' );
151 1         3 $try->( '/foo/bar/dhandler.mc', '' );
152 1         3 $try->( '/foo.mc', 'bar' );
153 1         3 $try->( '/foo/dhandler.mc', 'bar' );
154 1         4 $try->( '/dhandler.mc', 'foo/bar' );
155 1         3 $try->(undef);
156 1     1   532 }
  1         3  
  1         4  
157              
158             1;