]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / plack.t
1 #!perl -w
2 # Copyright (C) 2014-2021 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 my $psgi = "./examples/public-inbox.psgi";
8 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
9 require_mods(@mods);
10 foreach my $mod (@mods) { use_ok $mod; }
11 ok(-f $psgi, "psgi example file found");
12 my $pfx = 'http://example.com/test';
13 my $eml = eml_load('t/iso-2202-jp.eml');
14 # ensure successful message deliveries
15 my $ibx = create_inbox('test-1', sub {
16         my ($im, $ibx) = @_;
17         my $addr = $ibx->{-primary_address};
18         $im->add($eml) or xbail '->add';
19         $eml->header_set('Content-Type',
20                 "text/plain; charset=\rso\rb\0gus\rithurts");
21         $eml->header_set('Message-ID', '<broken@example.com>');
22         $im->add($eml) or xbail '->add';
23         $im->add(PublicInbox::Eml->new(<<EOF)) or xbail '->add';
24 From: Me <me\@example.com>
25 To: You <you\@example.com>
26 Cc: $addr
27 Message-Id: <blah\@example.com>
28 Subject: hihi
29 Date: Fri, 02 Oct 1993 00:00:00 +0000
30 Content-Type: text/plain; charset=iso-8859-1
31
32 > quoted text
33 zzzzzz
34 EOF
35         # multipart with two text bodies
36         $im->add(eml_load('t/plack-2-txt-bodies.eml')) or BAIL_OUT '->add';
37
38         # multipart with attached patch + filename
39         $im->add(eml_load('t/plack-attached-patch.eml')) or BAIL_OUT '->add';
40
41         # multipart collapsed to single quoted-printable text/plain
42         $im->add(eml_load('t/plack-qp.eml')) or BAIL_OUT '->add';
43         my $crlf = <<EOF;
44 From: Me
45   <me\@example.com>
46 To: $addr
47 Message-Id: <crlf\@example.com>
48 Subject: carriage
49   return
50   in
51   long
52   subject
53 Date: Fri, 02 Oct 1993 00:00:00 +0000
54
55 :(
56 EOF
57         $crlf =~ s/\n/\r\n/sg;
58         $im->add(PublicInbox::Eml->new($crlf)) or BAIL_OUT '->add';
59
60         open my $fh, '>', "$ibx->{inboxdir}/description" or BAIL_OUT "open: $!";
61         print $fh "test for public-inbox\n" or BAIL_OUT;
62         close $fh or BAIL_OUT "close: $!";
63         open $fh, '>', "$ibx->{inboxdir}/pi_config";
64         print $fh <<EOF or BAIL_OUT;
65 [publicinbox "test"]
66         inboxdir = $ibx->{inboxdir}
67         newsgroup = inbox.test
68         address = $addr
69         url = $pfx/
70 EOF
71         close $fh or BAIL_OUT "close: $!";
72 });
73
74 local $ENV{PI_CONFIG} = "$ibx->{inboxdir}/pi_config";
75 my $app = require $psgi;
76 test_psgi($app, sub {
77         my ($cb) = @_;
78         foreach my $u (qw(robots.txt favicon.ico .well-known/foo)) {
79                 my $res = $cb->(GET("http://example.com/$u"));
80                 is($res->code, 404, "$u is missing");
81         }
82 });
83
84 test_psgi($app, sub {
85         my ($cb) = @_;
86         my $res = $cb->(GET('http://example.com/test/crlf@example.com/'));
87         is($res->code, 200, 'retrieved CRLF as HTML');
88         like($res->content, qr/mailto:me\@example/, 'no %40, per RFC 6068');
89         unlike($res->content, qr/\r/, 'no CR in HTML');
90         $res = $cb->(GET('http://example.com/test/crlf@example.com/raw'));
91         is($res->code, 200, 'retrieved CRLF raw');
92         like($res->content, qr/\r/, 'CR preserved in raw message');
93         $res = $cb->(GET('http://example.com/test/bogus@example.com/raw'));
94         is($res->code, 404, 'missing /raw is 404');
95 });
96
97 # redirect with newsgroup
98 test_psgi($app, sub {
99         my ($cb) = @_;
100         my $from = 'http://example.com/inbox.test';
101         my $to = 'http://example.com/test/';
102         my $res = $cb->(GET($from));
103         is($res->code, 301, 'newsgroup name is permanent redirect');
104         is($to, $res->header('Location'), 'redirect location matches');
105         $from .= '/';
106         is($res->code, 301, 'newsgroup name/ is permanent redirect');
107         is($to, $res->header('Location'), 'redirect location matches');
108 });
109
110 # redirect with trailing /
111 test_psgi($app, sub {
112         my ($cb) = @_;
113         my $from = 'http://example.com/test';
114         my $to = "$from/";
115         my $res = $cb->(GET($from));
116         is(301, $res->code, 'is permanent redirect');
117         is($to, $res->header('Location'),
118                 'redirect location matches with trailing slash');
119 });
120
121 foreach my $t (qw(t T)) {
122         test_psgi($app, sub {
123                 my ($cb) = @_;
124                 my $u = $pfx . "/blah\@example.com/$t";
125                 my $res = $cb->(GET($u));
126                 is(301, $res->code, "redirect for missing /");
127                 my $location = $res->header('Location');
128                 like($location, qr!/\Q$t\E/#u\z!,
129                         'redirected with missing /');
130         });
131 }
132 foreach my $t (qw(f)) {
133         test_psgi($app, sub {
134                 my ($cb) = @_;
135                 my $u = $pfx . "/blah\@example.com/$t";
136                 my $res = $cb->(GET($u));
137                 is(301, $res->code, "redirect for legacy /f");
138                 my $location = $res->header('Location');
139                 like($location, qr!/blah\@example\.com/\z!,
140                         'redirected with missing /');
141         });
142 }
143
144 test_psgi($app, sub {
145         my ($cb) = @_;
146         my $atomurl = 'http://example.com/test/new.atom';
147         my $res = $cb->(GET('http://example.com/test/new.html'));
148         is(200, $res->code, 'success response received');
149         like($res->content, qr!href="new\.atom"!,
150                 'atom URL generated');
151         like($res->content, qr!href="blah\@example\.com/"!,
152                 'index generated');
153         like($res->content, qr!1993-10-02!, 'date set');
154 });
155
156 test_psgi($app, sub {
157         my ($cb) = @_;
158         my $res = $cb->(GET($pfx . '/atom.xml'));
159         is(200, $res->code, 'success response received for atom');
160         my $body = $res->content;
161         like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
162                 'atom feed generated correct URL');
163         like($body, qr/<title>test for public-inbox/,
164                 "set title in XML feed");
165         like($body, qr/zzzzzz/, 'body included');
166         $res = $cb->(GET($pfx . '/description'));
167         like($res->content, qr/test for public-inbox/, 'got description');
168 });
169
170 test_psgi($app, sub {
171         my ($cb) = @_;
172         my $path = '/blah@example.com/';
173         my $res = $cb->(GET($pfx . $path));
174         is(200, $res->code, "success for $path");
175         my $html = $res->content;
176         like($html, qr!<title>hihi - Me</title>!, 'HTML returned');
177         like($html, qr!<a\nhref="raw"!s, 'raw link present');
178         like($html, qr!&gt; quoted text!s, 'quoted text inline');
179
180         $path .= 'f/';
181         $res = $cb->(GET($pfx . $path));
182         is(301, $res->code, "redirect for $path");
183         my $location = $res->header('Location');
184         like($location, qr!/blah\@example\.com/\z!,
185                 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
186
187         $res = $cb->(GET($pfx . '/multipart@example.com/'));
188         like($res->content,
189                 qr/hi\n.*-- Attachment #2.*\nbye\n/s, 'multipart split');
190
191         $res = $cb->(GET($pfx . '/patch@example.com/'));
192         $html = $res->content;
193         like($html, qr!see attached!, 'original body');
194         like($html, qr!.*Attachment #2: foo&(?:amp|#38);\.patch --!,
195                 'parts split with filename');
196
197         $res = $cb->(GET($pfx . '/qp@example.com/'));
198         like($res->content, qr/\bhi = bye\b/, "HTML output decoded QP");
199 });
200
201 test_psgi($app, sub {
202         my ($cb) = @_;
203         my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
204         is(200, $res->code, 'success response received for /*/raw');
205         like($res->content, qr!^From !sm, "mbox returned");
206         is($res->header('Content-Type'), 'text/plain; charset=iso-8859-1',
207                 'charset from message used');
208
209         $res = $cb->(GET($pfx . '/broken@example.com/raw'));
210         is($res->header('Content-Type'), 'text/plain; charset=UTF-8',
211                 'broken charset ignored');
212
213         $res = $cb->(GET($pfx . '/199707281508.AAA24167@hoyogw.example/raw'));
214         is($res->header('Content-Type'), 'text/plain; charset=ISO-2022-JP',
215                 'ISO-2002-JP returned');
216         chomp(my $body = $res->content);
217         my $raw = PublicInbox::Eml->new(\$body);
218         is($raw->body_raw, $eml->body_raw, 'ISO-2022-JP body unmodified');
219
220         $res = $cb->(GET($pfx . '/blah@example.com/t.mbox.gz'));
221         is(501, $res->code, '501 when overview missing');
222         like($res->content, qr!\bOverview\b!, 'overview omission noted');
223 });
224
225 # legacy redirects
226 foreach my $t (qw(m f)) {
227         test_psgi($app, sub {
228                 my ($cb) = @_;
229                 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
230                 is(301, $res->code, "redirect for old $t .txt link");
231                 my $location = $res->header('Location');
232                 like($location, qr!/blah\@example\.com/raw\z!,
233                         ".txt redirected to /raw");
234         });
235 }
236
237 my %umap = (
238         'm' => '',
239         'f' => '',
240         't' => 't/',
241 );
242 while (my ($t, $e) = each %umap) {
243         test_psgi($app, sub {
244                 my ($cb) = @_;
245                 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
246                 is(301, $res->code, "redirect for old $t .html link");
247                 my $location = $res->header('Location');
248                 like($location,
249                         qr!/blah\@example\.com/$e(?:#u)?\z!,
250                         ".html redirected to new location");
251         });
252 }
253 foreach my $sfx (qw(mbox mbox.gz)) {
254         test_psgi($app, sub {
255                 my ($cb) = @_;
256                 my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
257                 is(301, $res->code, 'redirect for old thread link');
258                 my $location = $res->header('Location');
259                 like($location,
260                      qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
261                      "$sfx redirected to /mbox.gz");
262         });
263 }
264 test_psgi($app, sub {
265         my ($cb) = @_;
266         # for a while, we used to support /$INBOX/$X40/
267         # when we "compressed" long Message-IDs to SHA-1
268         # Now we're stuck supporting them forever :<
269         foreach my $path ('f2912279bd7bcd8b7ab3033234942d58746d56f7') {
270                 my $from = "http://example.com/test/$path/";
271                 my $res = $cb->(GET($from));
272                 is(301, $res->code, 'is permanent redirect');
273                 like($res->header('Location'),
274                         qr!/test/blah\@example\.com/!,
275                         'redirect from x40 MIDs works');
276         }
277 });
278
279 # dumb HTTP clone/fetch support
280 test_psgi($app, sub {
281         my ($cb) = @_;
282         my $path = '/test/info/refs';
283         my $req = HTTP::Request->new('GET' => $path);
284         my $res = $cb->($req);
285         is(200, $res->code, 'refs readable');
286         my $orig = $res->content;
287
288         $req->header('Range', 'bytes=5-10');
289         $res = $cb->($req);
290         is(206, $res->code, 'got partial response');
291         is($res->content, substr($orig, 5, 6), 'partial body OK');
292
293         $req->header('Range', 'bytes=5-');
294         $res = $cb->($req);
295         is(206, $res->code, 'got partial another response');
296         is($res->content, substr($orig, 5), 'partial body OK past end');
297 });
298
299 # things which should fail
300 test_psgi($app, sub {
301         my ($cb) = @_;
302
303         my $res = $cb->(PUT('/'));
304         is(405, $res->code, 'no PUT to / allowed');
305         $res = $cb->(PUT('/test/'));
306         is(405, $res->code, 'no PUT /$INBOX allowed');
307
308         # TODO
309         # $res = $cb->(GET('/'));
310 });
311
312 done_testing();