1 # Copyright (C) 2017-2018 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;
10 use bytes (); # only for bytes::length
11 my @mods = qw(PublicInbox::SearchIdx HTTP::Request::Common Plack::Test
12 URI::Escape Plack::Builder);
13 foreach my $mod (@mods) {
15 plan skip_all => "$mod missing for psgi_search.t" if $@;
17 use_ok $_ foreach @mods;
18 my $tmpdir = tempdir('pi-psgi-search.XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 my $git_dir = "$tmpdir/a.git";
21 is(0, system(qw(git init -q --bare), $git_dir), "git init (main)");
22 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
23 ok($rw, "search indexer created");
24 my $digits = '10010260936330';
25 my $ua = 'Pine.LNX.4.10';
26 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
30 From: Ævar Arnfjörð Bjarmason <avarab\@example>
31 To: git\@vger.kernel.org
36 # nb. using internal API, fragile!
39 foreach (reverse split(/\n\n/, $data)) {
41 my $mime = Email::MIME->new(\$_);
42 my $bytes = bytes::length($mime->as_string);
43 my $doc_id = $rw->add_message($mime, $bytes, ++$num, 'ignored');
44 ok($doc_id, 'message added');
49 my $cfgpfx = "publicinbox.test";
50 my $config = PublicInbox::Config->new({
51 "$cfgpfx.address" => 'git@vger.kernel.org',
52 "$cfgpfx.mainrepo" => $git_dir,
54 my $www = PublicInbox::WWW->new($config);
55 test_psgi(sub { $www->call(@_) }, sub {
58 $res = $cb->(GET('/test/?q=%C3%86var'));
59 my $html = $res->content;
60 like($html, qr/<title>Ævar - /, 'HTML escaped in title');
61 my @res = ($html =~ m/\?q=(.+var)\b/g);
62 ok(scalar(@res), 'saw query strings');
63 my %uniq = map { $_ => 1 } @res;
64 is(1, scalar keys %uniq, 'all query values identical in HTML');
65 is('%C3%86var', (keys %uniq)[0], 'matches original query');
66 ok(index($html, 'by Ævar Arnfjörð Bjarmason') >= 0,
67 "displayed Ævar's name properly in HTML");
70 local $SIG{__WARN__} = sub { push @$warn, @_ };
71 $res = $cb->(GET('/test/?q=s:test&l=5e'));
72 is($res->code, 200, 'successful search result');
73 is_deeply([], $warn, 'no warnings from non-numeric comparison');
75 $res = $cb->(POST('/test/?q=s:bogus&x=m'));
76 is($res->code, 404, 'failed search result gives 404');
77 is_deeply([], $warn, 'no warnings');
79 my $mid_re = qr/\Q$mid\E/o;
80 while (length($digits) > 8) {
81 $res = $cb->(GET("/test/$ua.$digits/"));
82 is($res->code, 300, 'partial match found while truncated');
83 like($res->content, qr/\b1 partial match found\b/);
84 like($res->content, $mid_re, 'found mid in response');