lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / psgi_search.t
blob8c981c6cc593e78252335ac2c9bfdfcfbd4e1e03
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use PublicInbox::TestCommon;
6 use IO::Uncompress::Gunzip qw(gunzip);
7 use PublicInbox::Eml;
8 use PublicInbox::Inbox;
9 my @mods = qw(DBD::SQLite Xapian HTTP::Request::Common Plack::Test
10                 URI::Escape Plack::Builder);
11 require_mods(@mods);
12 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
13 use_ok 'PublicInbox::WWW';
14 use_ok 'PublicInbox::SearchIdx';
15 my ($tmpdir, $for_destroy) = tmpdir();
16 local $ENV{TZ} = 'UTC';
18 my $digits = '10010260936330';
19 my $ua = 'Pine.LNX.4.10';
20 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
21 my $ibx = create_inbox '26-git', indexlevel => 'full', tmpdir => "$tmpdir/1",
22 sub {
23         my ($im) = @_;
24         # n.b. these headers are not properly RFC2047-encoded
25         $im->add(PublicInbox::Eml->new(<<EOF)) or BAIL_OUT;
26 Subject: test Ævar
27 Message-ID: <$mid>
28 From: Ævar Arnfjörð Bjarmason <avarab\@example>
29 To: git\@vger.kernel.org
31 EOF
33         $im->add(PublicInbox::Eml->new(<<"")) or BAIL_OUT;
34 Message-ID: <reply\@asdf>
35 From: replier <r\@example.com>
36 In-Reply-To: <$mid>
37 Subject: mismatch
39         $im->add(PublicInbox::Eml->new(<<'EOF')) or BAIL_OUT;
40 Subject:
41 Message-ID: <blank-subject@example.com>
42 From: blank subject <blank-subject@example.com>
43 To: git@vger.kernel.org
45 EOF
47         $im->add(PublicInbox::Eml->new(<<'EOF')) or BAIL_OUT;
48 Message-ID: <no-subject-at-all@example.com>
49 From: no subject at all <no-subject-at-all@example.com>
50 To: git@vger.kernel.org
52 EOF
53         $im->add(PublicInbox::Eml->new(<<'EOF')) or BAIL_OUT;
54 Message-ID: <ampersand@example.com>
55 From: <e@example.com>
56 To: git@vger.kernel.org
57 Subject: git & ampersand
59 hi +++ b/foo
60 x=y
61 s'more
63 EOF
66 my $cfg = cfg_new $tmpdir, <<EOF;
67 [publicinbox "test"]
68         address = git\@vger.kernel.org
69         inboxdir = $ibx->{inboxdir}
70 EOF
71 my $www = PublicInbox::WWW->new($cfg);
72 test_psgi(sub { $www->call(@_) }, sub {
73         my ($cb) = @_;
74         my ($html, $res);
75         my $approxidate = 'now';
76         for my $req ('/test/?q=%C3%86var', '/test/?q=%25C3%2586var') {
77                 $res = $cb->(GET($req."+d:..$approxidate"));
78                 $html = $res->content;
79                 like($html, qr/<title>&#198;var d:\.\.\Q$approxidate\E/,
80                         'HTML escaped in title, "d:..$APPROXIDATE" preserved');
81                 my @res = ($html =~ m/\?q=(.+var)\+d:\.\.\Q$approxidate\E/g);
82                 ok(scalar(@res), 'saw query strings');
83                 my %uniq = map { $_ => 1 } @res;
84                 is(1, scalar keys %uniq, 'all query values identical in HTML');
85                 is('%C3%86var', (keys %uniq)[0], 'matches original query');
86                 ok(index($html, 'by &#198;var Arnfj&#246;r&#240; Bjarmason')
87                         >= 0, "displayed Ævar's name properly in HTML");
88                 like($html, qr/download mbox\.gz: .*?"full threads"/s,
89                         '"full threads" download option shown');
90         }
91         like($html, qr/Initial query\b.*?returned no.results, used:.*instead/s,
92                 'noted retry on double-escaped query {-uxs_retried}');
94         my $warn = [];
95         local $SIG{__WARN__} = sub { push @$warn, @_ };
96         $res = $cb->(GET('/test/?q=s:test&l=5e'));
97         is($res->code, 200, 'successful search result');
98         is_deeply([], $warn, 'no warnings from non-numeric comparison');
100         $res = $cb->(GET('/test/?&q=s:test'));
101         is($res->code, 200, 'successful search result');
102         is_deeply([], $warn, 'no warnings from black parameter');
104         $res = $cb->(POST('/test/?q=s:bogus&x=m'));
105         is($res->code, 404, 'failed search result gives 404');
106         like($res->content, qr/No results found/, "`No results' shown");
107         is_deeply([], $warn, 'no warnings');
109         my $mid_re = qr/\Q$mid\E/o;
110         while (length($digits) > 8) {
111                 $res = $cb->(GET("/test/$ua.$digits/"));
112                 is($res->code, 300, 'partial match found while truncated');
113                 like($res->content, qr/\b1 partial match found\b/);
114                 like($res->content, $mid_re, 'found mid in response');
115                 chop($digits);
116         }
117         $res = $cb->(GET("/test/$mid/"));
118         $html = $res->content;
119         like($html, qr/\bFrom: &#198;var /,
120                 "displayed Ævar's name properly in permalink From:");
121         unlike($html, qr/&#195;/, 'no raw octets in permalink HTML');
123         $res = $cb->(GET('/test/'));
124         $html = $res->content;
125         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
126                 'subject-less message linked from "/$INBOX/"');
127         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
128                 'blank subject message linked from "/$INBOX/"');
129         like($html, qr/test &#198;var/,
130                 "displayed Ævar's name properly in topic view");
132         $res = $cb->(GET('/test/?q=tc:git'));
133         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
134                 'subject-less message linked from "/$INBOX/?q=..."');
135         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
136                 'blank subject message linked from "/$INBOX/?q=..."');
137         $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
138         like($res->header('Content-Disposition'),
139                 qr/filename=no-subject\.txt/);
140         $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
141         like($res->header('Content-Disposition'),
142                 qr/filename=no-subject\.mbox\.gz/);
144         # "full threads" mbox.gz download
145         $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
146         is($res->code, 200, 'successful mbox download with threads');
147         gunzip(\($res->content) => \(my $before));
148         is_deeply([ "Message-ID: <$mid>\n", "Message-ID: <reply\@asdf>\n" ],
149                 [ grep(/^Message-ID:/m, split(/^/m, $before)) ],
150                 'got full thread');
152         # clobber has_threadid to emulate old versions:
153         {
154                 my $sidx = PublicInbox::SearchIdx->new($ibx, 0);
155                 my $xdb = $sidx->idx_acquire;
156                 $xdb->set_metadata('has_threadid', '0');
157                 $sidx->idx_release;
158         }
159         $cfg->each_inbox(sub { delete $_[0]->{search} });
160         $res = $cb->(GET('/test/?q=s:test'));
161         is($res->code, 200, 'successful search w/o has_threadid');
162         unlike($html, qr/download mbox\.gz: .*?"full threads"/s,
163                 '"full threads" download option not shown w/o has_threadid');
165         # in case somebody uses curl to bypass <form>
166         $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
167         is($res->code, 200, 'successful mbox download w/ threads');
168         gunzip(\($res->content) => \(my $after));
169         isnt($before, $after);
171         $res = $cb->(GET('/test/?q=git+%26+ampersand&x=A'));
172         is $res->code, 200, 'Atom hit with ampersand';
173         unlike $res->content, qr/git\+&\+ampersand/, '& is HTML-escaped';
175         $res = $cb->(GET('/test/?q=%22hi+%2b%2b%2b+b/foo%22&x=A'));
176         is $res->code, 200, 'slashes and plusses search hit';
177         like $res->content, qr!q=%22hi\+(?:%2[bB]){3}\+b/foo%22!,
178                 '+ and " escaped, but slash not escaped in query';
180         $res = $cb->(GET(q{/test/?q=%22s'more%22&x=A}));
181         is $res->code, 200, 'single quote inside phrase';
182         # TODO: more tests and odd cases
185 done_testing();