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>
6 use PublicInbox::TestCommon;
7 my $psgi = "./examples/public-inbox.psgi";
8 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
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 {
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>
27 Message-Id: <blah\@example.com>
29 Date: Fri, 02 Oct 1993 00:00:00 +0000
30 Content-Type: text/plain; charset=iso-8859-1
35 # multipart with two text bodies
36 $im->add(eml_load('t/plack-2-txt-bodies.eml')) or BAIL_OUT '->add';
38 # multipart with attached patch + filename
39 $im->add(eml_load('t/plack-attached-patch.eml')) or BAIL_OUT '->add';
41 # multipart collapsed to single quoted-printable text/plain
42 $im->add(eml_load('t/plack-qp.eml')) or BAIL_OUT '->add';
47 Message-Id: <crlf\@example.com>
53 Date: Fri, 02 Oct 1993 00:00:00 +0000
57 $crlf =~ s/\n/\r\n/sg;
58 $im->add(PublicInbox::Eml->new($crlf)) or BAIL_OUT '->add';
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;
66 inboxdir = $ibx->{inboxdir}
67 newsgroup = inbox.test
71 close $fh or BAIL_OUT "close: $!";
74 local $ENV{PI_CONFIG} = "$ibx->{inboxdir}/pi_config";
75 my $app = require $psgi;
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");
86 my $res = $cb->(GET('http://example.com/test/crlf@example.com/'));
87 is($res->code, 200, 'retrieved CRLF as HTML');
88 like($res->content, qr/mailto:me\@example/, 'no %40, per RFC 6068');
89 unlike($res->content, qr/\r/, 'no CR in HTML');
90 $res = $cb->(GET('http://example.com/test/crlf@example.com/raw'));
91 is($res->code, 200, 'retrieved CRLF raw');
92 like($res->content, qr/\r/, 'CR preserved in raw message');
93 $res = $cb->(GET('http://example.com/test/bogus@example.com/raw'));
94 is($res->code, 404, 'missing /raw is 404');
97 # redirect with newsgroup
100 my $from = 'http://example.com/inbox.test';
101 my $to = 'http://example.com/test/';
102 my $res = $cb->(GET($from));
103 is($res->code, 301, 'newsgroup name is permanent redirect');
104 is($to, $res->header('Location'), 'redirect location matches');
106 is($res->code, 301, 'newsgroup name/ is permanent redirect');
107 is($to, $res->header('Location'), 'redirect location matches');
110 # redirect with trailing /
111 test_psgi($app, sub {
113 my $from = 'http://example.com/test';
115 my $res = $cb->(GET($from));
116 is(301, $res->code, 'is permanent redirect');
117 is($to, $res->header('Location'),
118 'redirect location matches with trailing slash');
121 foreach my $t (qw(t T)) {
122 test_psgi($app, sub {
124 my $u = $pfx . "/blah\@example.com/$t";
125 my $res = $cb->(GET($u));
126 is(301, $res->code, "redirect for missing /");
127 my $location = $res->header('Location');
128 like($location, qr!/\Q$t\E/#u\z!,
129 'redirected with missing /');
132 foreach my $t (qw(f)) {
133 test_psgi($app, sub {
135 my $u = $pfx . "/blah\@example.com/$t";
136 my $res = $cb->(GET($u));
137 is(301, $res->code, "redirect for legacy /f");
138 my $location = $res->header('Location');
139 like($location, qr!/blah\@example\.com/\z!,
140 'redirected with missing /');
144 test_psgi($app, sub {
146 my $atomurl = 'http://example.com/test/new.atom';
147 my $res = $cb->(GET('http://example.com/test/new.html'));
148 is(200, $res->code, 'success response received');
149 like($res->content, qr!href="new\.atom"!,
150 'atom URL generated');
151 like($res->content, qr!href="blah\@example\.com/"!,
153 like($res->content, qr!1993-10-02!, 'date set');
156 test_psgi($app, sub {
158 my $res = $cb->(GET($pfx . '/atom.xml'));
159 is(200, $res->code, 'success response received for atom');
160 my $body = $res->content;
161 like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
162 'atom feed generated correct URL');
163 like($body, qr/<title>test for public-inbox/,
164 "set title in XML feed");
165 like($body, qr/zzzzzz/, 'body included');
166 $res = $cb->(GET($pfx . '/description'));
167 like($res->content, qr/test for public-inbox/, 'got description');
170 test_psgi($app, sub {
172 my $path = '/blah@example.com/';
173 my $res = $cb->(GET($pfx . $path));
174 is(200, $res->code, "success for $path");
175 my $html = $res->content;
176 like($html, qr!<title>hihi - Me</title>!, 'HTML returned');
177 like($html, qr!<a\nhref="raw"!s, 'raw link present');
178 like($html, qr!> quoted text!s, 'quoted text inline');
181 $res = $cb->(GET($pfx . $path));
182 is(301, $res->code, "redirect for $path");
183 my $location = $res->header('Location');
184 like($location, qr!/blah\@example\.com/\z!,
185 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
187 $res = $cb->(GET($pfx . '/multipart@example.com/'));
189 qr/hi\n.*-- Attachment #2.*\nbye\n/s, 'multipart split');
191 $res = $cb->(GET($pfx . '/patch@example.com/'));
192 $html = $res->content;
193 like($html, qr!see attached!, 'original body');
194 like($html, qr!.*Attachment #2: foo&(?:amp|#38);\.patch --!,
195 'parts split with filename');
197 $res = $cb->(GET($pfx . '/qp@example.com/'));
198 like($res->content, qr/\bhi = bye\b/, "HTML output decoded QP");
201 test_psgi($app, sub {
203 my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
204 is(200, $res->code, 'success response received for /*/raw');
205 like($res->content, qr!^From !sm, "mbox returned");
206 is($res->header('Content-Type'), 'text/plain; charset=iso-8859-1',
207 'charset from message used');
209 $res = $cb->(GET($pfx . '/broken@example.com/raw'));
210 is($res->header('Content-Type'), 'text/plain; charset=UTF-8',
211 'broken charset ignored');
213 $res = $cb->(GET($pfx . '/199707281508.AAA24167@hoyogw.example/raw'));
214 is($res->header('Content-Type'), 'text/plain; charset=ISO-2022-JP',
215 'ISO-2002-JP returned');
216 chomp(my $body = $res->content);
217 my $raw = PublicInbox::Eml->new(\$body);
218 is($raw->body_raw, $eml->body_raw, 'ISO-2022-JP body unmodified');
220 $res = $cb->(GET($pfx . '/blah@example.com/t.mbox.gz'));
221 is(501, $res->code, '501 when overview missing');
222 like($res->content, qr!\bOverview\b!, 'overview omission noted');
226 foreach my $t (qw(m f)) {
227 test_psgi($app, sub {
229 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
230 is(301, $res->code, "redirect for old $t .txt link");
231 my $location = $res->header('Location');
232 like($location, qr!/blah\@example\.com/raw\z!,
233 ".txt redirected to /raw");
242 while (my ($t, $e) = each %umap) {
243 test_psgi($app, sub {
245 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
246 is(301, $res->code, "redirect for old $t .html link");
247 my $location = $res->header('Location');
249 qr!/blah\@example\.com/$e(?:#u)?\z!,
250 ".html redirected to new location");
253 foreach my $sfx (qw(mbox mbox.gz)) {
254 test_psgi($app, sub {
256 my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
257 is(301, $res->code, 'redirect for old thread link');
258 my $location = $res->header('Location');
260 qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
261 "$sfx redirected to /mbox.gz");
264 test_psgi($app, sub {
266 # for a while, we used to support /$INBOX/$X40/
267 # when we "compressed" long Message-IDs to SHA-1
268 # Now we're stuck supporting them forever :<
269 foreach my $path ('f2912279bd7bcd8b7ab3033234942d58746d56f7') {
270 my $from = "http://example.com/test/$path/";
271 my $res = $cb->(GET($from));
272 is(301, $res->code, 'is permanent redirect');
273 like($res->header('Location'),
274 qr!/test/blah\@example\.com/!,
275 'redirect from x40 MIDs works');
279 # dumb HTTP clone/fetch support
280 test_psgi($app, sub {
282 my $path = '/test/info/refs';
283 my $req = HTTP::Request->new('GET' => $path);
284 my $res = $cb->($req);
285 is(200, $res->code, 'refs readable');
286 my $orig = $res->content;
288 $req->header('Range', 'bytes=5-10');
290 is(206, $res->code, 'got partial response');
291 is($res->content, substr($orig, 5, 6), 'partial body OK');
293 $req->header('Range', 'bytes=5-');
295 is(206, $res->code, 'got partial another response');
296 is($res->content, substr($orig, 5), 'partial body OK past end');
299 # things which should fail
300 test_psgi($app, sub {
303 my $res = $cb->(PUT('/'));
304 is(405, $res->code, 'no PUT to / allowed');
305 $res = $cb->(PUT('/test/'));
306 is(405, $res->code, 'no PUT /$INBOX allowed');
309 # $res = $cb->(GET('/'));