2 # Copyright (C) 2018-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;
9 use PublicInbox::Config;
10 use PublicInbox::MID qw(mids);
11 require_mods(qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
12 URI::Escape Plack::Builder HTTP::Date));
13 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
14 use_ok 'PublicInbox::WWW';
15 my ($tmpdir, $for_destroy) = tmpdir();
16 my $eml = PublicInbox::Eml->new(<<'EOF');
17 From oldbug-pre-a0c07cba0e5d8b6a Fri Oct 2 00:00:00 1993
20 Subject: this is a subject
22 Date: Fri, 02 Oct 1993 00:00:00 +0000
27 my $ibx = create_inbox 'v2', version => 2, indexlevel => 'medium',
28 tmpdir => "$tmpdir/v2", sub {
30 $im->add($eml) or BAIL_OUT;
31 $eml->body_set("hello world!\n");
33 local $SIG{__WARN__} = sub { push @warn, @_ };
34 $eml->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
35 $im->add($eml) or BAIL_OUT;
36 is(scalar(@warn), 1, 'got one warning');
37 my $mids = mids($eml->header_obj);
38 $new_mid = $mids->[1];
39 open my $fh, '>', "$ibx->{inboxdir}/new_mid" or BAIL_OUT;
40 print $fh $new_mid or BAIL_OUT;
41 close $fh or BAIL_OUT;
44 open my $fh, '<', "$ibx->{inboxdir}/new_mid" or BAIL_OUT;
48 my $cfgpath = "$ibx->{inboxdir}/pi_config";
50 open my $fh, '>', $cfgpath or BAIL_OUT $!;
51 print $fh <<EOF or BAIL_OUT $!;
52 [publicinbox "v2test"]
53 inboxdir = $ibx->{inboxdir}
54 address = $ibx->{-primary_address}
56 close $fh or BAIL_OUT;
59 my $msg = $ibx->msg_by_mid('a-mid@b');
60 like($$msg, qr/\AFrom oldbug/s,
61 '"From_" line stored to test old bug workaround');
62 my $cfg = PublicInbox::Config->new($cfgpath);
63 my $www = PublicInbox::WWW->new($cfg);
64 my ($res, $raw, @from_);
67 $res = $cb->(GET('/v2test/description'));
68 like($res->content, qr!\$INBOX_DIR/description missing!,
69 'got v2 description missing message');
70 $res = $cb->(GET('/v2test/a-mid@b/raw'));
72 unlike($raw, qr/^From oldbug/sm, 'buggy "From_" line omitted');
73 like($raw, qr/^hello world$/m, 'got first message');
74 like($raw, qr/^hello world!$/m, 'got second message');
75 @from_ = ($raw =~ m/^From /mg);
76 is(scalar(@from_), 2, 'two From_ lines');
78 $res = $cb->(GET("/v2test/$new_mid/raw"));
80 like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
81 @from_ = ($raw =~ m/^From /mg);
82 is(scalar(@from_), 1, 'only one From_ line');
84 # Atom feed should sort by Date: (if Received is missing)
85 $res = $cb->(GET('/v2test/new.atom'));
86 my @bodies = ($res->content =~ />(hello [^<]+)</mg);
87 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
88 'Atom ordering is chronological');
90 # new.html should sort by Date:, too (if Received is missing)
91 $res = $cb->(GET('/v2test/new.html'));
92 @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
93 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
94 'new.html ordering is chronological');
96 test_psgi(sub { $www->call(@_) }, $client0);
97 my $env = { TMPDIR => $tmpdir, PI_CONFIG => $cfgpath };
98 test_httpd($env, $client0, 9);
100 $eml->header_set('Message-ID', 'a-mid@b');
101 $eml->body_set("hello ghosts\n");
102 my $im = $ibx->importer(0);
105 local $SIG{__WARN__} = sub { push @warn, @_ };
106 ok($im->add($eml), 'added 3rd duplicate-but-different message');
107 is(scalar(@warn), 1, 'got another warning');
108 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
110 my $mids = mids($eml->header_obj);
111 my $third = $mids->[-1];
116 $res = $cb->(GET('/v2test/_/text/config/raw'));
117 my $lm = $res->header('Last-Modified');
118 ok($lm, 'Last-Modified set w/ ->mm');
119 $lm = HTTP::Date::str2time($lm);
120 is($lm, $ibx->mm->created_at,
121 'Last-Modified for text/config/raw matches ->created_at');
124 $res = $cb->(GET("/v2test/$third/raw"));
125 $raw = $res->content;
126 like($raw, qr/^hello ghosts$/m, 'got third message');
127 @from_ = ($raw =~ m/^From /mg);
128 is(scalar(@from_), 1, 'one From_ line');
130 $res = $cb->(GET('/v2test/a-mid@b/raw'));
131 $raw = $res->content;
132 like($raw, qr/^hello world$/m, 'got first message');
133 like($raw, qr/^hello world!$/m, 'got second message');
134 like($raw, qr/^hello ghosts$/m, 'got third message');
135 @from_ = ($raw =~ m/^From /mg);
136 is(scalar(@from_), 3, 'three From_ lines');
137 $cfg->each_inbox(sub { $_[0]->search->reopen });
140 eval { require IO::Uncompress::Gunzip };
141 skip 'IO::Uncompress::Gunzip missing', 6 if $@;
142 my ($in, $out, $status);
143 my $req = GET('/v2test/a-mid@b/raw');
144 $req->header('Accept-Encoding' => 'gzip');
146 is($res->header('Content-Encoding'), 'gzip', 'gzip encoding');
148 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
149 is($out, $raw, 'gzip response matches');
151 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
153 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
154 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
155 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
156 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
157 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
158 @from_ = ($out =~ m/^From /mg);
159 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
162 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
164 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
165 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
166 like($out, qr/^hello world$/m, 'got first in mbox POST');
167 like($out, qr/^hello world!$/m, 'got second in mbox POST');
168 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
169 @from_ = ($out =~ m/^From /mg);
170 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
172 # all.mbox.gz interface
173 $res = $cb->(GET('/v2test/all.mbox.gz'));
175 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
176 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
177 like($out, qr/^hello world$/m, 'got first in all.mbox');
178 like($out, qr/^hello world!$/m, 'got second in all.mbox');
179 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
180 @from_ = ($out =~ m/^From /mg);
181 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
184 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
185 is($res->code, 200, 'success with threaded search');
186 my $raw = $res->content;
187 ok($raw =~ s/\A.*>Results 1-3 of 3\b//s, 'got all results');
188 my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ +(?:\d+\% )?(.+)$/gm);
189 is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
191 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=A'));
192 is($res->code, 200, 'success with Atom search');
194 require_mods(qw(XML::TreePP), 2);
195 my $t = XML::TreePP->new->parse($res->content);
196 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
197 'looks like an an Atom feed');
198 is(scalar @{$t->{feed}->{entry}}, 3, 'parsed three entries');
201 local $SIG{__WARN__} = 'DEFAULT';
202 $res = $cb->(GET('/v2test/a-mid@b/'));
203 $raw = $res->content;
204 like($raw, qr/^hello world$/m, 'got first message');
205 like($raw, qr/^hello world!$/m, 'got second message');
206 like($raw, qr/^hello ghosts$/m, 'got third message');
207 @from_ = ($raw =~ m/>From: /mg);
208 is(scalar(@from_), 3, 'three From: lines');
209 foreach my $mid ('a-mid@b', $new_mid, $third) {
210 like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
212 like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
215 test_psgi(sub { $www->call(@_) }, $client1);
216 test_httpd($env, $client1, 38);
219 my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
220 $eml->header_set('Message-Id', @$exp);
221 $eml->header_set('Subject', '4th dupe');
222 local $SIG{__WARN__} = sub {};
223 ok($im->add($eml), 'added one message');
225 my @h = $eml->header('Message-ID');
226 is_deeply($exp, \@h, 'reused existing Message-ID');
227 $cfg->each_inbox(sub { $_[0]->search->reopen });
232 my $res = $cb->(GET('/v2test/new.atom'));
233 my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
236 is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
238 $res = $cb->(GET('/v2test/reuse@mid/T/'));
239 $raw = $res->content;
240 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
241 my @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
242 is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
243 'duplicate messages share the same root');
245 $res = $cb->(GET('/v2test/reuse@mid/t/'));
246 $raw = $res->content;
247 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
249 $res = $cb->(GET('/v2test/0/info/refs'));
250 is($res->code, 200, 'got info refs for dumb clones');
251 $res = $cb->(GET('/v2test/0.git/info/refs'));
252 is($res->code, 200, 'got info refs for dumb clones w/ .git suffix');
253 $res = $cb->(GET('/v2test/info/refs'));
254 is($res->code, 404, 'v2 git URL w/o shard fails');
257 test_psgi(sub { $www->call(@_) }, $client2);
258 test_httpd($env, $client2, 8);
260 # ensure conflicted attachments can be resolved
261 local $SIG{__WARN__} = sub {};
262 foreach my $body (qw(old new)) {
263 $im->add(eml_load "t/psgi_v2-$body.eml") or BAIL_OUT;
267 $cfg->each_inbox(sub { $_[0]->search->reopen });
271 my $res = $cb->(GET('/v2test/a@dup/'));
272 my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
273 is(scalar(@links), 2, 'both attachment links exist');
274 isnt($links[0], $links[1], 'attachment links are different');
276 my $old = $cb->(GET('/v2test/' . $links[0]));
277 my $new = $cb->(GET('/v2test/' . $links[1]));
278 is($old->content, 'old', 'got expected old content');
279 is($new->content, 'new', 'got expected new content');
281 $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'.'000000'));
282 is($res->code, 404, '404 for out-of-range t= param');
284 local $SIG{__WARN__} = sub { push @warn, @_ };
285 $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'));
286 is_deeply(\@warn, [], 'no warnings on YYYYMMDD only');
288 test_psgi(sub { $www->call(@_) }, $client3);
289 test_httpd($env, $client3, 4);