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 unlike($res->content, qr/\r/, 'no CR in HTML');
89 $res = $cb->(GET('http://example.com/test/crlf@example.com/raw'));
90 is($res->code, 200, 'retrieved CRLF raw');
91 like($res->content, qr/\r/, 'CR preserved in raw message');
92 $res = $cb->(GET('http://example.com/test/bogus@example.com/raw'));
93 is($res->code, 404, 'missing /raw is 404');
96 # redirect with newsgroup
99 my $from = 'http://example.com/inbox.test';
100 my $to = 'http://example.com/test/';
101 my $res = $cb->(GET($from));
102 is($res->code, 301, 'newsgroup name is permanent redirect');
103 is($to, $res->header('Location'), 'redirect location matches');
105 is($res->code, 301, 'newsgroup name/ is permanent redirect');
106 is($to, $res->header('Location'), 'redirect location matches');
109 # redirect with trailing /
110 test_psgi($app, sub {
112 my $from = 'http://example.com/test';
114 my $res = $cb->(GET($from));
115 is(301, $res->code, 'is permanent redirect');
116 is($to, $res->header('Location'),
117 'redirect location matches with trailing slash');
120 foreach my $t (qw(t T)) {
121 test_psgi($app, sub {
123 my $u = $pfx . "/blah\@example.com/$t";
124 my $res = $cb->(GET($u));
125 is(301, $res->code, "redirect for missing /");
126 my $location = $res->header('Location');
127 like($location, qr!/\Q$t\E/#u\z!,
128 'redirected with missing /');
131 foreach my $t (qw(f)) {
132 test_psgi($app, sub {
134 my $u = $pfx . "/blah\@example.com/$t";
135 my $res = $cb->(GET($u));
136 is(301, $res->code, "redirect for legacy /f");
137 my $location = $res->header('Location');
138 like($location, qr!/blah\@example\.com/\z!,
139 'redirected with missing /');
143 test_psgi($app, sub {
145 my $atomurl = 'http://example.com/test/new.atom';
146 my $res = $cb->(GET('http://example.com/test/new.html'));
147 is(200, $res->code, 'success response received');
148 like($res->content, qr!href="new\.atom"!,
149 'atom URL generated');
150 like($res->content, qr!href="blah\@example\.com/"!,
152 like($res->content, qr!1993-10-02!, 'date set');
155 test_psgi($app, sub {
157 my $res = $cb->(GET($pfx . '/atom.xml'));
158 is(200, $res->code, 'success response received for atom');
159 my $body = $res->content;
160 like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
161 'atom feed generated correct URL');
162 like($body, qr/<title>test for public-inbox/,
163 "set title in XML feed");
164 like($body, qr/zzzzzz/, 'body included');
165 $res = $cb->(GET($pfx . '/description'));
166 like($res->content, qr/test for public-inbox/, 'got description');
169 test_psgi($app, sub {
171 my $path = '/blah@example.com/';
172 my $res = $cb->(GET($pfx . $path));
173 is(200, $res->code, "success for $path");
174 my $html = $res->content;
175 like($html, qr!<title>hihi - Me</title>!, 'HTML returned');
176 like($html, qr!<a\nhref="raw"!s, 'raw link present');
177 like($html, qr!> quoted text!s, 'quoted text inline');
180 $res = $cb->(GET($pfx . $path));
181 is(301, $res->code, "redirect for $path");
182 my $location = $res->header('Location');
183 like($location, qr!/blah\@example\.com/\z!,
184 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
186 $res = $cb->(GET($pfx . '/multipart@example.com/'));
188 qr/hi\n.*-- Attachment #2.*\nbye\n/s, 'multipart split');
190 $res = $cb->(GET($pfx . '/patch@example.com/'));
191 $html = $res->content;
192 like($html, qr!see attached!, 'original body');
193 like($html, qr!.*Attachment #2: foo&(?:amp|#38);\.patch --!,
194 'parts split with filename');
196 $res = $cb->(GET($pfx . '/qp@example.com/'));
197 like($res->content, qr/\bhi = bye\b/, "HTML output decoded QP");
200 test_psgi($app, sub {
202 my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
203 is(200, $res->code, 'success response received for /*/raw');
204 like($res->content, qr!^From !sm, "mbox returned");
205 is($res->header('Content-Type'), 'text/plain; charset=iso-8859-1',
206 'charset from message used');
208 $res = $cb->(GET($pfx . '/broken@example.com/raw'));
209 is($res->header('Content-Type'), 'text/plain; charset=UTF-8',
210 'broken charset ignored');
212 $res = $cb->(GET($pfx . '/199707281508.AAA24167@hoyogw.example/raw'));
213 is($res->header('Content-Type'), 'text/plain; charset=ISO-2022-JP',
214 'ISO-2002-JP returned');
215 chomp(my $body = $res->content);
216 my $raw = PublicInbox::Eml->new(\$body);
217 is($raw->body_raw, $eml->body_raw, 'ISO-2022-JP body unmodified');
219 $res = $cb->(GET($pfx . '/blah@example.com/t.mbox.gz'));
220 is(501, $res->code, '501 when overview missing');
221 like($res->content, qr!\bOverview\b!, 'overview omission noted');
225 foreach my $t (qw(m f)) {
226 test_psgi($app, sub {
228 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
229 is(301, $res->code, "redirect for old $t .txt link");
230 my $location = $res->header('Location');
231 like($location, qr!/blah\@example\.com/raw\z!,
232 ".txt redirected to /raw");
241 while (my ($t, $e) = each %umap) {
242 test_psgi($app, sub {
244 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
245 is(301, $res->code, "redirect for old $t .html link");
246 my $location = $res->header('Location');
248 qr!/blah\@example\.com/$e(?:#u)?\z!,
249 ".html redirected to new location");
252 foreach my $sfx (qw(mbox mbox.gz)) {
253 test_psgi($app, sub {
255 my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
256 is(301, $res->code, 'redirect for old thread link');
257 my $location = $res->header('Location');
259 qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
260 "$sfx redirected to /mbox.gz");
263 test_psgi($app, sub {
265 # for a while, we used to support /$INBOX/$X40/
266 # when we "compressed" long Message-IDs to SHA-1
267 # Now we're stuck supporting them forever :<
268 foreach my $path ('f2912279bd7bcd8b7ab3033234942d58746d56f7') {
269 my $from = "http://example.com/test/$path/";
270 my $res = $cb->(GET($from));
271 is(301, $res->code, 'is permanent redirect');
272 like($res->header('Location'),
273 qr!/test/blah\@example\.com/!,
274 'redirect from x40 MIDs works');
278 # dumb HTTP clone/fetch support
279 test_psgi($app, sub {
281 my $path = '/test/info/refs';
282 my $req = HTTP::Request->new('GET' => $path);
283 my $res = $cb->($req);
284 is(200, $res->code, 'refs readable');
285 my $orig = $res->content;
287 $req->header('Range', 'bytes=5-10');
289 is(206, $res->code, 'got partial response');
290 is($res->content, substr($orig, 5, 6), 'partial body OK');
292 $req->header('Range', 'bytes=5-');
294 is(206, $res->code, 'got partial another response');
295 is($res->content, substr($orig, 5), 'partial body OK past end');
298 # things which should fail
299 test_psgi($app, sub {
302 my $res = $cb->(PUT('/'));
303 is(405, $res->code, 'no PUT to / allowed');
304 $res = $cb->(PUT('/test/'));
305 is(405, $res->code, 'no PUT /$INBOX allowed');
308 # $res = $cb->(GET('/'));