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