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