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 $maindir = "$tmpdir/main.git";
12 my $addr = 'test-public@example.com';
13 my $cfgpfx = "publicinbox.test";
14 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
16 use_ok 'PublicInbox::Import';
17 use_ok 'PublicInbox::Git';
20 foreach my $mod (@mods) { use_ok $mod; }
22 ok(-f $psgi, "psgi example file found");
23 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
24 open my $fh, '>', "$maindir/description" or die "open: $!\n";
25 print $fh "test for public-inbox\n";
26 close $fh or die "close: $!\n";
28 "$cfgpfx.address" => $addr,
29 "$cfgpfx.inboxdir" => $maindir,
30 "$cfgpfx.url" => 'http://example.com/test/',
31 "$cfgpfx.newsgroup" => 'inbox.test',
33 while (my ($k,$v) = each %cfg) {
34 is(0, system(qw(git config --file), $pi_config, $k, $v),
38 # ensure successful message delivery
40 my $mime = Email::MIME->new(<<EOF);
41 From: Me <me\@example.com>
42 To: You <you\@example.com>
44 Message-Id: <blah\@example.com>
46 Date: Fri, 02 Oct 1993 00:00:00 +0000
50 my $git = PublicInbox::Git->new($maindir);
51 my $im = PublicInbox::Import->new($git, 'test', $addr);
54 my $rev = $git->qx(qw(rev-list HEAD));
55 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
56 @ls = $git->qx(qw(ls-tree -r --name-only HEAD));
60 local $ENV{PI_CONFIG} = $pi_config;
66 foreach my $u (qw(robots.txt favicon.ico .well-known/foo)) {
67 my $res = $cb->(GET("http://example.com/$u"));
68 is($res->code, 404, "$u is missing");
72 # redirect with newsgroup
75 my $from = 'http://example.com/inbox.test';
76 my $to = 'http://example.com/test/';
77 my $res = $cb->(GET($from));
78 is($res->code, 301, 'newsgroup name is permanent redirect');
79 is($to, $res->header('Location'), 'redirect location matches');
81 is($res->code, 301, 'newsgroup name/ is permanent redirect');
82 is($to, $res->header('Location'), 'redirect location matches');
85 # redirect with trailing /
88 my $from = 'http://example.com/test';
90 my $res = $cb->(GET($from));
91 is(301, $res->code, 'is permanent redirect');
92 is($to, $res->header('Location'),
93 'redirect location matches with trailing slash');
96 my $pfx = 'http://example.com/test';
97 foreach my $t (qw(t T)) {
100 my $u = $pfx . "/blah\@example.com/$t";
101 my $res = $cb->(GET($u));
102 is(301, $res->code, "redirect for missing /");
103 my $location = $res->header('Location');
104 like($location, qr!/\Q$t\E/#u\z!,
105 'redirected with missing /');
108 foreach my $t (qw(f)) {
109 test_psgi($app, sub {
111 my $u = $pfx . "/blah\@example.com/$t";
112 my $res = $cb->(GET($u));
113 is(301, $res->code, "redirect for legacy /f");
114 my $location = $res->header('Location');
115 like($location, qr!/blah\@example\.com/\z!,
116 'redirected with missing /');
120 test_psgi($app, sub {
122 my $atomurl = 'http://example.com/test/new.atom';
123 my $res = $cb->(GET('http://example.com/test/new.html'));
124 is(200, $res->code, 'success response received');
125 like($res->content, qr!href="new\.atom"!,
126 'atom URL generated');
127 like($res->content, qr!href="blah\@example\.com/"!,
129 like($res->content, qr!1993-10-02!, 'date set');
132 test_psgi($app, sub {
134 my $res = $cb->(GET($pfx . '/atom.xml'));
135 is(200, $res->code, 'success response received for atom');
136 my $body = $res->content;
137 like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
138 'atom feed generated correct URL');
139 like($body, qr/<title>test for public-inbox/,
140 "set title in XML feed");
141 like($body, qr/zzzzzz/, 'body included');
144 test_psgi($app, sub {
146 my $path = '/blah@example.com/';
147 my $res = $cb->(GET($pfx . $path));
148 is(200, $res->code, "success for $path");
149 like($res->content, qr!<title>hihi - Me</title>!,
153 $res = $cb->(GET($pfx . $path));
154 is(301, $res->code, "redirect for $path");
155 my $location = $res->header('Location');
156 like($location, qr!/blah\@example\.com/\z!,
157 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
160 test_psgi($app, sub {
162 my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
163 is(200, $res->code, 'success response received for /*/raw');
164 like($res->content, qr!^From !sm, "mbox returned");
168 foreach my $t (qw(m f)) {
169 test_psgi($app, sub {
171 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
172 is(301, $res->code, "redirect for old $t .txt link");
173 my $location = $res->header('Location');
174 like($location, qr!/blah\@example\.com/raw\z!,
175 ".txt redirected to /raw");
184 while (my ($t, $e) = each %umap) {
185 test_psgi($app, sub {
187 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
188 is(301, $res->code, "redirect for old $t .html link");
189 my $location = $res->header('Location');
191 qr!/blah\@example\.com/$e(?:#u)?\z!,
192 ".html redirected to new location");
195 foreach my $sfx (qw(mbox mbox.gz)) {
196 test_psgi($app, sub {
198 my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
199 is(301, $res->code, 'redirect for old thread link');
200 my $location = $res->header('Location');
202 qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
203 "$sfx redirected to /mbox.gz");
206 test_psgi($app, sub {
208 # for a while, we used to support /$INBOX/$X40/
209 # when we "compressed" long Message-IDs to SHA-1
210 # Now we're stuck supporting them forever :<
211 foreach my $path (@ls) {
213 my $from = "http://example.com/test/$path/";
214 my $res = $cb->(GET($from));
215 is(301, $res->code, 'is permanent redirect');
216 like($res->header('Location'),
217 qr!/test/blah\@example\.com/!,
218 'redirect from x40 MIDs works');
222 # dumb HTTP clone/fetch support
223 test_psgi($app, sub {
225 my $path = '/test/info/refs';
226 my $req = HTTP::Request->new('GET' => $path);
227 my $res = $cb->($req);
228 is(200, $res->code, 'refs readable');
229 my $orig = $res->content;
231 $req->header('Range', 'bytes=5-10');
233 is(206, $res->code, 'got partial response');
234 is($res->content, substr($orig, 5, 6), 'partial body OK');
236 $req->header('Range', 'bytes=5-');
238 is(206, $res->code, 'got partial another response');
239 is($res->content, substr($orig, 5), 'partial body OK past end');
242 # things which should fail
243 test_psgi($app, sub {
246 my $res = $cb->(PUT('/'));
247 is(405, $res->code, 'no PUT to / allowed');
248 $res = $cb->(PUT('/test/'));
249 is(405, $res->code, 'no PUT /$INBOX allowed');
252 # $res = $cb->(GET('/'));