Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
clean up redirects
  • Loading branch information
haarg committed Sep 5, 2017
1 parent dba8c40 commit f230124
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
4 changes: 2 additions & 2 deletions lib/MetaCPAN/Web/Controller/Module.pm
Expand Up @@ -19,12 +19,12 @@ sub redirect_to_pod : Path : Args {

# Force the author arg to uppercase to avoid another redirect.
$c->res->redirect(
'/pod/release/' . join( q{/}, uc( shift @path ), @path ), 301 );
$c->uri_for( '/pod/release', uc( shift @path ), @path ), 301 );
}

# /module/Foo::Bar
else {
$c->res->redirect( '/pod/' . join( q{/}, @path ), 301 );
$c->res->redirect( $c->uri_for( '/pod', @path ), 301 );
}

$c->detach();
Expand Down
19 changes: 11 additions & 8 deletions lib/MetaCPAN/Web/Controller/Pod.pm
Expand Up @@ -37,20 +37,23 @@ sub find : Path : Args(1) {

# /pod/release/$AUTHOR/$release/@path
sub release : Local : Args {
my ( $self, $c, @path ) = @_;
my ( $self, $c, $author, $release, @path ) = @_;

if ( !@path ) {
$c->detach('/not_found');
}
$c->browser_max_age('1d');

# force consistent casing in URLs
if ( @path > 2 && $path[0] ne uc( $path[0] ) ) {
if ( $author ne uc $author ) {
$c->res->redirect(
'/pod/release/' . join( q{/}, uc( shift @path ), @path ), 301 );
$c->uri_for( $c->action, uc $author, $release, @path ), 301 );
$c->detach();
}

my $release_data = $c->model('ReleaseInfo')->get( @path[ 0, 1 ] )
->else( sub { Future->done( {} ) } );
my $pod_file = $c->model('API::Module')->get(@path);
my $release_data
= $c->model('ReleaseInfo')->get( $author, $release )->else_done( {} );
my $pod_file = $c->model('API::Module')->get( $author, $release, @path );
$c->stash(
{
pod_file => $pod_file->get,
Expand All @@ -59,7 +62,7 @@ sub release : Local : Args {
}
);

$c->forward( 'view', [@path] );
$c->forward( 'view', [ $author, $release, @path ] );
}

# /pod/distribution/$name/@path
Expand Down Expand Up @@ -96,7 +99,7 @@ sub view : Private {
my $permalinks = $c->stash->{permalinks};

if ( $data->{directory} ) {
$c->res->redirect( '/source/' . join( q{/}, @path ), 301 );
$c->res->redirect( $c->uri_for( '/source', @path ), 301 );
$c->detach;
}

Expand Down
7 changes: 1 addition & 6 deletions lib/MetaCPAN/Web/Controller/Release.pm
Expand Up @@ -37,12 +37,7 @@ sub by_author_and_release : Chained('root') PathPart('') Args(2) {
# force consistent casing in URLs
if ( $author ne uc($author) ) {
$c->res->redirect(
$c->uri_for_action(
$c->controller->action_for('by_author_and_release'),
[ uc($author), $release ]
),
301
);
$c->uri_for_action( $c->action, uc($author), $release ), 301 );
$c->detach();
}

Expand Down
6 changes: 3 additions & 3 deletions lib/MetaCPAN/Web/Controller/Search.pm
Expand Up @@ -47,19 +47,19 @@ sub index : Path : Args(0) {
my $module = $model->first($query)->get;
$module = $module->[0] if $module and is_arrayref($module);
if ( $module && $module eq $query ) {
$c->res->redirect( '/pod/' . $module );
$c->res->redirect( $c->uri_for( '/pod', $module ) );
$c->detach;
}
else {
my $author = $c->model('API::Author')->search($query)->get;
if ( $author->{total} == 1
&& $query eq $author->{authors}->[0]->{pauseid} )
{
$c->res->redirect( '/author/' . uc($query) );
$c->res->redirect( $c->uri_for( '/author', uc($query) ) );
$c->detach;
}
elsif ($module) {
$c->res->redirect("/pod/$module");
$c->res->redirect( $c->uri_for( '/pod', $module ) );
$c->detach;
}
else {
Expand Down
7 changes: 5 additions & 2 deletions t/controller/pod.t
Expand Up @@ -23,6 +23,9 @@ test_psgi app, sub {
'contains link to "this" version'
);

# just in case, for comparisons
$this =~ s{^http://[^/]+}{};

my $latest = $res->content;
ok( $res = $cb->( GET $this ), "GET $this" );
is(
Expand All @@ -45,8 +48,8 @@ test_psgi app, sub {
=~ s{(/pod/release/)([^/]+)}{$1\L$2}; # lc author name
ok( $res = $cb->( GET $lc_this ), "GET $lc_this" );
is( $res->code, 301, '301 on lowercase author name' );
is( $res->headers->header('location'),
$this, 'redirect to uppercase author name' );
my $location = $res->headers->header('location') =~ s{^http://[^/]+}{}r;
is( $location, $this, 'redirect to uppercase author name' );
};

done_testing;
14 changes: 9 additions & 5 deletions t/controller/search.t
Expand Up @@ -62,23 +62,27 @@ test_psgi app, sub {

ok( $res = $cb->( GET '/search?q=RJBS&lucky=1' ),
'GET /search?q=&lucky=1' );
is( $res->headers->{location},
'/author/RJBS', 'get redirect to author page' );
is(
$res->headers->{location},
'http://localhost/author/RJBS',
'get redirect to author page'
);

ok( $res = $cb->( GET '/search?q=JSON&lucky=1' ),
'GET /search?q=&lucky=1' );
is( $res->headers->{location},
'/pod/JSON', 'get redirect to pod page if module is found' );
'http://localhost/pod/JSON',
'get redirect to pod page if module is found' );

ok( $res = $cb->( GET '/search?q=win32&lucky=1' ),
'GET /search?q=&lucky=1' );
is( $res->headers->{location}, '/pod/Win32',
is( $res->headers->{location}, 'http://localhost/pod/Win32',
'get redirect to pod page if module found and the query not upper case'
);

ok( $res = $cb->( GET '/search?q=WIN32&lucky=1' ),
'GET /search?q=&lucky=1' );
is( $res->headers->{location}, '/author/WIN32',
is( $res->headers->{location}, 'http://localhost/author/WIN32',
'get redirect to author page when author is found and search with upper case'
);

Expand Down

0 comments on commit f230124

Please sign in to comment.