Skip to content

Commit ee35525

Browse files
committed
/search?q=about で/about に遷移させない
1 parent 121063b commit ee35525

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

lib/PJP/Web/Dispatcher.pm

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,27 @@ get '/search' => sub {
456456
return $c->create_simple_status_page(400, 'Bad Request');
457457
}
458458

459-
return $c->redirect("/$q");
459+
if ($q =~ /^(perl.*)/) {
460+
return $c->redirect("/pod/$1");
461+
}
462+
463+
if ($q =~ /^([\$\@\%].+)/) {
464+
return $c->redirect("/variable/" . uri_escape($1));
465+
}
466+
467+
foreach my $function_regexp (@PJP::M::BuiltinFunction::REGEXP) {
468+
if ($q =~ /^($function_regexp)/) {
469+
return $c->redirect("/func/$1");
470+
}
471+
}
472+
473+
if ($q =~ /^([A-Z-a-z][\w:]+)/) {
474+
if (my $path = PJP::M::PodFile->get_latest($1)) {
475+
return $c->redirect('/docs/' . $path);
476+
}
477+
}
478+
479+
return $c->res_404({ query => $q });
460480
};
461481

462482
# 以下、ルーティングルールを用いて、ドキュメント検索を行っている

t/endpoints.t

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,22 +381,45 @@ subtest 'perldoc.jp/$VALUE のように指定したら、よしなにリダイ
381381
};
382382

383383
subtest 'GET /search' => sub {
384-
subtest '検索クエリが指定された場合、/$qへリダイレクトされる' => sub {
385-
# /search?q=chomp -> /chomp -> /func/chomp
384+
subtest 'perlで始まるクエリの場合、/pod/perlxxxへリダイレクトされる' => sub {
385+
# /search?q=perlintro -> /pod/perlintro -> /docs/perl/.../perlintro.pod
386+
$mech->get('/search?q=perlintro');
387+
is $mech->status, 200, 'status is 200';
388+
is $mech->title, 'perlintro - Perl の概要 - perldoc.jp';
389+
$mech->base_like(qr{/docs/perl/[^/]+/perlintro\.pod$});
390+
};
391+
392+
subtest '組み込み変数を検索した場合、/variable/へリダイレクトされる' => sub {
393+
# /search?q=$_ -> /variable/$_
394+
$mech->get('/search?q=$_');
395+
is $mech->status, 200, 'status is 200';
396+
is $mech->title, 'Perlの組み込み変数 $_ の翻訳 - perldoc.jp';
397+
$mech->base_like(qr{/variable/%24_$});
398+
};
399+
400+
subtest '組み込み関数を検索した場合、/func/へリダイレクトされる' => sub {
401+
# /search?q=chomp -> /func/chomp
386402
$mech->get('/search?q=chomp');
387403
is $mech->status, 200, 'status is 200';
388404
is $mech->title, 'Perlの組み込み関数 chomp の翻訳 - perldoc.jp';
389405
$mech->base_like(qr{/func/chomp$});
390406
};
391407

392-
subtest 'モジュール名を検索した場合も/$qへリダイレクトされる' => sub {
393-
# /search?q=Acme::Bleach -> /Acme::Bleach -> /docs/modules/...
408+
subtest 'モジュール名を検索した場合、/docs/modules/へリダイレクトされる' => sub {
409+
# /search?q=Acme::Bleach -> /docs/modules/...
394410
$mech->get('/search?q=Acme::Bleach');
395411
is $mech->status, 200, 'status is 200';
396412
like $mech->title, qr/^Acme::Bleach/;
397413
$mech->base_like(qr{/docs/modules/Acme-Bleach-\d+\.\d+/Bleach\.pod$});
398414
};
399415

416+
subtest '存在しないものを検索した場合、404が返る' => sub {
417+
$mech->get('/search?q=DoesNotExist');
418+
is $mech->status, 404, 'status is 404';
419+
$mech->content_contains('DoesNotExist');
420+
$mech->content_contains('検索結果が見つかりませんでした');
421+
};
422+
400423
subtest 'qパラメータがない場合、400が返る' => sub {
401424
$mech->get('/search');
402425
is $mech->status, 400, 'status is 400';
@@ -406,6 +429,24 @@ subtest 'GET /search' => sub {
406429
$mech->get('/search?q= ');
407430
is $mech->status, 400, 'status is 400';
408431
};
432+
433+
subtest '特殊なクエリのテスト' => sub {
434+
# 数値のみ
435+
$mech->get('/search?q=123');
436+
is $mech->status, 404, 'status is 404 for numeric query';
437+
438+
# falsy値 "0"
439+
$mech->get('/search?q=0');
440+
is $mech->status, 404, 'status is 404 for falsy value 0';
441+
442+
# URL形式
443+
$mech->get('/search?q=https://example.com');
444+
is $mech->status, 404, 'status is 404 for URL-like query';
445+
446+
# 既存のページへのリクエスト
447+
$mech->get('/search?q=about');
448+
is $mech->status, 404, 'status is 404 for existing page name query';
449+
};
409450
};
410451

411452
done_testing;

0 commit comments

Comments
 (0)