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