2 # Copyright (C) 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
23 Content-Type: text/plain; charset=iso-8859-1
28 my $ibx = create_inbox 'v2-1', version => 2, indexlevel => 'medium',
29 tmpdir => "$tmpdir/v2", sub {
31 $im->add($eml) or BAIL_OUT;
32 $eml->body_set("hello world!\n");
34 local $SIG{__WARN__} = sub { push @warn, @_ };
35 $eml->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
36 $im->add($eml) or BAIL_OUT;
37 is(scalar(@warn), 1, 'got one warning');
38 my $mids = mids($eml->header_obj);
39 $new_mid = $mids->[1];
40 open my $fh, '>', "$ibx->{inboxdir}/new_mid" or BAIL_OUT;
41 print $fh $new_mid or BAIL_OUT;
42 close $fh or BAIL_OUT;
45 open my $fh, '<', "$ibx->{inboxdir}/new_mid" or BAIL_OUT;
49 my $cfgpath = "$ibx->{inboxdir}/pi_config";
51 open my $fh, '>', $cfgpath or BAIL_OUT $!;
52 print $fh <<EOF or BAIL_OUT $!;
53 [publicinbox "v2test"]
54 inboxdir = $ibx->{inboxdir}
55 address = $ibx->{-primary_address}
57 close $fh or BAIL_OUT;
60 my $msg = $ibx->msg_by_mid('a-mid@b');
61 like($$msg, qr/\AFrom oldbug/s,
62 '"From_" line stored to test old bug workaround');
63 my $cfg = PublicInbox::Config->new($cfgpath);
64 my $www = PublicInbox::WWW->new($cfg);
65 my ($res, $raw, @from_);
68 $res = $cb->(GET('/v2test/description'));
69 like($res->content, qr!\$INBOX_DIR/description missing!,
70 'got v2 description missing message');
71 $res = $cb->(GET('/v2test/a-mid@b/raw'));
72 is($res->header('Content-Type'), 'text/plain; charset=iso-8859-1',
73 'charset from message used');
75 unlike($raw, qr/^From oldbug/sm, 'buggy "From_" line omitted');
76 like($raw, qr/^hello world$/m, 'got first message');
77 like($raw, qr/^hello world!$/m, 'got second message');
78 @from_ = ($raw =~ m/^From /mg);
79 is(scalar(@from_), 2, 'two From_ lines');
81 $res = $cb->(GET("/v2test/$new_mid/raw"));
83 like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
84 @from_ = ($raw =~ m/^From /mg);
85 is(scalar(@from_), 1, 'only one From_ line');
87 # Atom feed should sort by Date: (if Received is missing)
88 $res = $cb->(GET('/v2test/new.atom'));
89 my @bodies = ($res->content =~ />(hello [^<]+)</mg);
90 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
91 'Atom ordering is chronological');
93 # new.html should sort by Date:, too (if Received is missing)
94 $res = $cb->(GET('/v2test/new.html'));
95 @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
96 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
97 'new.html ordering is chronological');
99 $res = $cb->(GET('/v2test/new.atom'));
100 my @dates = ($res->content =~ m!title><updated>([^<]+)</updated>!g);
101 is_deeply(\@dates, [ "1993-10-02T00:01:00Z", "1993-10-02T00:00:00Z" ],
102 'Date headers made it through');
104 test_psgi(sub { $www->call(@_) }, $client0);
105 my $env = { TMPDIR => $tmpdir, PI_CONFIG => $cfgpath };
106 test_httpd($env, $client0, 9);
108 $eml->header_set('Message-ID', 'a-mid@b');
109 $eml->body_set("hello ghosts\n");
110 my $im = $ibx->importer(0);
113 local $SIG{__WARN__} = sub { push @warn, @_ };
114 ok($im->add($eml), 'added 3rd duplicate-but-different message');
115 is(scalar(@warn), 1, 'got another warning');
116 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
118 my $mids = mids($eml->header_obj);
119 my $third = $mids->[-1];
124 $res = $cb->(GET('/v2test/_/text/config/raw'));
125 my $lm = $res->header('Last-Modified');
126 ok($lm, 'Last-Modified set w/ ->mm');
127 $lm = HTTP::Date::str2time($lm);
128 is($lm, $ibx->mm->created_at,
129 'Last-Modified for text/config/raw matches ->created_at');
132 $res = $cb->(GET("/v2test/$third/raw"));
133 $raw = $res->content;
134 like($raw, qr/^hello ghosts$/m, 'got third message');
135 @from_ = ($raw =~ m/^From /mg);
136 is(scalar(@from_), 1, 'one From_ line');
138 $res = $cb->(GET('/v2test/a-mid@b/raw'));
139 $raw = $res->content;
140 like($raw, qr/^hello world$/m, 'got first message');
141 like($raw, qr/^hello world!$/m, 'got second message');
142 like($raw, qr/^hello ghosts$/m, 'got third message');
143 @from_ = ($raw =~ m/^From /mg);
144 is(scalar(@from_), 3, 'three From_ lines');
145 $cfg->each_inbox(sub { $_[0]->search->reopen });
148 eval { require IO::Uncompress::Gunzip };
149 skip 'IO::Uncompress::Gunzip missing', 6 if $@;
150 my ($in, $out, $status);
151 my $req = GET('/v2test/a-mid@b/raw');
152 $req->header('Accept-Encoding' => 'gzip');
154 is($res->header('Content-Encoding'), 'gzip', 'gzip encoding');
156 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
157 is($out, $raw, 'gzip response matches');
159 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
161 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
162 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
163 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
164 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
165 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
166 @from_ = ($out =~ m/^From /mg);
167 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
170 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
172 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
173 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
174 like($out, qr/^hello world$/m, 'got first in mbox POST');
175 like($out, qr/^hello world!$/m, 'got second in mbox POST');
176 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
177 @from_ = ($out =~ m/^From /mg);
178 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
180 # all.mbox.gz interface
181 $res = $cb->(GET('/v2test/all.mbox.gz'));
183 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
184 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
185 like($out, qr/^hello world$/m, 'got first in all.mbox');
186 like($out, qr/^hello world!$/m, 'got second in all.mbox');
187 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
188 @from_ = ($out =~ m/^From /mg);
189 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
192 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
193 is($res->code, 200, 'success with threaded search');
194 my $raw = $res->content;
195 ok($raw =~ s/\A.*>Results 1-3 of 3\b//s, 'got all results');
196 my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ +(?:\d+\% )?(.+)$/gm);
197 is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
199 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=A'));
200 is($res->code, 200, 'success with Atom search');
202 require_mods(qw(XML::TreePP), 2);
203 my $t = XML::TreePP->new->parse($res->content);
204 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
205 'looks like an an Atom feed');
206 is(scalar @{$t->{feed}->{entry}}, 3, 'parsed three entries');
209 local $SIG{__WARN__} = 'DEFAULT';
210 $res = $cb->(GET('/v2test/a-mid@b/'));
211 $raw = $res->content;
212 like($raw, qr/WARNING: multiple messages have this Message-ID/,
213 'warned about duplicate Message-IDs');
214 like($raw, qr/^hello world$/m, 'got first message');
215 like($raw, qr/^hello world!$/m, 'got second message');
216 like($raw, qr/^hello ghosts$/m, 'got third message');
217 @from_ = ($raw =~ m/>From: /mg);
218 is(scalar(@from_), 3, 'three From: lines');
219 foreach my $mid ('a-mid@b', $new_mid, $third) {
220 like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
222 like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
225 test_psgi(sub { $www->call(@_) }, $client1);
226 test_httpd($env, $client1, 38);
229 my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
230 $eml->header_set('Message-Id', @$exp);
231 $eml->header_set('Subject', '4th dupe');
232 local $SIG{__WARN__} = sub {};
233 ok($im->add($eml), 'added one message');
235 my @h = $eml->header('Message-ID');
236 is_deeply($exp, \@h, 'reused existing Message-ID');
237 $cfg->each_inbox(sub { $_[0]->search->reopen });
242 my $res = $cb->(GET('/v2test/new.atom'));
243 my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
246 is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
248 $res = $cb->(GET('/v2test/reuse@mid/T/'));
249 $raw = $res->content;
250 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
251 my @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
252 is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
253 'duplicate messages share the same root');
255 $res = $cb->(GET('/v2test/reuse@mid/t/'));
256 $raw = $res->content;
257 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
259 $res = $cb->(GET('/v2test/0/info/refs'));
260 is($res->code, 200, 'got info refs for dumb clones');
261 $res = $cb->(GET('/v2test/0.git/info/refs'));
262 is($res->code, 200, 'got info refs for dumb clones w/ .git suffix');
263 $res = $cb->(GET('/v2test/info/refs'));
264 is($res->code, 404, 'v2 git URL w/o shard fails');
267 test_psgi(sub { $www->call(@_) }, $client2);
268 test_httpd($env, $client2, 8);
270 # ensure conflicted attachments can be resolved
271 local $SIG{__WARN__} = sub {};
272 foreach my $body (qw(old new)) {
273 $im->add(eml_load "t/psgi_v2-$body.eml") or BAIL_OUT;
277 $cfg->each_inbox(sub { $_[0]->search->reopen });
281 my $res = $cb->(GET('/v2test/a@dup/'));
282 my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
283 is(scalar(@links), 2, 'both attachment links exist');
284 isnt($links[0], $links[1], 'attachment links are different');
286 my $old = $cb->(GET('/v2test/' . $links[0]));
287 my $new = $cb->(GET('/v2test/' . $links[1]));
288 is($old->content, 'old', 'got expected old content');
289 is($new->content, 'new', 'got expected new content');
291 $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'.'000000'));
292 is($res->code, 404, '404 for out-of-range t= param');
294 local $SIG{__WARN__} = sub { push @warn, @_ };
295 $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'));
296 is_deeply(\@warn, [], 'no warnings on YYYYMMDD only');
298 test_psgi(sub { $www->call(@_) }, $client3);
299 test_httpd($env, $client3, 4);