1 # Copyright (C) 2017-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use File::Temp qw/tempdir/;
8 use PublicInbox::Config;
9 use PublicInbox::Inbox;
10 use PublicInbox::InboxWritable;
12 use bytes (); # only for bytes::length
13 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
14 URI::Escape Plack::Builder);
15 foreach my $mod (@mods) {
17 plan skip_all => "$mod missing for psgi_search.t" if $@;
20 use_ok $_ foreach (@mods, qw(PublicInbox::SearchIdx));
21 my $tmpdir = tempdir('pi-psgi-search.XXXXXX', TMPDIR => 1, CLEANUP => 1);
23 my $ibx = PublicInbox::Inbox->new({
25 address => 'git@vger.kernel.org',
28 $ibx = PublicInbox::InboxWritable->new($ibx);
30 my $im = $ibx->importer(0);
31 my $digits = '10010260936330';
32 my $ua = 'Pine.LNX.4.10';
33 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
37 From: Ævar Arnfjörð Bjarmason <avarab\@example>
38 To: git\@vger.kernel.org
42 my $mime = Email::MIME->new(\$data);
45 PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
47 my $cfgpfx = "publicinbox.test";
48 my $config = PublicInbox::Config->new(\<<EOF);
49 $cfgpfx.address=git\@vger.kernel.org
50 $cfgpfx.inboxdir=$tmpdir
52 my $www = PublicInbox::WWW->new($config);
53 test_psgi(sub { $www->call(@_) }, sub {
56 $res = $cb->(GET('/test/?q=%C3%86var'));
57 my $html = $res->content;
58 like($html, qr/<title>Ævar - /, 'HTML escaped in title');
59 my @res = ($html =~ m/\?q=(.+var)\b/g);
60 ok(scalar(@res), 'saw query strings');
61 my %uniq = map { $_ => 1 } @res;
62 is(1, scalar keys %uniq, 'all query values identical in HTML');
63 is('%C3%86var', (keys %uniq)[0], 'matches original query');
64 ok(index($html, 'by Ævar Arnfjörð Bjarmason') >= 0,
65 "displayed Ævar's name properly in HTML");
68 local $SIG{__WARN__} = sub { push @$warn, @_ };
69 $res = $cb->(GET('/test/?q=s:test&l=5e'));
70 is($res->code, 200, 'successful search result');
71 is_deeply([], $warn, 'no warnings from non-numeric comparison');
73 $res = $cb->(POST('/test/?q=s:bogus&x=m'));
74 is($res->code, 404, 'failed search result gives 404');
75 is_deeply([], $warn, 'no warnings');
77 my $mid_re = qr/\Q$mid\E/o;
78 while (length($digits) > 8) {
79 $res = $cb->(GET("/test/$ua.$digits/"));
80 is($res->code, 300, 'partial match found while truncated');
81 like($res->content, qr/\b1 partial match found\b/);
82 like($res->content, $mid_re, 'found mid in response');