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