| 1 | package Mason::t::ResolveURI; | ||
| 2 | $Mason::t::ResolveURI::VERSION = '2.23'; | ||
| 3 | 1 | ▶ | use Test::Class::Most parent => 'Mason::Test::Class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | sub test_resolve : Tests { | ||
| 6 | my $self = shift; | ||
| 7 | |||
| 8 | my @interp_params = (); | ||
| 9 | my $try = sub { | ||
| 10 | my ( $run_path, $existing_paths, $resolve_path, $path_info ) = @_; | ||
| 11 | $path_info ||= ''; | ||
| 12 | |||
| 13 | $self->setup_dirs(@interp_params); | ||
| 14 | foreach my $existing_path (@$existing_paths) { | ||
| 15 | my $allow_path_info = 0; | ||
| 16 | if ( $existing_path =~ /=1$/ ) { | ||
| 17 | substr( $existing_path, -2, 2 ) = ''; | ||
| 18 | $allow_path_info = 1; | ||
| 19 | } | ||
| 20 | $self->add_comp( | ||
| 21 | 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 | my $desc = sprintf( "run %s against %s", $run_path, join( ",", @$existing_paths ) ); | ||
| 28 | if ( defined($resolve_path) ) { | ||
| 29 | my $good = "path: $resolve_path; path_info: $path_info"; | ||
| 30 | is( $self->interp->run($run_path)->output, $good, "$desc = matched $good" ); | ||
| 31 | } | ||
| 32 | else { | ||
| 33 | throws_ok { $self->interp->run($run_path)->output } | ||
| 34 | qr/could not resolve request path/, | ||
| 35 | "$desc = failed to match"; | ||
| 36 | } | ||
| 37 | }; | ||
| 38 | |||
| 39 | my $run_path = '/foo/bar/baz'; | ||
| 40 | |||
| 41 | $try->( $run_path, ['/foo/bar/baz.mc'], '/foo/bar/baz.mc', '' ); | ||
| 42 | $try->( $run_path, ['/foo/bar/baz/dhandler.mc'], '/foo/bar/baz/dhandler.mc', '' ); | ||
| 43 | $try->( $run_path, ['/foo/bar/baz/index.mc'], '/foo/bar/baz/index.mc', '' ); | ||
| 44 | $try->( $run_path, ['/foo/bar.mc=1'], '/foo/bar.mc', 'baz' ); | ||
| 45 | $try->( $run_path, ['/foo/bar/dhandler.mc'], '/foo/bar/dhandler.mc', 'baz' ); | ||
| 46 | $try->( $run_path, ['/foo.mc=1'], '/foo.mc', 'bar/baz' ); | ||
| 47 | $try->( $run_path, ['/foo/dhandler.mc'], '/foo/dhandler.mc', 'bar/baz' ); | ||
| 48 | $try->( $run_path, ['/dhandler.mc'], '/dhandler.mc', 'foo/bar/baz' ); | ||
| 49 | $try->( $run_path, [ '/dhandler.mc', '/foo/dhandler.mc' ], '/foo/dhandler.mc', 'bar/baz' ); | ||
| 50 | $try->( $run_path, [ '/foo/dhandler.mc', '/foo/bar.mc=1' ], '/foo/bar.mc', 'baz' ); | ||
| 51 | $try->( $run_path, [ '/foo/dhandler.mc', '/foo/bar.mc' ], '/foo/dhandler.mc', 'bar/baz' ); | ||
| 52 | |||
| 53 | # Not found | ||
| 54 | $try->( $run_path, ['/foo/bar.mc'], undef ); | ||
| 55 | $try->( $run_path, ['/foo.mc'], undef ); | ||
| 56 | $try->( $run_path, ['/foo/bar/baz/blarg.mc'], undef ); | ||
| 57 | $try->( $run_path, ['/foo/bar/baz/blarg/dhandler.mc'], undef ); | ||
| 58 | $try->( $run_path, ['/foo/bar/baz'], undef ); | ||
| 59 | $try->( $run_path, ['/foo/dhandler'], undef ); | ||
| 60 | $try->( $run_path, ['/foo/bar/index.mc'], undef ); | ||
| 61 | $try->( $run_path, ['/foo/blarg.mc'], undef ); | ||
| 62 | $try->( $run_path, ['/foo/blarg/dhandler.mc'], undef ); | ||
| 63 | |||
| 64 | # Can't access autobase or dhandler directly, but can access index | ||
| 65 | $try->( '/foo/Base', ['/foo/Base.mc'], undef ); | ||
| 66 | $try->( '/foo/dhandler', ['/foo/dhandler.mc'], '/foo/dhandler.mc', 'dhandler' ); | ||
| 67 | $try->( '/foo/index', ['/foo/index.mc'], '/foo/index.mc' ); | ||
| 68 | |||
| 69 | # no autoextend_run_path | ||
| 70 | @interp_params = ( autoextend_request_path => 0, top_level_extensions => ['.html'] ); | ||
| 71 | $try->( '/foo/bar/baz.html', ['/foo/bar/baz.html'], '/foo/bar/baz.html', '' ); | ||
| 72 | $try->( '/foo/bar/baz.html', ['/foo/bar/baz.html.mc'], undef ); | ||
| 73 | $try->( "/foo.mc/bar.mi", ['/foo.mc/bar.mi'], undef ); | ||
| 74 | @interp_params = ( autoextend_request_path => 0, top_level_extensions => [] ); | ||
| 75 | $try->( '/foo/bar/baz.html', ['/foo/bar/baz.html'], '/foo/bar/baz.html', '' ); | ||
| 76 | $try->( "/foo.mc/bar.mi", ['/foo.mc/bar.mi'], '/foo.mc/bar.mi', '' ); | ||
| 77 | |||
| 78 | # dhandler_names | ||
| 79 | @interp_params = ( dhandler_names => ['dhandler'] ); | ||
| 80 | $try->( $run_path, ['/foo/bar/baz/dhandler.mc'], undef ); | ||
| 81 | $try->( $run_path, ['/foo/bar/baz/dhandler'], '/foo/bar/baz/dhandler', '' ); | ||
| 82 | $try->( $run_path, ['/foo/bar/dhandler'], '/foo/bar/dhandler', 'baz' ); | ||
| 83 | |||
| 84 | # index_names | ||
| 85 | @interp_params = ( index_names => [ 'index', 'index2' ] ); | ||
| 86 | $try->( $run_path, ['/foo/bar/baz/index.mc'], undef ); | ||
| 87 | $try->( $run_path, ['/foo/bar/baz/index'], '/foo/bar/baz/index', '' ); | ||
| 88 | $try->( $run_path, ['/foo/bar/baz/index2'], '/foo/bar/baz/index2', '' ); | ||
| 89 | $try->( $run_path, [ '/foo/bar/baz/index2', '/foo/bar/baz/index' ], '/foo/bar/baz/index', '' ); | ||
| 90 | |||
| 91 | # trailing slashes | ||
| 92 | $try->( '/foo', ['/foo.mc=1'], '/foo.mc', '' ); | ||
| 93 | $try->( '/foo/', ['/foo.mc=1'], '/foo.mc', '/' ); | ||
| 94 | $try->( '/foo/bar', ['/foo.mc=1'], '/foo.mc', 'bar' ); | ||
| 95 | $try->( '/foo/bar/', ['/foo.mc=1'], '/foo.mc', 'bar/' ); | ||
| 96 | $try->( '/foo/', ['/foo.mc'], undef ); | ||
| 97 | @interp_params = ( dhandler_names => ['dhandler'] ); | ||
| 98 | $try->( '/foo/', ['/foo/dhandler'], '/foo/dhandler', '/' ); | ||
| 99 | $try->( '/foo/bar', ['/foo/dhandler'], '/foo/dhandler', 'bar' ); | ||
| 100 | $try->( '/foo/bar/', ['/foo/dhandler'], '/foo/dhandler', 'bar/' ); | ||
| 101 | @interp_params = ( index_names => ['index'] ); | ||
| 102 | $try->( '/foo/', ['/foo/index'], undef ); | ||
| 103 | $try->( '/foo/', ['/foo/index=1'], '/foo/index', '/' ); | ||
| 104 | @interp_params = ( dhandler_names => ['dhandler'], index_names => ['index'] ); | ||
| 105 | $try->( '/foo/', [ '/foo/dhandler', '/foo/index' ], '/foo/dhandler', '/' ); | ||
| 106 | $try->( '/foo/', [ '/foo/dhandler', '/foo/index=1' ], '/foo/index', '/' ); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub test_decline : Tests { | ||
| 110 | my $self = shift; | ||
| 111 | |||
| 112 | my @existing_paths = | ||
| 113 | qw(/foo/bar.mc /foo/bar/dhandler.mc /foo/bar/index.mc /foo.mc /foo/dhandler.mc /dhandler.mc); | ||
| 114 | my @paths_to_decline = (); | ||
| 115 | my $run_path = '/foo/bar'; | ||
| 116 | |||
| 117 | my $try = sub { | ||
| 118 | my ( $resolve_path, $path_info ) = @_; | ||
| 119 | my %paths_to_decline_hash = map { ( $_, 1 ) } @paths_to_decline; | ||
| 120 | |||
| 121 | $self->setup_dirs(); | ||
| 122 | foreach my $existing_path (@existing_paths) { | ||
| 123 | 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 | $self->add_comp( | ||
| 128 | path => $existing_path, | ||
| 129 | src => $component, | ||
| 130 | ); | ||
| 131 | $self->add_comp( path => '/Base.mp', src => 'method allow_path_info { 1 }' ); | ||
| 132 | } | ||
| 133 | my $desc = sprintf( "declining: %s", join( ",", @paths_to_decline ) || '<nothing>' ); | ||
| 134 | if ( defined($resolve_path) ) { | ||
| 135 | is( $self->interp->run($run_path)->output, | ||
| 136 | "path: $resolve_path; path_info: $path_info", $desc ); | ||
| 137 | } | ||
| 138 | else { | ||
| 139 | throws_ok { $self->interp->run($run_path)->output } | ||
| 140 | qr/could not resolve request path/, | ||
| 141 | $desc; | ||
| 142 | } | ||
| 143 | push( @paths_to_decline, $resolve_path ); | ||
| 144 | }; | ||
| 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 | $try->( '/foo/bar.mc', '' ); | ||
| 150 | $try->( '/foo/bar/index.mc', '' ); | ||
| 151 | $try->( '/foo/bar/dhandler.mc', '' ); | ||
| 152 | $try->( '/foo.mc', 'bar' ); | ||
| 153 | $try->( '/foo/dhandler.mc', 'bar' ); | ||
| 154 | $try->( '/dhandler.mc', 'foo/bar' ); | ||
| 155 | $try->(undef); | ||
| 156 | } | ||
| 157 | |||
| 158 | 1; | ||