2 # Copyright (C) 2017-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use IO::Uncompress::Gunzip qw(gunzip);
9 use PublicInbox::Config;
10 use PublicInbox::Inbox;
11 use bytes (); # only for bytes::length
12 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
13 URI::Escape Plack::Builder);
15 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
16 use_ok 'PublicInbox::WWW';
17 use_ok 'PublicInbox::SearchIdx';
18 my ($tmpdir, $for_destroy) = tmpdir();
19 local $ENV{TZ} = 'UTC';
21 my $digits = '10010260936330';
22 my $ua = 'Pine.LNX.4.10';
23 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
24 my $ibx = create_inbox 'git', indexlevel => 'full', tmpdir => "$tmpdir/1", sub {
26 # n.b. these headers are not properly RFC2047-encoded
27 $im->add(PublicInbox::Eml->new(<<EOF)) or BAIL_OUT;
30 From: Ævar Arnfjörð Bjarmason <avarab\@example>
31 To: git\@vger.kernel.org
35 $im->add(PublicInbox::Eml->new(<<"")) or BAIL_OUT;
36 Message-ID: <reply\@asdf>
37 From: replier <r\@example.com>
41 $im->add(PublicInbox::Eml->new(<<'EOF')) or BAIL_OUT;
43 Message-ID: <blank-subject@example.com>
44 From: blank subject <blank-subject@example.com>
45 To: git@vger.kernel.org
49 $im->add(PublicInbox::Eml->new(<<'EOF')) or BAIL_OUT;
50 Message-ID: <no-subject-at-all@example.com>
51 From: no subject at all <no-subject-at-all@example.com>
52 To: git@vger.kernel.org
57 my $cfgpfx = "publicinbox.test";
58 my $cfg = PublicInbox::Config->new(\<<EOF);
59 $cfgpfx.address=git\@vger.kernel.org
60 $cfgpfx.inboxdir=$ibx->{inboxdir}
62 my $www = PublicInbox::WWW->new($cfg);
63 test_psgi(sub { $www->call(@_) }, sub {
66 my $approxidate = 'now';
67 for my $req ('/test/?q=%C3%86var', '/test/?q=%25C3%2586var') {
68 $res = $cb->(GET($req."+d:..$approxidate"));
69 $html = $res->content;
70 like($html, qr/<title>Ævar d:\.\.\Q$approxidate\E/,
71 'HTML escaped in title, "d:..$APPROXIDATE" preserved');
72 my @res = ($html =~ m/\?q=(.+var)\+d:\.\.\Q$approxidate\E/g);
73 ok(scalar(@res), 'saw query strings');
74 my %uniq = map { $_ => 1 } @res;
75 is(1, scalar keys %uniq, 'all query values identical in HTML');
76 is('%C3%86var', (keys %uniq)[0], 'matches original query');
77 ok(index($html, 'by Ævar Arnfjörð Bjarmason')
78 >= 0, "displayed Ævar's name properly in HTML");
79 like($html, qr/download mbox\.gz: .*?"full threads"/s,
80 '"full threads" download option shown');
82 like($html, qr/Initial query\b.*?returned no.results, used:.*instead/s,
83 'noted retry on double-escaped query {-uxs_retried}');
86 local $SIG{__WARN__} = sub { push @$warn, @_ };
87 $res = $cb->(GET('/test/?q=s:test&l=5e'));
88 is($res->code, 200, 'successful search result');
89 is_deeply([], $warn, 'no warnings from non-numeric comparison');
91 $res = $cb->(POST('/test/?q=s:bogus&x=m'));
92 is($res->code, 404, 'failed search result gives 404');
93 is_deeply([], $warn, 'no warnings');
95 my $mid_re = qr/\Q$mid\E/o;
96 while (length($digits) > 8) {
97 $res = $cb->(GET("/test/$ua.$digits/"));
98 is($res->code, 300, 'partial match found while truncated');
99 like($res->content, qr/\b1 partial match found\b/);
100 like($res->content, $mid_re, 'found mid in response');
104 $res = $cb->(GET('/test/'));
105 $html = $res->content;
106 like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
107 'subject-less message linked from "/$INBOX/"');
108 like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
109 'blank subject message linked from "/$INBOX/"');
110 like($html, qr/test Ævar/,
111 "displayed Ævar's name properly in topic view");
113 $res = $cb->(GET('/test/?q=tc:git'));
114 like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
115 'subject-less message linked from "/$INBOX/?q=..."');
116 like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
117 'blank subject message linked from "/$INBOX/?q=..."');
118 $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
119 like($res->header('Content-Disposition'),
120 qr/filename=no-subject\.txt/);
121 $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
122 like($res->header('Content-Disposition'),
123 qr/filename=no-subject\.mbox\.gz/);
125 # "full threads" mbox.gz download
126 $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
127 is($res->code, 200, 'successful mbox download with threads');
128 gunzip(\($res->content) => \(my $before));
129 is_deeply([ "Message-ID: <$mid>\n", "Message-ID: <reply\@asdf>\n" ],
130 [ grep(/^Message-ID:/m, split(/^/m, $before)) ],
133 # clobber has_threadid to emulate old versions:
135 my $sidx = PublicInbox::SearchIdx->new($ibx, 0);
136 my $xdb = $sidx->idx_acquire;
137 $xdb->set_metadata('has_threadid', '0');
140 $cfg->each_inbox(sub { delete $_[0]->{search} });
141 $res = $cb->(GET('/test/?q=s:test'));
142 is($res->code, 200, 'successful search w/o has_threadid');
143 unlike($html, qr/download mbox\.gz: .*?"full threads"/s,
144 '"full threads" download option not shown w/o has_threadid');
146 # in case somebody uses curl to bypass <form>
147 $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
148 is($res->code, 200, 'successful mbox download w/ threads');
149 gunzip(\($res->content) => \(my $after));
150 isnt($before, $after);