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');
85 $res = $cb->(GET('http://example.com/test/bogus@example.com/raw'));
86 is($res->code, 404, 'missing /raw is 404');
89 # redirect with newsgroup
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');
98 is($res->code, 301, 'newsgroup name/ is permanent redirect');
99 is($to, $res->header('Location'), 'redirect location matches');
102 # redirect with trailing /
103 test_psgi($app, sub {
105 my $from = 'http://example.com/test';
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');
113 foreach my $t (qw(t T)) {
114 test_psgi($app, sub {
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 /');
124 foreach my $t (qw(f)) {
125 test_psgi($app, sub {
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 /');
136 test_psgi($app, sub {
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/"!,
145 like($res->content, qr!1993-10-02!, 'date set');
148 test_psgi($app, sub {
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');
162 test_psgi($app, sub {
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!> quoted text!s, 'quoted text inline');
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/');
179 $res = $cb->(GET($pfx . '/multipart@example.com/'));
181 qr/hi\n.*-- Attachment #2.*\nbye\n/s, 'multipart split');
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');
189 $res = $cb->(GET($pfx . '/qp@example.com/'));
190 like($res->content, qr/\bhi = bye\b/, "HTML output decoded QP");
193 test_psgi($app, sub {
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");
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');
205 foreach my $t (qw(m f)) {
206 test_psgi($app, sub {
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");
221 while (my ($t, $e) = each %umap) {
222 test_psgi($app, sub {
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');
228 qr!/blah\@example\.com/$e(?:#u)?\z!,
229 ".html redirected to new location");
232 foreach my $sfx (qw(mbox mbox.gz)) {
233 test_psgi($app, sub {
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');
239 qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
240 "$sfx redirected to /mbox.gz");
243 test_psgi($app, sub {
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');
258 # dumb HTTP clone/fetch support
259 test_psgi($app, sub {
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;
267 $req->header('Range', 'bytes=5-10');
269 is(206, $res->code, 'got partial response');
270 is($res->content, substr($orig, 5, 6), 'partial body OK');
272 $req->header('Range', 'bytes=5-');
274 is(206, $res->code, 'got partial another response');
275 is($res->content, substr($orig, 5), 'partial body OK past end');
278 # things which should fail
279 test_psgi($app, sub {
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');
288 # $res = $cb->(GET('/'));