1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
8 my $psgi = "./examples/public-inbox.psgi";
9 my ($tmpdir, $for_destroy) = tmpdir();
10 my $pi_config = "$tmpdir/config";
11 my $inboxdir = "$tmpdir/main.git";
12 my $addr = 'test-public@example.com';
13 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
15 use_ok 'PublicInbox::Import';
16 use_ok 'PublicInbox::Git';
19 foreach my $mod (@mods) { use_ok $mod; }
20 local $ENV{PI_CONFIG} = $pi_config;
21 ok(-f $psgi, "psgi example file found");
22 my $pfx = 'http://example.com/test';
23 ok(run_script(['-init', 'test', $inboxdir, "$pfx/", $addr]),
25 PublicInbox::Import::run_die([qw(git config -f), $pi_config,
26 'publicinbox.test.newsgroup', 'inbox.test']);
27 open my $fh, '>', "$inboxdir/description" or die "open: $!\n";
28 print $fh "test for public-inbox\n";
29 close $fh or die "close: $!\n";
30 my $app = require $psgi;
31 my $git = PublicInbox::Git->new($inboxdir);
32 my $im = PublicInbox::Import->new($git, 'test', $addr);
33 # ensure successful message delivery
35 my $mime = Email::MIME->new(<<EOF);
36 From: Me <me\@example.com>
37 To: You <you\@example.com>
39 Message-Id: <blah\@example.com>
41 Date: Fri, 02 Oct 1993 00:00:00 +0000
48 my $rev = $git->qx(qw(rev-list HEAD));
49 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
50 @ls = $git->qx(qw(ls-tree -r --name-only HEAD));
53 # multipart with two text bodies
54 my %attr_text = (attributes => { content_type => 'text/plain' });
56 Email::MIME->create(%attr_text, body => 'hi'),
57 Email::MIME->create(%attr_text, body => 'bye')
59 $mime = Email::MIME->create(
61 From => 'a@example.com',
63 'Message-ID' => '<multipart@example.com>',
64 'In-Reply-To' => '<irp@example.com>'
70 # multipart with attached patch + filename
71 $parts = [ Email::MIME->create(%attr_text, body => 'hi, see attached'),
74 content_type => 'text/plain',
75 filename => "foo&.patch",
77 body => "--- a/file\n+++ b/file\n" .
78 "@@ -49, 7 +49,34 @@\n"
81 $mime = Email::MIME->create(
83 From => 'a@example.com',
84 Subject => '[PATCH] asdf',
85 'Message-ID' => '<patch@example.com>'
91 # multipart collapsed to single quoted-printable text/plain
95 content_type => 'text/plain',
96 encoding => 'quoted-printable'
101 $mime = Email::MIME->create(
103 From => 'qp@example.com',
105 'Message-ID' => '<qp@example.com>',
109 like($mime->body_raw, qr/hi =3D bye=/, 'our test used QP correctly');
115 test_psgi($app, sub {
117 foreach my $u (qw(robots.txt favicon.ico .well-known/foo)) {
118 my $res = $cb->(GET("http://example.com/$u"));
119 is($res->code, 404, "$u is missing");
123 # redirect with newsgroup
124 test_psgi($app, sub {
126 my $from = 'http://example.com/inbox.test';
127 my $to = 'http://example.com/test/';
128 my $res = $cb->(GET($from));
129 is($res->code, 301, 'newsgroup name is permanent redirect');
130 is($to, $res->header('Location'), 'redirect location matches');
132 is($res->code, 301, 'newsgroup name/ is permanent redirect');
133 is($to, $res->header('Location'), 'redirect location matches');
136 # redirect with trailing /
137 test_psgi($app, sub {
139 my $from = 'http://example.com/test';
141 my $res = $cb->(GET($from));
142 is(301, $res->code, 'is permanent redirect');
143 is($to, $res->header('Location'),
144 'redirect location matches with trailing slash');
147 foreach my $t (qw(t T)) {
148 test_psgi($app, sub {
150 my $u = $pfx . "/blah\@example.com/$t";
151 my $res = $cb->(GET($u));
152 is(301, $res->code, "redirect for missing /");
153 my $location = $res->header('Location');
154 like($location, qr!/\Q$t\E/#u\z!,
155 'redirected with missing /');
158 foreach my $t (qw(f)) {
159 test_psgi($app, sub {
161 my $u = $pfx . "/blah\@example.com/$t";
162 my $res = $cb->(GET($u));
163 is(301, $res->code, "redirect for legacy /f");
164 my $location = $res->header('Location');
165 like($location, qr!/blah\@example\.com/\z!,
166 'redirected with missing /');
170 test_psgi($app, sub {
172 my $atomurl = 'http://example.com/test/new.atom';
173 my $res = $cb->(GET('http://example.com/test/new.html'));
174 is(200, $res->code, 'success response received');
175 like($res->content, qr!href="new\.atom"!,
176 'atom URL generated');
177 like($res->content, qr!href="blah\@example\.com/"!,
179 like($res->content, qr!1993-10-02!, 'date set');
182 test_psgi($app, sub {
184 my $res = $cb->(GET($pfx . '/atom.xml'));
185 is(200, $res->code, 'success response received for atom');
186 my $body = $res->content;
187 like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
188 'atom feed generated correct URL');
189 like($body, qr/<title>test for public-inbox/,
190 "set title in XML feed");
191 like($body, qr/zzzzzz/, 'body included');
192 $res = $cb->(GET($pfx . '/description'));
193 like($res->content, qr/test for public-inbox/, 'got description');
196 test_psgi($app, sub {
198 my $path = '/blah@example.com/';
199 my $res = $cb->(GET($pfx . $path));
200 is(200, $res->code, "success for $path");
201 my $html = $res->content;
202 like($html, qr!<title>hihi - Me</title>!, 'HTML returned');
203 like($html, qr!<a\nhref="raw"!s, 'raw link present');
204 like($html, qr!> quoted text!s, 'quoted text inline');
207 $res = $cb->(GET($pfx . $path));
208 is(301, $res->code, "redirect for $path");
209 my $location = $res->header('Location');
210 like($location, qr!/blah\@example\.com/\z!,
211 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
213 $res = $cb->(GET($pfx . '/multipart@example.com/'));
215 qr/hi\n.*-- Attachment #2.*\nbye\n/s, 'multipart split');
217 $res = $cb->(GET($pfx . '/patch@example.com/'));
218 $html = $res->content;
219 like($html, qr!see attached!, 'original body');
220 like($html, qr!.*Attachment #2: foo&(?:amp|#38);\.patch --!,
221 'parts split with filename');
223 $res = $cb->(GET($pfx . '/qp@example.com/'));
224 like($res->content, qr/\bhi = bye\b/, "HTML output decoded QP");
227 test_psgi($app, sub {
229 my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
230 is(200, $res->code, 'success response received for /*/raw');
231 like($res->content, qr!^From !sm, "mbox returned");
235 foreach my $t (qw(m f)) {
236 test_psgi($app, sub {
238 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
239 is(301, $res->code, "redirect for old $t .txt link");
240 my $location = $res->header('Location');
241 like($location, qr!/blah\@example\.com/raw\z!,
242 ".txt redirected to /raw");
251 while (my ($t, $e) = each %umap) {
252 test_psgi($app, sub {
254 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
255 is(301, $res->code, "redirect for old $t .html link");
256 my $location = $res->header('Location');
258 qr!/blah\@example\.com/$e(?:#u)?\z!,
259 ".html redirected to new location");
262 foreach my $sfx (qw(mbox mbox.gz)) {
263 test_psgi($app, sub {
265 my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
266 is(301, $res->code, 'redirect for old thread link');
267 my $location = $res->header('Location');
269 qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
270 "$sfx redirected to /mbox.gz");
273 test_psgi($app, sub {
275 # for a while, we used to support /$INBOX/$X40/
276 # when we "compressed" long Message-IDs to SHA-1
277 # Now we're stuck supporting them forever :<
278 foreach my $path (@ls) {
280 my $from = "http://example.com/test/$path/";
281 my $res = $cb->(GET($from));
282 is(301, $res->code, 'is permanent redirect');
283 like($res->header('Location'),
284 qr!/test/blah\@example\.com/!,
285 'redirect from x40 MIDs works');
289 # dumb HTTP clone/fetch support
290 test_psgi($app, sub {
292 my $path = '/test/info/refs';
293 my $req = HTTP::Request->new('GET' => $path);
294 my $res = $cb->($req);
295 is(200, $res->code, 'refs readable');
296 my $orig = $res->content;
298 $req->header('Range', 'bytes=5-10');
300 is(206, $res->code, 'got partial response');
301 is($res->content, substr($orig, 5, 6), 'partial body OK');
303 $req->header('Range', 'bytes=5-');
305 is(206, $res->code, 'got partial another response');
306 is($res->content, substr($orig, 5), 'partial body OK past end');
309 # things which should fail
310 test_psgi($app, sub {
313 my $res = $cb->(PUT('/'));
314 is(405, $res->code, 'no PUT to / allowed');
315 $res = $cb->(PUT('/test/'));
316 is(405, $res->code, 'no PUT /$INBOX allowed');
319 # $res = $cb->(GET('/'));