]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
testcommon: introduce mime_load sub
[public-inbox.git] / t / plack.t
1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use PublicInbox::TestCommon;
8 my $psgi = "./examples/public-inbox.psgi";
9 my ($tmpdir, $for_destroy) = tmpdir();
10 my $pi_config = "$tmpdir/config";
11 my $inboxdir = "$tmpdir/main.git";
12 my $addr = 'test-public@example.com';
13 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
14 require_mods(@mods);
15 use_ok 'PublicInbox::Import';
16 use_ok 'PublicInbox::Git';
17 my @ls;
18
19 foreach my $mod (@mods) { use_ok $mod; }
20 local $ENV{PI_CONFIG} = $pi_config;
21 ok(-f $psgi, "psgi example file found");
22 my $pfx = 'http://example.com/test';
23 ok(run_script(['-init', 'test', $inboxdir, "$pfx/", $addr]),
24         'initialized repo');
25 PublicInbox::Import::run_die([qw(git config -f), $pi_config,
26         'publicinbox.test.newsgroup', 'inbox.test']);
27 open my $fh, '>', "$inboxdir/description" or die "open: $!\n";
28 print $fh "test for public-inbox\n";
29 close $fh or die "close: $!\n";
30 my $app = require $psgi;
31 my $git = PublicInbox::Git->new($inboxdir);
32 my $im = PublicInbox::Import->new($git, 'test', $addr);
33 # ensure successful message delivery
34 {
35         my $mime = PublicInbox::MIME->new(<<EOF);
36 From: Me <me\@example.com>
37 To: You <you\@example.com>
38 Cc: $addr
39 Message-Id: <blah\@example.com>
40 Subject: hihi
41 Date: Fri, 02 Oct 1993 00:00:00 +0000
42
43 > quoted text
44 zzzzzz
45 EOF
46         $im->add($mime);
47         $im->done;
48         my $rev = $git->qx(qw(rev-list HEAD));
49         like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
50         @ls = $git->qx(qw(ls-tree -r --name-only HEAD));
51         chomp @ls;
52
53         # multipart with two text bodies
54         my %attr_text = (attributes => { content_type => 'text/plain' });
55         $mime = mime_load 't/plack-2-txt-bodies.eml', sub {
56         my $parts = [
57                 Email::MIME->create(%attr_text, body => 'hi'),
58                 Email::MIME->create(%attr_text, body => 'bye')
59         ];
60         Email::MIME->create(
61                 header_str => [
62                         From => 'a@example.com',
63                         Subject => 'blargh',
64                         'Message-ID' => '<multipart@example.com>',
65                         'In-Reply-To' => '<irp@example.com>'
66                 ],
67                 parts => $parts,
68         )}; # mime_load sub
69         $im->add($mime);
70
71         # multipart with attached patch + filename
72         $mime = mime_load 't/plack-attached-patch.eml', sub {
73         my $parts = [
74                 Email::MIME->create(%attr_text, body => 'hi, see attached'),
75                 Email::MIME->create(
76                         attributes => {
77                                         content_type => 'text/plain',
78                                         filename => "foo&.patch",
79                         },
80                         body => "--- a/file\n+++ b/file\n" .
81                                 "@@ -49, 7 +49,34 @@\n"
82                         )
83         ];
84         Email::MIME->create(
85                 header_str => [
86                         From => 'a@example.com',
87                         Subject => '[PATCH] asdf',
88                         'Message-ID' => '<patch@example.com>'
89                 ],
90                 parts => $parts
91         )}; # mime_load sub
92         $im->add($mime);
93
94         # multipart collapsed to single quoted-printable text/plain
95         $mime = mime_load 't/plack-qp.eml', sub {
96         my $parts = [
97                 Email::MIME->create(
98                         attributes => {
99                                 content_type => 'text/plain',
100                                 encoding => 'quoted-printable'
101                         },
102                         body => 'hi = bye',
103                 )
104         ];
105         Email::MIME->create(
106                 header_str => [
107                         From => 'qp@example.com',
108                         Subject => 'QP',
109                         'Message-ID' => '<qp@example.com>',
110                         ],
111                 parts => $parts,
112         )};
113         like($mime->body_raw, qr/hi =3D bye=/, 'our test used QP correctly');
114         $im->add($mime);
115
116         my $crlf = <<EOF;
117 From: Me
118   <me\@example.com>
119 To: $addr
120 Message-Id: <crlf\@example.com>
121 Subject: carriage
122   return
123   in
124   long
125   subject
126 Date: Fri, 02 Oct 1993 00:00:00 +0000
127
128 :(
129 EOF
130         $crlf =~ s/\n/\r\n/sg;
131         $im->add(PublicInbox::MIME->new($crlf));
132
133         $im->done;
134 }
135
136 test_psgi($app, sub {
137         my ($cb) = @_;
138         foreach my $u (qw(robots.txt favicon.ico .well-known/foo)) {
139                 my $res = $cb->(GET("http://example.com/$u"));
140                 is($res->code, 404, "$u is missing");
141         }
142 });
143
144 test_psgi($app, sub {
145         my ($cb) = @_;
146         my $res = $cb->(GET('http://example.com/test/crlf@example.com/'));
147         is($res->code, 200, 'retrieved CRLF as HTML');
148         unlike($res->content, qr/\r/, 'no CR in HTML');
149         $res = $cb->(GET('http://example.com/test/crlf@example.com/raw'));
150         is($res->code, 200, 'retrieved CRLF raw');
151         like($res->content, qr/\r/, 'CR preserved in raw message');
152 });
153
154 # redirect with newsgroup
155 test_psgi($app, sub {
156         my ($cb) = @_;
157         my $from = 'http://example.com/inbox.test';
158         my $to = 'http://example.com/test/';
159         my $res = $cb->(GET($from));
160         is($res->code, 301, 'newsgroup name is permanent redirect');
161         is($to, $res->header('Location'), 'redirect location matches');
162         $from .= '/';
163         is($res->code, 301, 'newsgroup name/ is permanent redirect');
164         is($to, $res->header('Location'), 'redirect location matches');
165 });
166
167 # redirect with trailing /
168 test_psgi($app, sub {
169         my ($cb) = @_;
170         my $from = 'http://example.com/test';
171         my $to = "$from/";
172         my $res = $cb->(GET($from));
173         is(301, $res->code, 'is permanent redirect');
174         is($to, $res->header('Location'),
175                 'redirect location matches with trailing slash');
176 });
177
178 foreach my $t (qw(t T)) {
179         test_psgi($app, sub {
180                 my ($cb) = @_;
181                 my $u = $pfx . "/blah\@example.com/$t";
182                 my $res = $cb->(GET($u));
183                 is(301, $res->code, "redirect for missing /");
184                 my $location = $res->header('Location');
185                 like($location, qr!/\Q$t\E/#u\z!,
186                         'redirected with missing /');
187         });
188 }
189 foreach my $t (qw(f)) {
190         test_psgi($app, sub {
191                 my ($cb) = @_;
192                 my $u = $pfx . "/blah\@example.com/$t";
193                 my $res = $cb->(GET($u));
194                 is(301, $res->code, "redirect for legacy /f");
195                 my $location = $res->header('Location');
196                 like($location, qr!/blah\@example\.com/\z!,
197                         'redirected with missing /');
198         });
199 }
200
201 test_psgi($app, sub {
202         my ($cb) = @_;
203         my $atomurl = 'http://example.com/test/new.atom';
204         my $res = $cb->(GET('http://example.com/test/new.html'));
205         is(200, $res->code, 'success response received');
206         like($res->content, qr!href="new\.atom"!,
207                 'atom URL generated');
208         like($res->content, qr!href="blah\@example\.com/"!,
209                 'index generated');
210         like($res->content, qr!1993-10-02!, 'date set');
211 });
212
213 test_psgi($app, sub {
214         my ($cb) = @_;
215         my $res = $cb->(GET($pfx . '/atom.xml'));
216         is(200, $res->code, 'success response received for atom');
217         my $body = $res->content;
218         like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
219                 'atom feed generated correct URL');
220         like($body, qr/<title>test for public-inbox/,
221                 "set title in XML feed");
222         like($body, qr/zzzzzz/, 'body included');
223         $res = $cb->(GET($pfx . '/description'));
224         like($res->content, qr/test for public-inbox/, 'got description');
225 });
226
227 test_psgi($app, sub {
228         my ($cb) = @_;
229         my $path = '/blah@example.com/';
230         my $res = $cb->(GET($pfx . $path));
231         is(200, $res->code, "success for $path");
232         my $html = $res->content;
233         like($html, qr!<title>hihi - Me</title>!, 'HTML returned');
234         like($html, qr!<a\nhref="raw"!s, 'raw link present');
235         like($html, qr!&gt; quoted text!s, 'quoted text inline');
236
237         $path .= 'f/';
238         $res = $cb->(GET($pfx . $path));
239         is(301, $res->code, "redirect for $path");
240         my $location = $res->header('Location');
241         like($location, qr!/blah\@example\.com/\z!,
242                 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
243
244         $res = $cb->(GET($pfx . '/multipart@example.com/'));
245         like($res->content,
246                 qr/hi\n.*-- Attachment #2.*\nbye\n/s, 'multipart split');
247
248         $res = $cb->(GET($pfx . '/patch@example.com/'));
249         $html = $res->content;
250         like($html, qr!see attached!, 'original body');
251         like($html, qr!.*Attachment #2: foo&(?:amp|#38);\.patch --!,
252                 'parts split with filename');
253
254         $res = $cb->(GET($pfx . '/qp@example.com/'));
255         like($res->content, qr/\bhi = bye\b/, "HTML output decoded QP");
256 });
257
258 test_psgi($app, sub {
259         my ($cb) = @_;
260         my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
261         is(200, $res->code, 'success response received for /*/raw');
262         like($res->content, qr!^From !sm, "mbox returned");
263 });
264
265 # legacy redirects
266 foreach my $t (qw(m f)) {
267         test_psgi($app, sub {
268                 my ($cb) = @_;
269                 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
270                 is(301, $res->code, "redirect for old $t .txt link");
271                 my $location = $res->header('Location');
272                 like($location, qr!/blah\@example\.com/raw\z!,
273                         ".txt redirected to /raw");
274         });
275 }
276
277 my %umap = (
278         'm' => '',
279         'f' => '',
280         't' => 't/',
281 );
282 while (my ($t, $e) = each %umap) {
283         test_psgi($app, sub {
284                 my ($cb) = @_;
285                 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
286                 is(301, $res->code, "redirect for old $t .html link");
287                 my $location = $res->header('Location');
288                 like($location,
289                         qr!/blah\@example\.com/$e(?:#u)?\z!,
290                         ".html redirected to new location");
291         });
292 }
293 foreach my $sfx (qw(mbox mbox.gz)) {
294         test_psgi($app, sub {
295                 my ($cb) = @_;
296                 my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
297                 is(301, $res->code, 'redirect for old thread link');
298                 my $location = $res->header('Location');
299                 like($location,
300                      qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
301                      "$sfx redirected to /mbox.gz");
302         });
303 }
304 test_psgi($app, sub {
305         my ($cb) = @_;
306         # for a while, we used to support /$INBOX/$X40/
307         # when we "compressed" long Message-IDs to SHA-1
308         # Now we're stuck supporting them forever :<
309         foreach my $path (@ls) {
310                 $path =~ tr!/!!d;
311                 my $from = "http://example.com/test/$path/";
312                 my $res = $cb->(GET($from));
313                 is(301, $res->code, 'is permanent redirect');
314                 like($res->header('Location'),
315                         qr!/test/blah\@example\.com/!,
316                         'redirect from x40 MIDs works');
317         }
318 });
319
320 # dumb HTTP clone/fetch support
321 test_psgi($app, sub {
322         my ($cb) = @_;
323         my $path = '/test/info/refs';
324         my $req = HTTP::Request->new('GET' => $path);
325         my $res = $cb->($req);
326         is(200, $res->code, 'refs readable');
327         my $orig = $res->content;
328
329         $req->header('Range', 'bytes=5-10');
330         $res = $cb->($req);
331         is(206, $res->code, 'got partial response');
332         is($res->content, substr($orig, 5, 6), 'partial body OK');
333
334         $req->header('Range', 'bytes=5-');
335         $res = $cb->($req);
336         is(206, $res->code, 'got partial another response');
337         is($res->content, substr($orig, 5), 'partial body OK past end');
338 });
339
340 # things which should fail
341 test_psgi($app, sub {
342         my ($cb) = @_;
343
344         my $res = $cb->(PUT('/'));
345         is(405, $res->code, 'no PUT to / allowed');
346         $res = $cb->(PUT('/test/'));
347         is(405, $res->code, 'no PUT /$INBOX allowed');
348
349         # TODO
350         # $res = $cb->(GET('/'));
351 });
352
353 done_testing();