]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_search.t
www_coderepo: implement /$CODE_REPO/atom/ endpoint
[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         is_deeply([], $warn, 'no warnings');
97
98         my $mid_re = qr/\Q$mid\E/o;
99         while (length($digits) > 8) {
100                 $res = $cb->(GET("/test/$ua.$digits/"));
101                 is($res->code, 300, 'partial match found while truncated');
102                 like($res->content, qr/\b1 partial match found\b/);
103                 like($res->content, $mid_re, 'found mid in response');
104                 chop($digits);
105         }
106         $res = $cb->(GET("/test/$mid/"));
107         $html = $res->content;
108         like($html, qr/\bFrom: &#198;var /,
109                 "displayed Ævar's name properly in permalink From:");
110         unlike($html, qr/&#195;/, 'no raw octets in permalink HTML');
111
112         $res = $cb->(GET('/test/'));
113         $html = $res->content;
114         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
115                 'subject-less message linked from "/$INBOX/"');
116         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
117                 'blank subject message linked from "/$INBOX/"');
118         like($html, qr/test &#198;var/,
119                 "displayed Ævar's name properly in topic view");
120
121         $res = $cb->(GET('/test/?q=tc:git'));
122         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
123                 'subject-less message linked from "/$INBOX/?q=..."');
124         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
125                 'blank subject message linked from "/$INBOX/?q=..."');
126         $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
127         like($res->header('Content-Disposition'),
128                 qr/filename=no-subject\.txt/);
129         $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
130         like($res->header('Content-Disposition'),
131                 qr/filename=no-subject\.mbox\.gz/);
132
133         # "full threads" mbox.gz download
134         $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
135         is($res->code, 200, 'successful mbox download with threads');
136         gunzip(\($res->content) => \(my $before));
137         is_deeply([ "Message-ID: <$mid>\n", "Message-ID: <reply\@asdf>\n" ],
138                 [ grep(/^Message-ID:/m, split(/^/m, $before)) ],
139                 'got full thread');
140
141         # clobber has_threadid to emulate old versions:
142         {
143                 my $sidx = PublicInbox::SearchIdx->new($ibx, 0);
144                 my $xdb = $sidx->idx_acquire;
145                 $xdb->set_metadata('has_threadid', '0');
146                 $sidx->idx_release;
147         }
148         $cfg->each_inbox(sub { delete $_[0]->{search} });
149         $res = $cb->(GET('/test/?q=s:test'));
150         is($res->code, 200, 'successful search w/o has_threadid');
151         unlike($html, qr/download mbox\.gz: .*?"full threads"/s,
152                 '"full threads" download option not shown w/o has_threadid');
153
154         # in case somebody uses curl to bypass <form>
155         $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
156         is($res->code, 200, 'successful mbox download w/ threads');
157         gunzip(\($res->content) => \(my $after));
158         isnt($before, $after);
159 });
160
161 done_testing();