1 # Copyright (C) 2014-2019 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
47 my $rev = $git->qx(qw(rev-list HEAD));
48 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
49 @ls = $git->qx(qw(ls-tree -r --name-only HEAD));
55 foreach my $u (qw(robots.txt favicon.ico .well-known/foo)) {
56 my $res = $cb->(GET("http://example.com/$u"));
57 is($res->code, 404, "$u is missing");
61 # redirect with newsgroup
64 my $from = 'http://example.com/inbox.test';
65 my $to = 'http://example.com/test/';
66 my $res = $cb->(GET($from));
67 is($res->code, 301, 'newsgroup name is permanent redirect');
68 is($to, $res->header('Location'), 'redirect location matches');
70 is($res->code, 301, 'newsgroup name/ is permanent redirect');
71 is($to, $res->header('Location'), 'redirect location matches');
74 # redirect with trailing /
77 my $from = 'http://example.com/test';
79 my $res = $cb->(GET($from));
80 is(301, $res->code, 'is permanent redirect');
81 is($to, $res->header('Location'),
82 'redirect location matches with trailing slash');
85 foreach my $t (qw(t T)) {
88 my $u = $pfx . "/blah\@example.com/$t";
89 my $res = $cb->(GET($u));
90 is(301, $res->code, "redirect for missing /");
91 my $location = $res->header('Location');
92 like($location, qr!/\Q$t\E/#u\z!,
93 'redirected with missing /');
96 foreach my $t (qw(f)) {
99 my $u = $pfx . "/blah\@example.com/$t";
100 my $res = $cb->(GET($u));
101 is(301, $res->code, "redirect for legacy /f");
102 my $location = $res->header('Location');
103 like($location, qr!/blah\@example\.com/\z!,
104 'redirected with missing /');
108 test_psgi($app, sub {
110 my $atomurl = 'http://example.com/test/new.atom';
111 my $res = $cb->(GET('http://example.com/test/new.html'));
112 is(200, $res->code, 'success response received');
113 like($res->content, qr!href="new\.atom"!,
114 'atom URL generated');
115 like($res->content, qr!href="blah\@example\.com/"!,
117 like($res->content, qr!1993-10-02!, 'date set');
120 test_psgi($app, sub {
122 my $res = $cb->(GET($pfx . '/atom.xml'));
123 is(200, $res->code, 'success response received for atom');
124 my $body = $res->content;
125 like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
126 'atom feed generated correct URL');
127 like($body, qr/<title>test for public-inbox/,
128 "set title in XML feed");
129 like($body, qr/zzzzzz/, 'body included');
132 test_psgi($app, sub {
134 my $path = '/blah@example.com/';
135 my $res = $cb->(GET($pfx . $path));
136 is(200, $res->code, "success for $path");
137 like($res->content, qr!<title>hihi - Me</title>!,
141 $res = $cb->(GET($pfx . $path));
142 is(301, $res->code, "redirect for $path");
143 my $location = $res->header('Location');
144 like($location, qr!/blah\@example\.com/\z!,
145 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
148 test_psgi($app, sub {
150 my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
151 is(200, $res->code, 'success response received for /*/raw');
152 like($res->content, qr!^From !sm, "mbox returned");
156 foreach my $t (qw(m f)) {
157 test_psgi($app, sub {
159 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
160 is(301, $res->code, "redirect for old $t .txt link");
161 my $location = $res->header('Location');
162 like($location, qr!/blah\@example\.com/raw\z!,
163 ".txt redirected to /raw");
172 while (my ($t, $e) = each %umap) {
173 test_psgi($app, sub {
175 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
176 is(301, $res->code, "redirect for old $t .html link");
177 my $location = $res->header('Location');
179 qr!/blah\@example\.com/$e(?:#u)?\z!,
180 ".html redirected to new location");
183 foreach my $sfx (qw(mbox mbox.gz)) {
184 test_psgi($app, sub {
186 my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
187 is(301, $res->code, 'redirect for old thread link');
188 my $location = $res->header('Location');
190 qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
191 "$sfx redirected to /mbox.gz");
194 test_psgi($app, sub {
196 # for a while, we used to support /$INBOX/$X40/
197 # when we "compressed" long Message-IDs to SHA-1
198 # Now we're stuck supporting them forever :<
199 foreach my $path (@ls) {
201 my $from = "http://example.com/test/$path/";
202 my $res = $cb->(GET($from));
203 is(301, $res->code, 'is permanent redirect');
204 like($res->header('Location'),
205 qr!/test/blah\@example\.com/!,
206 'redirect from x40 MIDs works');
210 # dumb HTTP clone/fetch support
211 test_psgi($app, sub {
213 my $path = '/test/info/refs';
214 my $req = HTTP::Request->new('GET' => $path);
215 my $res = $cb->($req);
216 is(200, $res->code, 'refs readable');
217 my $orig = $res->content;
219 $req->header('Range', 'bytes=5-10');
221 is(206, $res->code, 'got partial response');
222 is($res->content, substr($orig, 5, 6), 'partial body OK');
224 $req->header('Range', 'bytes=5-');
226 is(206, $res->code, 'got partial another response');
227 is($res->content, substr($orig, 5), 'partial body OK past end');
230 # things which should fail
231 test_psgi($app, sub {
234 my $res = $cb->(PUT('/'));
235 is(405, $res->code, 'no PUT to / allowed');
236 $res = $cb->(PUT('/test/'));
237 is(405, $res->code, 'no PUT /$INBOX allowed');
240 # $res = $cb->(GET('/'));