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 # ensure successful message delivery
14 my $ibx = create_inbox('test', sub {
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>
21 Message-Id: <blah\@example.com>
23 Date: Fri, 02 Oct 1993 00:00:00 +0000
28 # multipart with two text bodies
29 $im->add(eml_load('t/plack-2-txt-bodies.eml')) or BAIL_OUT '->add';
31 # multipart with attached patch + filename
32 $im->add(eml_load('t/plack-attached-patch.eml')) or BAIL_OUT '->add';
34 # multipart collapsed to single quoted-printable text/plain
35 $im->add(eml_load('t/plack-qp.eml')) or BAIL_OUT '->add';
40 Message-Id: <crlf\@example.com>
46 Date: Fri, 02 Oct 1993 00:00:00 +0000
50 $crlf =~ s/\n/\r\n/sg;
51 $im->add(PublicInbox::Eml->new($crlf)) or BAIL_OUT '->add';
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;
59 inboxdir = $ibx->{inboxdir}
60 newsgroup = inbox.test
64 close $fh or BAIL_OUT "close: $!";
67 local $ENV{PI_CONFIG} = "$ibx->{inboxdir}/pi_config";
68 my $app = require $psgi;
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");
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');
87 # redirect with newsgroup
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');
96 is($res->code, 301, 'newsgroup name/ is permanent redirect');
97 is($to, $res->header('Location'), 'redirect location matches');
100 # redirect with trailing /
101 test_psgi($app, sub {
103 my $from = 'http://example.com/test';
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');
111 foreach my $t (qw(t T)) {
112 test_psgi($app, sub {
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 /');
122 foreach my $t (qw(f)) {
123 test_psgi($app, sub {
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 /');
134 test_psgi($app, sub {
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/"!,
143 like($res->content, qr!1993-10-02!, 'date set');
146 test_psgi($app, sub {
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');
160 test_psgi($app, sub {
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!> quoted text!s, 'quoted text inline');
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/');
177 $res = $cb->(GET($pfx . '/multipart@example.com/'));
179 qr/hi\n.*-- Attachment #2.*\nbye\n/s, 'multipart split');
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');
187 $res = $cb->(GET($pfx . '/qp@example.com/'));
188 like($res->content, qr/\bhi = bye\b/, "HTML output decoded QP");
191 test_psgi($app, sub {
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");
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');
203 foreach my $t (qw(m f)) {
204 test_psgi($app, sub {
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");
219 while (my ($t, $e) = each %umap) {
220 test_psgi($app, sub {
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');
226 qr!/blah\@example\.com/$e(?:#u)?\z!,
227 ".html redirected to new location");
230 foreach my $sfx (qw(mbox mbox.gz)) {
231 test_psgi($app, sub {
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');
237 qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
238 "$sfx redirected to /mbox.gz");
241 test_psgi($app, sub {
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');
256 # dumb HTTP clone/fetch support
257 test_psgi($app, sub {
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;
265 $req->header('Range', 'bytes=5-10');
267 is(206, $res->code, 'got partial response');
268 is($res->content, substr($orig, 5, 6), 'partial body OK');
270 $req->header('Range', 'bytes=5-');
272 is(206, $res->code, 'got partial another response');
273 is($res->content, substr($orig, 5), 'partial body OK past end');
276 # things which should fail
277 test_psgi($app, sub {
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');
286 # $res = $cb->(GET('/'));