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 my @mods = qw(PublicInbox::SearchIdx HTTP::Request::Common Plack::Test
11 URI::Escape Plack::Builder);
12 foreach my $mod (@mods) {
14 plan skip_all => "$mod missing for psgi_search.t" if $@;
16 use_ok $_ foreach @mods;
17 my $tmpdir = tempdir('pi-psgi-search.XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 my $git_dir = "$tmpdir/a.git";
20 is(0, system(qw(git init -q --bare), $git_dir), "git init (main)");
21 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
22 ok($rw, "search indexer created");
25 Message-Id: <utf8@example>
26 From: Ævar Arnfjörð Bjarmason <avarab@example>
27 To: git@vger.kernel.org
32 # nb. using internal API, fragile!
33 my $xdb = $rw->_xdb_acquire;
34 $xdb->begin_transaction;
36 foreach (reverse split(/\n\n/, $data)) {
38 my $mime = Email::MIME->new(\$_);
39 my $bytes = bytes::length($mime->as_string);
40 my $doc_id = $rw->add_message($mime, $bytes, ++$num, 'ignored');
41 my $mid = $mime->header('Message-Id');
42 ok($doc_id, 'message added: '. $mid);
45 $xdb->commit_transaction;
48 my $cfgpfx = "publicinbox.test";
49 my $config = PublicInbox::Config->new({
50 "$cfgpfx.address" => 'git@vger.kernel.org',
51 "$cfgpfx.mainrepo" => $git_dir,
53 my $www = PublicInbox::WWW->new($config);
54 test_psgi(sub { $www->call(@_) }, sub {
57 $res = $cb->(GET('/test/?q=%C3%86var'));
58 my $html = $res->content;
59 like($html, qr/<title>Ævar - /, 'HTML escaped in title');
60 my @res = ($html =~ m/\?q=(.+var)\b/g);
61 ok(scalar(@res), 'saw query strings');
62 my %uniq = map { $_ => 1 } @res;
63 is(1, scalar keys %uniq, 'all query values identical in HTML');
64 is('%C3%86var', (keys %uniq)[0], 'matches original query');
65 ok(index($html, 'by Ævar Arnfjörð Bjarmason') >= 0,
66 "displayed Ævar's name properly in HTML");