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.24';
3 1     1   818 use Test::Class::Most parent => 'Mason::Test::Class';
  1         30690  
  1         7  
4              
5             sub test_resolve : Tests {
6 1     1 0 398 my $self = shift;
7              
8 1         3 my @interp_params = ();
9             my $try = sub {
10 47     47   178 my ( $run_path, $existing_paths, $resolve_path, $path_info ) = @_;
11 47   100     360 $path_info ||= '';
12              
13 47         545 $self->setup_dirs(@interp_params);
14 47         2008 foreach my $existing_path (@$existing_paths) {
15 53         181 my $allow_path_info = 0;
16 53 100       344 if ( $existing_path =~ /=1$/ ) {
17 9         47 substr( $existing_path, -2, 2 ) = '';
18 9         23 $allow_path_info = 1;
19             }
20             $self->add_comp(
21 53 100       836 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         603 my $desc = sprintf( "run %s against %s", $run_path, join( ",", @$existing_paths ) );
28 47 100       251 if ( defined($resolve_path) ) {
29 31         162 my $good = "path: $resolve_path; path_info: $path_info";
30 31         255 is( $self->interp->run($run_path)->output, $good, "$desc = matched $good" );
31             }
32             else {
33 16         2803 throws_ok { $self->interp->run($run_path)->output }
34 16         389 qr/could not resolve request path/,
35             "$desc = failed to match";
36             }
37 1         8 };
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         860 $try->( $run_path, ['/foo/bar/baz/dhandler.mc'], '/foo/bar/baz/dhandler.mc', '' );
43 1         728 $try->( $run_path, ['/foo/bar/baz/index.mc'], '/foo/bar/baz/index.mc', '' );
44 1         911 $try->( $run_path, ['/foo/bar.mc=1'], '/foo/bar.mc', 'baz' );
45 1         901 $try->( $run_path, ['/foo/bar/dhandler.mc'], '/foo/bar/dhandler.mc', 'baz' );
46 1         723 $try->( $run_path, ['/foo.mc=1'], '/foo.mc', 'bar/baz' );
47 1         1011 $try->( $run_path, ['/foo/dhandler.mc'], '/foo/dhandler.mc', 'bar/baz' );
48 1         1191 $try->( $run_path, ['/dhandler.mc'], '/dhandler.mc', 'foo/bar/baz' );
49 1         1190 $try->( $run_path, [ '/dhandler.mc', '/foo/dhandler.mc' ], '/foo/dhandler.mc', 'bar/baz' );
50 1         1197 $try->( $run_path, [ '/foo/dhandler.mc', '/foo/bar.mc=1' ], '/foo/bar.mc', 'baz' );
51 1         1390 $try->( $run_path, [ '/foo/dhandler.mc', '/foo/bar.mc' ], '/foo/dhandler.mc', 'bar/baz' );
52              
53             # Not found
54 1         1413 $try->( $run_path, ['/foo/bar.mc'], undef );
55 1         5724 $try->( $run_path, ['/foo.mc'], undef );
56 1         3266 $try->( $run_path, ['/foo/bar/baz/blarg.mc'], undef );
57 1         3766 $try->( $run_path, ['/foo/bar/baz/blarg/dhandler.mc'], undef );
58 1         3540 $try->( $run_path, ['/foo/bar/baz'], undef );
59 1         4039 $try->( $run_path, ['/foo/dhandler'], undef );
60 1         3196 $try->( $run_path, ['/foo/bar/index.mc'], undef );
61 1         4964 $try->( $run_path, ['/foo/blarg.mc'], undef );
62 1         4117 $try->( $run_path, ['/foo/blarg/dhandler.mc'], undef );
63              
64             # Can't access autobase or dhandler directly, but can access index
65 1         2728 $try->( '/foo/Base', ['/foo/Base.mc'], undef );
66 1         2634 $try->( '/foo/dhandler', ['/foo/dhandler.mc'], '/foo/dhandler.mc', 'dhandler' );
67 1         926 $try->( '/foo/index', ['/foo/index.mc'], '/foo/index.mc' );
68              
69             # no autoextend_run_path
70 1         1131 @interp_params = ( autoextend_request_path => 0, top_level_extensions => ['.html'] );
71 1         6 $try->( '/foo/bar/baz.html', ['/foo/bar/baz.html'], '/foo/bar/baz.html', '' );
72 1         975 $try->( '/foo/bar/baz.html', ['/foo/bar/baz.html.mc'], undef );
73 1         2745 $try->( "/foo.mc/bar.mi", ['/foo.mc/bar.mi'], undef );
74 1         2636 @interp_params = ( autoextend_request_path => 0, top_level_extensions => [] );
75 1         7 $try->( '/foo/bar/baz.html', ['/foo/bar/baz.html'], '/foo/bar/baz.html', '' );
76 1         954 $try->( "/foo.mc/bar.mi", ['/foo.mc/bar.mi'], '/foo.mc/bar.mi', '' );
77              
78             # dhandler_names
79 1         882 @interp_params = ( dhandler_names => ['dhandler'] );
80 1         9 $try->( $run_path, ['/foo/bar/baz/dhandler.mc'], undef );
81 1         2766 $try->( $run_path, ['/foo/bar/baz/dhandler'], '/foo/bar/baz/dhandler', '' );
82 1         811 $try->( $run_path, ['/foo/bar/dhandler'], '/foo/bar/dhandler', 'baz' );
83              
84             # index_names
85 1         772 @interp_params = ( index_names => [ 'index', 'index2' ] );
86 1         7 $try->( $run_path, ['/foo/bar/baz/index.mc'], undef );
87 1         4110 $try->( $run_path, ['/foo/bar/baz/index'], '/foo/bar/baz/index', '' );
88 1         1253 $try->( $run_path, ['/foo/bar/baz/index2'], '/foo/bar/baz/index2', '' );
89 1         1013 $try->( $run_path, [ '/foo/bar/baz/index2', '/foo/bar/baz/index' ], '/foo/bar/baz/index', '' );
90              
91             # trailing slashes
92 1         1134 $try->( '/foo', ['/foo.mc=1'], '/foo.mc', '' );
93 1         741 $try->( '/foo/', ['/foo.mc=1'], '/foo.mc', '/' );
94 1         877 $try->( '/foo/bar', ['/foo.mc=1'], '/foo.mc', 'bar' );
95 1         844 $try->( '/foo/bar/', ['/foo.mc=1'], '/foo.mc', 'bar/' );
96 1         926 $try->( '/foo/', ['/foo.mc'], undef );
97 1         3502 @interp_params = ( dhandler_names => ['dhandler'] );
98 1         9 $try->( '/foo/', ['/foo/dhandler'], '/foo/dhandler', '/' );
99 1         878 $try->( '/foo/bar', ['/foo/dhandler'], '/foo/dhandler', 'bar' );
100 1         894 $try->( '/foo/bar/', ['/foo/dhandler'], '/foo/dhandler', 'bar/' );
101 1         1077 @interp_params = ( index_names => ['index'] );
102 1         6 $try->( '/foo/', ['/foo/index'], undef );
103 1         3239 $try->( '/foo/', ['/foo/index=1'], '/foo/index', '/' );
104 1         955 @interp_params = ( dhandler_names => ['dhandler'], index_names => ['index'] );
105 1         8 $try->( '/foo/', [ '/foo/dhandler', '/foo/index' ], '/foo/dhandler', '/' );
106 1         628 $try->( '/foo/', [ '/foo/dhandler', '/foo/index=1' ], '/foo/index', '/' );
107 1     1   731 }
  1         2  
  1         4  
108              
109             sub test_decline : Tests {
110 1     1 0 765 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         2 my @paths_to_decline = ();
115 1         2 my $run_path = '/foo/bar';
116              
117             my $try = sub {
118 7     7   18 my ( $resolve_path, $path_info ) = @_;
119 7         21 my %paths_to_decline_hash = map { ( $_, 1 ) } @paths_to_decline;
  21         51  
120              
121 7         53 $self->setup_dirs();
122 7         176 foreach my $existing_path (@existing_paths) {
123 42 100       121 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         141 $self->add_comp(
128             path => $existing_path,
129             src => $component,
130             );
131 42         111 $self->add_comp( path => '/Base.mp', src => 'method allow_path_info { 1 }' );
132             }
133 7   100     89 my $desc = sprintf( "declining: %s", join( ",", @paths_to_decline ) || '<nothing>' );
134 7 100       25 if ( defined($resolve_path) ) {
135 6         47 is( $self->interp->run($run_path)->output,
136             "path: $resolve_path; path_info: $path_info", $desc );
137             }
138             else {
139 1         53 throws_ok { $self->interp->run($run_path)->output }
140 1         16 qr/could not resolve request path/,
141             $desc;
142             }
143 7         7350 push( @paths_to_decline, $resolve_path );
144 1         8 };
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         4 $try->( '/foo/bar/index.mc', '' );
151 1         3 $try->( '/foo/bar/dhandler.mc', '' );
152 1         6 $try->( '/foo.mc', 'bar' );
153 1         4 $try->( '/foo/dhandler.mc', 'bar' );
154 1         3 $try->( '/dhandler.mc', 'foo/bar' );
155 1         4 $try->(undef);
156 1     1   522 }
  1         1  
  1         3  
157              
158             1;