]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_v2.t
www: diff: fix encoding problems when showing diff
[public-inbox.git] / t / psgi_v2.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 require_git(2.6);
8 use PublicInbox::Eml;
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 $enc_dup = 'ref-20150309094050.GO3427@x1.example';
17
18 my $dibx = create_inbox 'v2-dup', version => 2, indexlevel => 'medium',
19                         tmpdir => "$tmpdir/dup", sub {
20         my ($im, $ibx) = @_;
21         my $common = <<"";
22 Date: Mon, 9 Mar 2015 09:40:50 +0000
23 From: x\@example.com
24 To: y\@example.com
25 Subject: re
26 Message-ID: <$enc_dup>
27 MIME-Version: 1.0
28
29         $im->add(PublicInbox::Eml->new($common.<<EOM)) or BAIL_OUT;
30 Content-Type: text/plain; charset=utf-8
31 Content-Disposition: inline
32 Content-Transfer-Encoding: 8bit
33
34 cr_mismatch
35 pipe \x{e2}\x{94}\x{82} or not
36 EOM
37         $im->add(PublicInbox::Eml->new($common.<<EOM)) or BAIL_OUT;
38 Content-Type: text/plain; charset="windows-1252"
39 Content-Transfer-Encoding: quoted-printable
40
41 cr_mismatch\r
42 pipe =E2=94=82 or not
43 EOM
44 };
45
46 my $eml = PublicInbox::Eml->new(<<'EOF');
47 From oldbug-pre-a0c07cba0e5d8b6a Fri Oct  2 00:00:00 1993
48 From: a@example.com
49 To: test@example.com
50 Subject: this is a subject
51 Message-ID: <a-mid@b>
52 Date: Fri, 02 Oct 1993 00:00:00 +0000
53 Content-Type: text/plain; charset=iso-8859-1
54
55 hello world
56 EOF
57 my $new_mid;
58 my $ibx = create_inbox 'v2-1', version => 2, indexlevel => 'medium',
59                         tmpdir => "$tmpdir/v2", sub {
60         my ($im, $ibx) = @_;
61         $im->add($eml) or BAIL_OUT;
62         $eml->body_set("hello world!\n");
63         my @warn;
64         local $SIG{__WARN__} = sub { push @warn, @_ };
65         $eml->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
66         $im->add($eml) or BAIL_OUT;
67         is(scalar(@warn), 1, 'got one warning');
68         my $mids = mids($eml->header_obj);
69         $new_mid = $mids->[1];
70         open my $fh, '>', "$ibx->{inboxdir}/new_mid" or BAIL_OUT;
71         print $fh $new_mid or BAIL_OUT;
72         close $fh or BAIL_OUT;
73 };
74 $new_mid //= do {
75         open my $fh, '<', "$ibx->{inboxdir}/new_mid" or BAIL_OUT;
76         local $/;
77         <$fh>;
78 };
79 my $cfgpath = "$ibx->{inboxdir}/pi_config";
80 {
81         open my $fh, '>', $cfgpath or BAIL_OUT $!;
82         print $fh <<EOF or BAIL_OUT $!;
83 [publicinbox "v2test"]
84         inboxdir = $ibx->{inboxdir}
85         address = $ibx->{-primary_address}
86 [publicinbox "dup"]
87         inboxdir = $dibx->{inboxdir}
88         address = $dibx->{-primary_address}
89 EOF
90         close $fh or BAIL_OUT;
91 }
92
93 my $msg = $ibx->msg_by_mid('a-mid@b');
94 like($$msg, qr/\AFrom oldbug/s,
95         '"From_" line stored to test old bug workaround');
96 my $cfg = PublicInbox::Config->new($cfgpath);
97 my $www = PublicInbox::WWW->new($cfg);
98 my ($res, $raw, @from_);
99 my $client0 = sub {
100         my ($cb) = @_;
101         $res = $cb->(GET('/v2test/description'));
102         like($res->content, qr!\$INBOX_DIR/description missing!,
103                 'got v2 description missing message');
104         $res = $cb->(GET('/v2test/a-mid@b/raw'));
105         is($res->header('Content-Type'), 'text/plain; charset=iso-8859-1',
106                 'charset from message used');
107         $raw = $res->content;
108         unlike($raw, qr/^From oldbug/sm, 'buggy "From_" line omitted');
109         like($raw, qr/^hello world$/m, 'got first message');
110         like($raw, qr/^hello world!$/m, 'got second message');
111         @from_ = ($raw =~ m/^From /mg);
112         is(scalar(@from_), 2, 'two From_ lines');
113
114         $res = $cb->(GET("/v2test/$new_mid/raw"));
115         $raw = $res->content;
116         like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
117         @from_ = ($raw =~ m/^From /mg);
118         is(scalar(@from_), 1, 'only one From_ line');
119
120         # Atom feed should sort by Date: (if Received is missing)
121         $res = $cb->(GET('/v2test/new.atom'));
122         my @bodies = ($res->content =~ />(hello [^<]+)</mg);
123         is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
124                 'Atom ordering is chronological');
125
126         # new.html should sort by Date:, too (if Received is missing)
127         $res = $cb->(GET('/v2test/new.html'));
128         @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
129         is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
130                 'new.html ordering is chronological');
131
132         $res = $cb->(GET('/v2test/new.atom'));
133         my @dates = ($res->content =~ m!title><updated>([^<]+)</updated>!g);
134         is_deeply(\@dates, [ "1993-10-02T00:01:00Z", "1993-10-02T00:00:00Z" ],
135                 'Date headers made it through');
136 };
137 test_psgi(sub { $www->call(@_) }, $client0);
138 my $env = { TMPDIR => $tmpdir, PI_CONFIG => $cfgpath };
139 test_httpd($env, $client0, 9);
140
141 $eml->header_set('Message-ID', 'a-mid@b');
142 $eml->body_set("hello ghosts\n");
143 my $im = $ibx->importer(0);
144 {
145         my @warn;
146         local $SIG{__WARN__} = sub { push @warn, @_ };
147         ok($im->add($eml), 'added 3rd duplicate-but-different message');
148         is(scalar(@warn), 1, 'got another warning');
149         like($warn[0], qr/mismatched/, 'warned about mismatched messages');
150 }
151 my $mids = mids($eml->header_obj);
152 my $third = $mids->[-1];
153 $im->done;
154
155 my $client1 = sub {
156         my ($cb) = @_;
157         $res = $cb->(GET('/v2test/_/text/config/raw'));
158         my $lm = $res->header('Last-Modified');
159         ok($lm, 'Last-Modified set w/ ->mm');
160         $lm = HTTP::Date::str2time($lm);
161         is($lm, $ibx->mm->created_at,
162                 'Last-Modified for text/config/raw matches ->created_at');
163         delete $ibx->{mm};
164
165         $res = $cb->(GET("/v2test/$third/raw"));
166         $raw = $res->content;
167         like($raw, qr/^hello ghosts$/m, 'got third message');
168         @from_ = ($raw =~ m/^From /mg);
169         is(scalar(@from_), 1, 'one From_ line');
170
171         $res = $cb->(GET('/v2test/a-mid@b/raw'));
172         $raw = $res->content;
173         like($raw, qr/^hello world$/m, 'got first message');
174         like($raw, qr/^hello world!$/m, 'got second message');
175         like($raw, qr/^hello ghosts$/m, 'got third message');
176         @from_ = ($raw =~ m/^From /mg);
177         is(scalar(@from_), 3, 'three From_ lines');
178         $cfg->each_inbox(sub { $_[0]->search->reopen });
179
180         SKIP: {
181                 eval { require IO::Uncompress::Gunzip };
182                 skip 'IO::Uncompress::Gunzip missing', 6 if $@;
183                 my ($in, $out, $status);
184                 my $req = GET('/v2test/a-mid@b/raw');
185                 $req->header('Accept-Encoding' => 'gzip');
186                 $res = $cb->($req);
187                 is($res->header('Content-Encoding'), 'gzip', 'gzip encoding');
188                 $in = $res->content;
189                 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
190                 is($out, $raw, 'gzip response matches');
191
192                 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
193                 $in = $res->content;
194                 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
195                 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
196                 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
197                 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
198                 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
199                 @from_ = ($out =~ m/^From /mg);
200                 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
201
202                 # search interface
203                 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
204                 $in = $res->content;
205                 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
206                 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
207                 like($out, qr/^hello world$/m, 'got first in mbox POST');
208                 like($out, qr/^hello world!$/m, 'got second in mbox POST');
209                 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
210                 @from_ = ($out =~ m/^From /mg);
211                 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
212
213                 # all.mbox.gz interface
214                 $res = $cb->(GET('/v2test/all.mbox.gz'));
215                 $in = $res->content;
216                 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
217                 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
218                 like($out, qr/^hello world$/m, 'got first in all.mbox');
219                 like($out, qr/^hello world!$/m, 'got second in all.mbox');
220                 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
221                 @from_ = ($out =~ m/^From /mg);
222                 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
223         };
224
225         $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
226         is($res->code, 200, 'success with threaded search');
227         my $raw = $res->content;
228         ok($raw =~ s/\A.*>Results 1-3 of 3\b//s, 'got all results');
229         my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ +(?:\d+\% )?(.+)$/gm);
230         is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
231
232         $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=A'));
233         is($res->code, 200, 'success with Atom search');
234         SKIP: {
235                 require_mods(qw(XML::TreePP), 2);
236                 my $t = XML::TreePP->new->parse($res->content);
237                 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
238                         'looks like an an Atom feed');
239                 is(scalar @{$t->{feed}->{entry}}, 3, 'parsed three entries');
240         };
241
242         local $SIG{__WARN__} = 'DEFAULT';
243         $res = $cb->(GET('/v2test/a-mid@b/'));
244         $raw = $res->content;
245         like($raw, qr/WARNING: multiple messages have this Message-ID/,
246                 'warned about duplicate Message-IDs');
247         like($raw, qr/^hello world$/m, 'got first message');
248         like($raw, qr/^hello world!$/m, 'got second message');
249         like($raw, qr/^hello ghosts$/m, 'got third message');
250         @from_ = ($raw =~ m/>From: /mg);
251         is(scalar(@from_), 3, 'three From: lines');
252         foreach my $mid ('a-mid@b', $new_mid, $third) {
253                 like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
254         }
255         like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
256
257         $res = $cb->(GET("/dup/$enc_dup/d/"));
258         is($res->code, 200, '/d/ (diff) endpoint works');
259         $raw = $res->content;
260         like($raw, qr!</span> cr_mismatch\n!s,
261                 'cr_mismatch is only diff context');
262         like($raw, qr!>\-pipe !s, 'pipe diff del line');
263         like($raw, qr!>\+pipe !s, 'pipe diff ins line');
264 };
265
266 test_psgi(sub { $www->call(@_) }, $client1);
267 test_httpd($env, $client1, 38);
268
269 {
270         my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
271         $eml->header_set('Message-Id', @$exp);
272         $eml->header_set('Subject', '4th dupe');
273         local $SIG{__WARN__} = sub {};
274         ok($im->add($eml), 'added one message');
275         $im->done;
276         my @h = $eml->header('Message-ID');
277         is_deeply($exp, \@h, 'reused existing Message-ID');
278         $cfg->each_inbox(sub { $_[0]->search->reopen });
279 }
280
281 my $client2 = sub {
282         my ($cb) = @_;
283         my $res = $cb->(GET('/v2test/new.atom'));
284         my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
285         my %ids;
286         $ids{$_}++ for @ids;
287         is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
288
289         $res = $cb->(GET('/v2test/reuse@mid/T/'));
290         $raw = $res->content;
291         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
292         my @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
293         is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
294                 'duplicate messages share the same root');
295
296         $res = $cb->(GET('/v2test/reuse@mid/t/'));
297         $raw = $res->content;
298         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
299
300         $res = $cb->(GET('/v2test/0/info/refs'));
301         is($res->code, 200, 'got info refs for dumb clones');
302         $res = $cb->(GET('/v2test/0.git/info/refs'));
303         is($res->code, 200, 'got info refs for dumb clones w/ .git suffix');
304         $res = $cb->(GET('/v2test/info/refs'));
305         is($res->code, 404, 'v2 git URL w/o shard fails');
306 };
307
308 test_psgi(sub { $www->call(@_) }, $client2);
309 test_httpd($env, $client2, 8);
310 {
311         # ensure conflicted attachments can be resolved
312         local $SIG{__WARN__} = sub {};
313         foreach my $body (qw(old new)) {
314                 $im->add(eml_load "t/psgi_v2-$body.eml") or BAIL_OUT;
315         }
316         $im->done;
317 }
318 $cfg->each_inbox(sub { $_[0]->search->reopen });
319
320 my $client3 = sub {
321         my ($cb) = @_;
322         my $res = $cb->(GET('/v2test/a@dup/'));
323         my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
324         is(scalar(@links), 2, 'both attachment links exist');
325         isnt($links[0], $links[1], 'attachment links are different');
326         {
327                 my $old = $cb->(GET('/v2test/' . $links[0]));
328                 my $new = $cb->(GET('/v2test/' . $links[1]));
329                 is($old->content, 'old', 'got expected old content');
330                 is($new->content, 'new', 'got expected new content');
331         }
332         $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'.'000000'));
333         is($res->code, 404, '404 for out-of-range t= param');
334         my @warn = ();
335         local $SIG{__WARN__} = sub { push @warn, @_ };
336         $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'));
337         is_deeply(\@warn, [], 'no warnings on YYYYMMDD only');
338 };
339 test_psgi(sub { $www->call(@_) }, $client3);
340 test_httpd($env, $client3, 4);
341
342 done_testing;