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>
6 use IO::Uncompress::Gunzip qw(gunzip);
8 use PublicInbox::Config;
9 use PublicInbox::Inbox;
10 use PublicInbox::InboxWritable;
11 use bytes (); # only for bytes::length
12 use PublicInbox::TestCommon;
13 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
14 URI::Escape Plack::Builder);
16 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
17 use_ok 'PublicInbox::WWW';
18 use_ok 'PublicInbox::SearchIdx';
19 my ($tmpdir, $for_destroy) = tmpdir();
21 my $ibx = PublicInbox::Inbox->new({
23 address => 'git@vger.kernel.org',
26 $ibx = PublicInbox::InboxWritable->new($ibx);
28 my $im = $ibx->importer(0);
29 my $digits = '10010260936330';
30 my $ua = 'Pine.LNX.4.10';
31 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
33 # n.b. these headers are not properly RFC2047-encoded
34 my $mime = PublicInbox::Eml->new(<<EOF);
37 From: Ævar Arnfjörð Bjarmason <avarab\@example>
38 To: git\@vger.kernel.org
43 $im->add(PublicInbox::Eml->new(<<""));
44 Message-ID: <reply\@asdf>
45 From: replier <r\@example.com>
49 $mime = PublicInbox::Eml->new(<<'EOF');
51 Message-ID: <blank-subject@example.com>
52 From: blank subject <blank-subject@example.com>
53 To: git@vger.kernel.org
58 $mime = PublicInbox::Eml->new(<<'EOF');
59 Message-ID: <no-subject-at-all@example.com>
60 From: no subject at all <no-subject-at-all@example.com>
61 To: git@vger.kernel.org
67 PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
69 my $cfgpfx = "publicinbox.test";
70 my $config = PublicInbox::Config->new(\<<EOF);
71 $cfgpfx.address=git\@vger.kernel.org
72 $cfgpfx.inboxdir=$tmpdir
74 my $www = PublicInbox::WWW->new($config);
75 test_psgi(sub { $www->call(@_) }, sub {
78 $res = $cb->(GET('/test/?q=%C3%86var'));
79 my $html = $res->content;
80 like($html, qr/<title>Ævar - /, 'HTML escaped in title');
81 my @res = ($html =~ m/\?q=(.+var)\b/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 Ævar Arnfjörð Bjarmason') >= 0,
87 "displayed Ævar's name properly in HTML");
89 like($html, qr/download mbox\.gz: .*?"full threads"/s,
90 '"full threads" download option shown');
93 local $SIG{__WARN__} = sub { push @$warn, @_ };
94 $res = $cb->(GET('/test/?q=s:test&l=5e'));
95 is($res->code, 200, 'successful search result');
96 is_deeply([], $warn, 'no warnings from non-numeric comparison');
98 $res = $cb->(POST('/test/?q=s:bogus&x=m'));
99 is($res->code, 404, 'failed search result gives 404');
100 is_deeply([], $warn, 'no warnings');
102 my $mid_re = qr/\Q$mid\E/o;
103 while (length($digits) > 8) {
104 $res = $cb->(GET("/test/$ua.$digits/"));
105 is($res->code, 300, 'partial match found while truncated');
106 like($res->content, qr/\b1 partial match found\b/);
107 like($res->content, $mid_re, 'found mid in response');
111 $res = $cb->(GET('/test/'));
112 $html = $res->content;
113 like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
114 'subject-less message linked from "/$INBOX/"');
115 like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
116 'blank subject message linked from "/$INBOX/"');
117 like($html, qr/test Ævar/,
118 "displayed Ævar's name properly in topic view");
120 $res = $cb->(GET('/test/?q=tc:git'));
121 like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
122 'subject-less message linked from "/$INBOX/?q=..."');
123 like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
124 'blank subject message linked from "/$INBOX/?q=..."');
125 $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
126 like($res->header('Content-Disposition'),
127 qr/filename=no-subject\.txt/);
128 $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
129 like($res->header('Content-Disposition'),
130 qr/filename=no-subject\.mbox\.gz/);
132 # "full threads" mbox.gz download
133 $res = $cb->(POST('/test/?q=s:test&x=m&t'));
134 is($res->code, 200, 'successful mbox download with threads');
135 gunzip(\($res->content) => \(my $before));
136 is_deeply([ "Message-ID: <$mid>\n", "Message-ID: <reply\@asdf>\n" ],
137 [ grep(/^Message-ID:/m, split(/^/m, $before)) ],
140 # clobber has_threadid to emulate old versions:
142 my $sidx = PublicInbox::SearchIdx->new($ibx, 0);
143 my $xdb = $sidx->idx_acquire;
144 $xdb->set_metadata('has_threadid', '0');
147 $config->each_inbox(sub { delete $_[0]->{search} });
148 $res = $cb->(GET('/test/?q=s:test'));
149 is($res->code, 200, 'successful search w/o has_threadid');
150 unlike($html, qr/download mbox\.gz: .*?"full threads"/s,
151 '"full threads" download option not shown w/o has_threadid');
153 # in case somebody uses curl to bypass <form>
154 $res = $cb->(POST('/test/?q=s:test&x=m&t'));
155 is($res->code, 200, 'successful mbox download w/ threads');
156 gunzip(\($res->content) => \(my $after));
157 isnt($before, $after);