forked from jonswar/perl-mason
/
ResolveURI.pm
142 lines (126 loc) · 6.43 KB
/
ResolveURI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package Mason::t::ResolveURI;
use Test::Class::Most parent => 'Mason::Test::Class';
sub test_resolve : Tests {
my $self = shift;
my @interp_params = ();
my $try = sub {
my ( $run_path, $existing_paths, $resolve_path, $path_info ) = @_;
$path_info ||= '';
$self->setup_dirs(@interp_params);
foreach my $existing_path (@$existing_paths) {
my $allow_path_info = 0;
if ( $existing_path =~ /=1$/ ) {
substr( $existing_path, -2, 2 ) = '';
$allow_path_info = 1;
}
$self->add_comp(
path => $existing_path,
src => join( "",
( $allow_path_info ? "<%class>method allow_path_info { 1 }</%class>\n" : "" ),
"path: <% \$self->cmeta->path %>; path_info: <% \$m->path_info %>" )
);
}
my $desc = sprintf( "run %s against %s", $run_path, join( ",", @$existing_paths ) );
if ( defined($resolve_path) ) {
my $good = "path: $resolve_path; path_info: $path_info";
is( $self->interp->run($run_path)->output, $good, "$desc = matched $good" );
}
else {
throws_ok { $self->interp->run($run_path)->output }
qr/could not resolve request path/,
"$desc = failed to match";
}
};
my $run_path = '/foo/bar/baz';
$try->( $run_path, ['/foo/bar/baz.mc'], '/foo/bar/baz.mc', '' );
$try->( $run_path, ['/foo/bar/baz/dhandler.mc'], '/foo/bar/baz/dhandler.mc', '' );
$try->( $run_path, ['/foo/bar/baz/index.mc'], '/foo/bar/baz/index.mc', '' );
$try->( $run_path, ['/foo/bar.mc=1'], '/foo/bar.mc', 'baz' );
$try->( $run_path, ['/foo/bar/dhandler.mc'], '/foo/bar/dhandler.mc', 'baz' );
$try->( $run_path, ['/foo.mc=1'], '/foo.mc', 'bar/baz' );
$try->( $run_path, ['/foo/dhandler.mc'], '/foo/dhandler.mc', 'bar/baz' );
$try->( $run_path, ['/dhandler.mc'], '/dhandler.mc', 'foo/bar/baz' );
$try->( $run_path, [ '/dhandler.mc', '/foo/dhandler.mc' ], '/foo/dhandler.mc', 'bar/baz' );
$try->( $run_path, [ '/foo/dhandler.mc', '/foo/bar.mc=1' ], '/foo/bar.mc', 'baz' );
$try->( $run_path, [ '/foo/dhandler.mc', '/foo/bar.mc' ], '/foo/dhandler.mc', 'bar/baz' );
# Not found
$try->( $run_path, ['/foo/bar.mc'], undef );
$try->( $run_path, ['/foo.mc'], undef );
$try->( $run_path, ['/foo/bar/baz/blarg.mc'], undef );
$try->( $run_path, ['/foo/bar/baz/blarg/dhandler.mc'], undef );
$try->( $run_path, ['/foo/bar/baz'], undef );
$try->( $run_path, ['/foo/dhandler'], undef );
$try->( $run_path, ['/foo/bar/index.mc'], undef );
$try->( $run_path, ['/foo/blarg.mc'], undef );
$try->( $run_path, ['/foo/blarg/dhandler.mc'], undef );
# Can't access autobase or dhandler directly, but can access index
$try->( '/foo/Base', ['/foo/Base.mc'], undef );
$try->( '/foo/dhandler', ['/foo/dhandler.mc'], '/foo/dhandler.mc', 'dhandler' );
$try->( '/foo/index', ['/foo/index.mc'], '/foo/index.mc' );
# no autoextend_run_path
@interp_params = ( autoextend_request_path => 0, top_level_extensions => ['.html'] );
$try->( '/foo/bar/baz.html', ['/foo/bar/baz.html'], '/foo/bar/baz.html', '' );
$try->( '/foo/bar/baz.html', ['/foo/bar/baz.html.mc'], undef );
$try->( "/foo.mc/bar.mi", ['/foo.mc/bar.mi'], undef );
@interp_params = ( autoextend_request_path => 0, top_level_extensions => [] );
$try->( '/foo/bar/baz.html', ['/foo/bar/baz.html'], '/foo/bar/baz.html', '' );
$try->( "/foo.mc/bar.mi", ['/foo.mc/bar.mi'], '/foo.mc/bar.mi', '' );
# dhandler_names
@interp_params = ( dhandler_names => ['dhandler'] );
$try->( $run_path, ['/foo/bar/baz/dhandler.mc'], undef );
$try->( $run_path, ['/foo/bar/baz/dhandler'], '/foo/bar/baz/dhandler', '' );
$try->( $run_path, ['/foo/bar/dhandler'], '/foo/bar/dhandler', 'baz' );
# index_names
@interp_params = ( index_names => [ 'index', 'index2' ] );
$try->( $run_path, ['/foo/bar/baz/index.mc'], undef );
$try->( $run_path, ['/foo/bar/baz/index'], '/foo/bar/baz/index', '' );
$try->( $run_path, ['/foo/bar/baz/index2'], '/foo/bar/baz/index2', '' );
$try->( $run_path, [ '/foo/bar/baz/index2', '/foo/bar/baz/index' ], '/foo/bar/baz/index', '' );
$try->( '/foo', [ '/foo/index' ], '/foo/index', '' );
$try->( '/foo/', [ '/foo/index' ], '/foo/index', '' );
}
sub test_decline : Tests {
my $self = shift;
my @existing_paths =
qw(/foo/bar.mc /foo/bar/dhandler.mc /foo/bar/index.mc /foo.mc /foo/dhandler.mc /dhandler.mc);
my @paths_to_decline = ();
my $run_path = '/foo/bar';
my $try = sub {
my ( $resolve_path, $path_info ) = @_;
my %paths_to_decline_hash = map { ( $_, 1 ) } @paths_to_decline;
$self->setup_dirs();
foreach my $existing_path (@existing_paths) {
my $component =
$paths_to_decline_hash{$existing_path}
? '<%perl>$m->decline();</%perl>'
: 'path: <% $self->cmeta->path %>; path_info: <% $m->path_info %>';
$self->add_comp(
path => $existing_path,
src => $component,
);
$self->add_comp( path => '/Base.mp', src => 'method allow_path_info { 1 }' );
}
my $desc = sprintf( "declining: %s", join( ",", @paths_to_decline ) || '<nothing>' );
if ( defined($resolve_path) ) {
is( $self->interp->run($run_path)->output,
"path: $resolve_path; path_info: $path_info", $desc );
}
else {
throws_ok { $self->interp->run($run_path)->output }
qr/could not resolve request path/,
$desc;
}
push( @paths_to_decline, $resolve_path );
};
# Repeatedly try /foo/bar, test the expected page component, then add
# that component to the decline list and try again.
#
$try->( '/foo/bar.mc', '' );
$try->( '/foo/bar/index.mc', '' );
$try->( '/foo/bar/dhandler.mc', '' );
$try->( '/foo.mc', 'bar' );
$try->( '/foo/dhandler.mc', 'bar' );
$try->( '/dhandler.mc', 'foo/bar' );
$try->(undef);
}
1;