]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_search.t
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / t / psgi_search.t
1 # Copyright (C) 2017-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use PublicInbox::Config;
8 use PublicInbox::Inbox;
9 use PublicInbox::InboxWritable;
10 use bytes (); # only for bytes::length
11 use PublicInbox::TestCommon;
12 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
13                 URI::Escape Plack::Builder);
14 require_mods(@mods);
15 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
16 use_ok 'PublicInbox::WWW';
17 my ($tmpdir, $for_destroy) = tmpdir();
18
19 my $ibx = PublicInbox::Inbox->new({
20         inboxdir => $tmpdir,
21         address => 'git@vger.kernel.org',
22         name => 'test',
23 });
24 $ibx = PublicInbox::InboxWritable->new($ibx);
25 $ibx->init_inbox(1);
26 my $im = $ibx->importer(0);
27 my $digits = '10010260936330';
28 my $ua = 'Pine.LNX.4.10';
29 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
30 my $mime = PublicInbox::MIME->new(<<EOF);
31 Subject: test
32 Message-ID: <$mid>
33 From: Ævar Arnfjörð Bjarmason <avarab\@example>
34 To: git\@vger.kernel.org
35
36 EOF
37 $im->add($mime);
38
39 $mime = PublicInbox::MIME->new(<<'EOF');
40 Subject:
41 Message-ID: <blank-subject@example.com>
42 From: blank subject <blank-subject@example.com>
43 To: git@vger.kernel.org
44
45 EOF
46 $im->add($mime);
47
48 $mime = PublicInbox::MIME->new(<<'EOF');
49 Message-ID: <no-subject-at-all@example.com>
50 From: no subject at all <no-subject-at-all@example.com>
51 To: git@vger.kernel.org
52
53 EOF
54 $im->add($mime);
55
56 $im->done;
57 PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
58
59 my $cfgpfx = "publicinbox.test";
60 my $config = PublicInbox::Config->new(\<<EOF);
61 $cfgpfx.address=git\@vger.kernel.org
62 $cfgpfx.inboxdir=$tmpdir
63 EOF
64 my $www = PublicInbox::WWW->new($config);
65 test_psgi(sub { $www->call(@_) }, sub {
66         my ($cb) = @_;
67         my $res;
68         $res = $cb->(GET('/test/?q=%C3%86var'));
69         my $html = $res->content;
70         like($html, qr/<title>&#198;var - /, 'HTML escaped in title');
71         my @res = ($html =~ m/\?q=(.+var)\b/g);
72         ok(scalar(@res), 'saw query strings');
73         my %uniq = map { $_ => 1 } @res;
74         is(1, scalar keys %uniq, 'all query values identical in HTML');
75         is('%C3%86var', (keys %uniq)[0], 'matches original query');
76         ok(index($html, 'by &#198;var Arnfj&#246;r&#240; Bjarmason') >= 0,
77                 "displayed Ævar's name properly in HTML");
78
79         my $warn = [];
80         local $SIG{__WARN__} = sub { push @$warn, @_ };
81         $res = $cb->(GET('/test/?q=s:test&l=5e'));
82         is($res->code, 200, 'successful search result');
83         is_deeply([], $warn, 'no warnings from non-numeric comparison');
84
85         $res = $cb->(POST('/test/?q=s:bogus&x=m'));
86         is($res->code, 404, 'failed search result gives 404');
87         is_deeply([], $warn, 'no warnings');
88
89         my $mid_re = qr/\Q$mid\E/o;
90         while (length($digits) > 8) {
91                 $res = $cb->(GET("/test/$ua.$digits/"));
92                 is($res->code, 300, 'partial match found while truncated');
93                 like($res->content, qr/\b1 partial match found\b/);
94                 like($res->content, $mid_re, 'found mid in response');
95                 chop($digits);
96         }
97
98         $res = $cb->(GET('/test/'));
99         $html = $res->content;
100         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
101                 'subject-less message linked from "/$INBOX/"');
102         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
103                 'blank subject message linked from "/$INBOX/"');
104
105         $res = $cb->(GET('/test/?q=tc:git'));
106         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
107                 'subject-less message linked from "/$INBOX/?q=..."');
108         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
109                 'blank subject message linked from "/$INBOX/?q=..."');
110         $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
111         like($res->header('Content-Disposition'),
112                 qr/filename=no-subject\.txt/);
113         $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
114         like($res->header('Content-Disposition'),
115                 qr/filename=no-subject\.mbox\.gz/);
116 });
117
118 done_testing();
119
120 1;