]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
www: $MSGID/raw: set charset in HTTP response
[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         unlike($res->content, qr/\r/, 'no CR in HTML');
89         $res = $cb->(GET('http://example.com/test/crlf@example.com/raw'));
90         is($res->code, 200, 'retrieved CRLF raw');
91         like($res->content, qr/\r/, 'CR preserved in raw message');
92         $res = $cb->(GET('http://example.com/test/bogus@example.com/raw'));
93         is($res->code, 404, 'missing /raw is 404');
94 });
95
96 # redirect with newsgroup
97 test_psgi($app, sub {
98         my ($cb) = @_;
99         my $from = 'http://example.com/inbox.test';
100         my $to = 'http://example.com/test/';
101         my $res = $cb->(GET($from));
102         is($res->code, 301, 'newsgroup name is permanent redirect');
103         is($to, $res->header('Location'), 'redirect location matches');
104         $from .= '/';
105         is($res->code, 301, 'newsgroup name/ is permanent redirect');
106         is($to, $res->header('Location'), 'redirect location matches');
107 });
108
109 # redirect with trailing /
110 test_psgi($app, sub {
111         my ($cb) = @_;
112         my $from = 'http://example.com/test';
113         my $to = "$from/";
114         my $res = $cb->(GET($from));
115         is(301, $res->code, 'is permanent redirect');
116         is($to, $res->header('Location'),
117                 'redirect location matches with trailing slash');
118 });
119
120 foreach my $t (qw(t T)) {
121         test_psgi($app, sub {
122                 my ($cb) = @_;
123                 my $u = $pfx . "/blah\@example.com/$t";
124                 my $res = $cb->(GET($u));
125                 is(301, $res->code, "redirect for missing /");
126                 my $location = $res->header('Location');
127                 like($location, qr!/\Q$t\E/#u\z!,
128                         'redirected with missing /');
129         });
130 }
131 foreach my $t (qw(f)) {
132         test_psgi($app, sub {
133                 my ($cb) = @_;
134                 my $u = $pfx . "/blah\@example.com/$t";
135                 my $res = $cb->(GET($u));
136                 is(301, $res->code, "redirect for legacy /f");
137                 my $location = $res->header('Location');
138                 like($location, qr!/blah\@example\.com/\z!,
139                         'redirected with missing /');
140         });
141 }
142
143 test_psgi($app, sub {
144         my ($cb) = @_;
145         my $atomurl = 'http://example.com/test/new.atom';
146         my $res = $cb->(GET('http://example.com/test/new.html'));
147         is(200, $res->code, 'success response received');
148         like($res->content, qr!href="new\.atom"!,
149                 'atom URL generated');
150         like($res->content, qr!href="blah\@example\.com/"!,
151                 'index generated');
152         like($res->content, qr!1993-10-02!, 'date set');
153 });
154
155 test_psgi($app, sub {
156         my ($cb) = @_;
157         my $res = $cb->(GET($pfx . '/atom.xml'));
158         is(200, $res->code, 'success response received for atom');
159         my $body = $res->content;
160         like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
161                 'atom feed generated correct URL');
162         like($body, qr/<title>test for public-inbox/,
163                 "set title in XML feed");
164         like($body, qr/zzzzzz/, 'body included');
165         $res = $cb->(GET($pfx . '/description'));
166         like($res->content, qr/test for public-inbox/, 'got description');
167 });
168
169 test_psgi($app, sub {
170         my ($cb) = @_;
171         my $path = '/blah@example.com/';
172         my $res = $cb->(GET($pfx . $path));
173         is(200, $res->code, "success for $path");
174         my $html = $res->content;
175         like($html, qr!<title>hihi - Me</title>!, 'HTML returned');
176         like($html, qr!<a\nhref="raw"!s, 'raw link present');
177         like($html, qr!&gt; quoted text!s, 'quoted text inline');
178
179         $path .= 'f/';
180         $res = $cb->(GET($pfx . $path));
181         is(301, $res->code, "redirect for $path");
182         my $location = $res->header('Location');
183         like($location, qr!/blah\@example\.com/\z!,
184                 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
185
186         $res = $cb->(GET($pfx . '/multipart@example.com/'));
187         like($res->content,
188                 qr/hi\n.*-- Attachment #2.*\nbye\n/s, 'multipart split');
189
190         $res = $cb->(GET($pfx . '/patch@example.com/'));
191         $html = $res->content;
192         like($html, qr!see attached!, 'original body');
193         like($html, qr!.*Attachment #2: foo&(?:amp|#38);\.patch --!,
194                 'parts split with filename');
195
196         $res = $cb->(GET($pfx . '/qp@example.com/'));
197         like($res->content, qr/\bhi = bye\b/, "HTML output decoded QP");
198 });
199
200 test_psgi($app, sub {
201         my ($cb) = @_;
202         my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
203         is(200, $res->code, 'success response received for /*/raw');
204         like($res->content, qr!^From !sm, "mbox returned");
205         is($res->header('Content-Type'), 'text/plain; charset=iso-8859-1',
206                 'charset from message used');
207
208         $res = $cb->(GET($pfx . '/broken@example.com/raw'));
209         is($res->header('Content-Type'), 'text/plain; charset=UTF-8',
210                 'broken charset ignored');
211
212         $res = $cb->(GET($pfx . '/199707281508.AAA24167@hoyogw.example/raw'));
213         is($res->header('Content-Type'), 'text/plain; charset=ISO-2022-JP',
214                 'ISO-2002-JP returned');
215         chomp(my $body = $res->content);
216         my $raw = PublicInbox::Eml->new(\$body);
217         is($raw->body_raw, $eml->body_raw, 'ISO-2022-JP body unmodified');
218
219         $res = $cb->(GET($pfx . '/blah@example.com/t.mbox.gz'));
220         is(501, $res->code, '501 when overview missing');
221         like($res->content, qr!\bOverview\b!, 'overview omission noted');
222 });
223
224 # legacy redirects
225 foreach my $t (qw(m f)) {
226         test_psgi($app, sub {
227                 my ($cb) = @_;
228                 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
229                 is(301, $res->code, "redirect for old $t .txt link");
230                 my $location = $res->header('Location');
231                 like($location, qr!/blah\@example\.com/raw\z!,
232                         ".txt redirected to /raw");
233         });
234 }
235
236 my %umap = (
237         'm' => '',
238         'f' => '',
239         't' => 't/',
240 );
241 while (my ($t, $e) = each %umap) {
242         test_psgi($app, sub {
243                 my ($cb) = @_;
244                 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
245                 is(301, $res->code, "redirect for old $t .html link");
246                 my $location = $res->header('Location');
247                 like($location,
248                         qr!/blah\@example\.com/$e(?:#u)?\z!,
249                         ".html redirected to new location");
250         });
251 }
252 foreach my $sfx (qw(mbox mbox.gz)) {
253         test_psgi($app, sub {
254                 my ($cb) = @_;
255                 my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
256                 is(301, $res->code, 'redirect for old thread link');
257                 my $location = $res->header('Location');
258                 like($location,
259                      qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
260                      "$sfx redirected to /mbox.gz");
261         });
262 }
263 test_psgi($app, sub {
264         my ($cb) = @_;
265         # for a while, we used to support /$INBOX/$X40/
266         # when we "compressed" long Message-IDs to SHA-1
267         # Now we're stuck supporting them forever :<
268         foreach my $path ('f2912279bd7bcd8b7ab3033234942d58746d56f7') {
269                 my $from = "http://example.com/test/$path/";
270                 my $res = $cb->(GET($from));
271                 is(301, $res->code, 'is permanent redirect');
272                 like($res->header('Location'),
273                         qr!/test/blah\@example\.com/!,
274                         'redirect from x40 MIDs works');
275         }
276 });
277
278 # dumb HTTP clone/fetch support
279 test_psgi($app, sub {
280         my ($cb) = @_;
281         my $path = '/test/info/refs';
282         my $req = HTTP::Request->new('GET' => $path);
283         my $res = $cb->($req);
284         is(200, $res->code, 'refs readable');
285         my $orig = $res->content;
286
287         $req->header('Range', 'bytes=5-10');
288         $res = $cb->($req);
289         is(206, $res->code, 'got partial response');
290         is($res->content, substr($orig, 5, 6), 'partial body OK');
291
292         $req->header('Range', 'bytes=5-');
293         $res = $cb->($req);
294         is(206, $res->code, 'got partial another response');
295         is($res->content, substr($orig, 5), 'partial body OK past end');
296 });
297
298 # things which should fail
299 test_psgi($app, sub {
300         my ($cb) = @_;
301
302         my $res = $cb->(PUT('/'));
303         is(405, $res->code, 'no PUT to / allowed');
304         $res = $cb->(PUT('/test/'));
305         is(405, $res->code, 'no PUT /$INBOX allowed');
306
307         # TODO
308         # $res = $cb->(GET('/'));
309 });
310
311 done_testing();