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