]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_search.t
www_coderepo: tree: quiet and 404 on non-existent refs
[public-inbox.git] / t / psgi_search.t
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 strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 use IO::Uncompress::Gunzip qw(gunzip);
8 use PublicInbox::Eml;
9 use PublicInbox::Config;
10 use PublicInbox::Inbox;
11 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
12                 URI::Escape Plack::Builder);
13 require_mods(@mods);
14 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
15 use_ok 'PublicInbox::WWW';
16 use_ok 'PublicInbox::SearchIdx';
17 my ($tmpdir, $for_destroy) = tmpdir();
18 local $ENV{TZ} = 'UTC';
19
20 my $digits = '10010260936330';
21 my $ua = 'Pine.LNX.4.10';
22 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
23 my $ibx = create_inbox 'git', indexlevel => 'full', tmpdir => "$tmpdir/1", sub {
24         my ($im) = @_;
25         # n.b. these headers are not properly RFC2047-encoded
26         $im->add(PublicInbox::Eml->new(<<EOF)) or BAIL_OUT;
27 Subject: test Ævar
28 Message-ID: <$mid>
29 From: Ævar Arnfjörð Bjarmason <avarab\@example>
30 To: git\@vger.kernel.org
31
32 EOF
33
34         $im->add(PublicInbox::Eml->new(<<"")) or BAIL_OUT;
35 Message-ID: <reply\@asdf>
36 From: replier <r\@example.com>
37 In-Reply-To: <$mid>
38 Subject: mismatch
39
40         $im->add(PublicInbox::Eml->new(<<'EOF')) or BAIL_OUT;
41 Subject:
42 Message-ID: <blank-subject@example.com>
43 From: blank subject <blank-subject@example.com>
44 To: git@vger.kernel.org
45
46 EOF
47
48         $im->add(PublicInbox::Eml->new(<<'EOF')) or BAIL_OUT;
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 };
55
56 my $cfgpfx = "publicinbox.test";
57 my $cfg = PublicInbox::Config->new(\<<EOF);
58 $cfgpfx.address=git\@vger.kernel.org
59 $cfgpfx.inboxdir=$ibx->{inboxdir}
60 EOF
61 my $www = PublicInbox::WWW->new($cfg);
62 test_psgi(sub { $www->call(@_) }, sub {
63         my ($cb) = @_;
64         my ($html, $res);
65         my $approxidate = 'now';
66         for my $req ('/test/?q=%C3%86var', '/test/?q=%25C3%2586var') {
67                 $res = $cb->(GET($req."+d:..$approxidate"));
68                 $html = $res->content;
69                 like($html, qr/<title>&#198;var d:\.\.\Q$approxidate\E/,
70                         'HTML escaped in title, "d:..$APPROXIDATE" preserved');
71                 my @res = ($html =~ m/\?q=(.+var)\+d:\.\.\Q$approxidate\E/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')
77                         >= 0, "displayed Ævar's name properly in HTML");
78                 like($html, qr/download mbox\.gz: .*?"full threads"/s,
79                         '"full threads" download option shown');
80         }
81         like($html, qr/Initial query\b.*?returned no.results, used:.*instead/s,
82                 'noted retry on double-escaped query {-uxs_retried}');
83
84         my $warn = [];
85         local $SIG{__WARN__} = sub { push @$warn, @_ };
86         $res = $cb->(GET('/test/?q=s:test&l=5e'));
87         is($res->code, 200, 'successful search result');
88         is_deeply([], $warn, 'no warnings from non-numeric comparison');
89
90         $res = $cb->(GET('/test/?&q=s:test'));
91         is($res->code, 200, 'successful search result');
92         is_deeply([], $warn, 'no warnings from black parameter');
93
94         $res = $cb->(POST('/test/?q=s:bogus&x=m'));
95         is($res->code, 404, 'failed search result gives 404');
96         like($res->content, qr/No results found/, "`No results' shown");
97         is_deeply([], $warn, 'no warnings');
98
99         my $mid_re = qr/\Q$mid\E/o;
100         while (length($digits) > 8) {
101                 $res = $cb->(GET("/test/$ua.$digits/"));
102                 is($res->code, 300, 'partial match found while truncated');
103                 like($res->content, qr/\b1 partial match found\b/);
104                 like($res->content, $mid_re, 'found mid in response');
105                 chop($digits);
106         }
107         $res = $cb->(GET("/test/$mid/"));
108         $html = $res->content;
109         like($html, qr/\bFrom: &#198;var /,
110                 "displayed Ævar's name properly in permalink From:");
111         unlike($html, qr/&#195;/, 'no raw octets in permalink HTML');
112
113         $res = $cb->(GET('/test/'));
114         $html = $res->content;
115         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
116                 'subject-less message linked from "/$INBOX/"');
117         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
118                 'blank subject message linked from "/$INBOX/"');
119         like($html, qr/test &#198;var/,
120                 "displayed Ævar's name properly in topic view");
121
122         $res = $cb->(GET('/test/?q=tc:git'));
123         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
124                 'subject-less message linked from "/$INBOX/?q=..."');
125         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
126                 'blank subject message linked from "/$INBOX/?q=..."');
127         $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
128         like($res->header('Content-Disposition'),
129                 qr/filename=no-subject\.txt/);
130         $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
131         like($res->header('Content-Disposition'),
132                 qr/filename=no-subject\.mbox\.gz/);
133
134         # "full threads" mbox.gz download
135         $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
136         is($res->code, 200, 'successful mbox download with threads');
137         gunzip(\($res->content) => \(my $before));
138         is_deeply([ "Message-ID: <$mid>\n", "Message-ID: <reply\@asdf>\n" ],
139                 [ grep(/^Message-ID:/m, split(/^/m, $before)) ],
140                 'got full thread');
141
142         # clobber has_threadid to emulate old versions:
143         {
144                 my $sidx = PublicInbox::SearchIdx->new($ibx, 0);
145                 my $xdb = $sidx->idx_acquire;
146                 $xdb->set_metadata('has_threadid', '0');
147                 $sidx->idx_release;
148         }
149         $cfg->each_inbox(sub { delete $_[0]->{search} });
150         $res = $cb->(GET('/test/?q=s:test'));
151         is($res->code, 200, 'successful search w/o has_threadid');
152         unlike($html, qr/download mbox\.gz: .*?"full threads"/s,
153                 '"full threads" download option not shown w/o has_threadid');
154
155         # in case somebody uses curl to bypass <form>
156         $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
157         is($res->code, 200, 'successful mbox download w/ threads');
158         gunzip(\($res->content) => \(my $after));
159         isnt($before, $after);
160 });
161
162 done_testing();