1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
7 use File::Temp qw/tempdir/;
8 my $psgi = "examples/public-inbox.psgi";
9 my $tmpdir = tempdir('pi-plack-XXXXXX', TMPDIR => 1, CLEANUP => 1);
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);
15 foreach my $mod (@mods) {
17 plan skip_all => "$mod missing for plack.t" if $@;
19 use_ok 'PublicInbox::Import';
20 use_ok 'PublicInbox::Git';
22 foreach my $mod (@mods) { use_ok $mod; }
24 ok(-f $psgi, "psgi example file found");
25 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
26 open my $fh, '>', "$maindir/description" or die "open: $!\n";
27 print $fh "test for public-inbox\n";
28 close $fh or die "close: $!\n";
30 "$cfgpfx.address" => $addr,
31 "$cfgpfx.mainrepo" => $maindir,
32 "$cfgpfx.url" => 'http://example.com/test/',
33 "$cfgpfx.newsgroup" => 'inbox.test',
35 while (my ($k,$v) = each %cfg) {
36 is(0, system(qw(git config --file), $pi_config, $k, $v),
40 # ensure successful message delivery
42 my $mime = Email::MIME->new(<<EOF);
43 From: Me <me\@example.com>
44 To: You <you\@example.com>
46 Message-Id: <blah\@example.com>
48 Date: Thu, 01 Jan 1970 00:00:00 +0000
52 my $git = PublicInbox::Git->new($maindir);
53 my $im = PublicInbox::Import->new($git, 'test', $addr);
56 my $rev = `git --git-dir="$maindir" rev-list HEAD`;
57 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
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'), 'redirect location matches');
95 my $pfx = 'http://example.com/test';
96 foreach my $t (qw(t T)) {
99 my $u = $pfx . "/blah\@example.com/$t";
100 my $res = $cb->(GET($u));
101 is(301, $res->code, "redirect for missing /");
102 my $location = $res->header('Location');
103 like($location, qr!/\Q$t\E/#u\z!,
104 'redirected with missing /');
107 foreach my $t (qw(f)) {
108 test_psgi($app, sub {
110 my $u = $pfx . "/blah\@example.com/$t";
111 my $res = $cb->(GET($u));
112 is(301, $res->code, "redirect for legacy /f");
113 my $location = $res->header('Location');
114 like($location, qr!/blah\@example\.com/\z!,
115 'redirected with missing /');
119 test_psgi($app, sub {
121 my $atomurl = 'http://example.com/test/new.atom';
122 my $res = $cb->(GET('http://example.com/test/new.html'));
123 is(200, $res->code, 'success response received');
124 like($res->content, qr!href="new\.atom"!,
125 'atom URL generated');
126 like($res->content, qr!href="blah\@example\.com/"!,
130 test_psgi($app, sub {
132 my $res = $cb->(GET($pfx . '/atom.xml'));
133 is(200, $res->code, 'success response received for atom');
135 qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
136 'atom feed generated correct URL');
139 test_psgi($app, sub {
141 my $path = '/blah@example.com/';
142 my $res = $cb->(GET($pfx . $path));
143 is(200, $res->code, "success for $path");
144 like($res->content, qr!<title>hihi - Me</title>!,
148 $res = $cb->(GET($pfx . $path));
149 is(301, $res->code, "redirect for $path");
150 my $location = $res->header('Location');
151 like($location, qr!/blah\@example\.com/\z!,
152 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
155 test_psgi($app, sub {
157 my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
158 is(200, $res->code, 'success response received for /*/raw');
159 like($res->content, qr!^From !sm, "mbox returned");
163 foreach my $t (qw(m f)) {
164 test_psgi($app, sub {
166 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
167 is(301, $res->code, "redirect for old $t .txt link");
168 my $location = $res->header('Location');
169 like($location, qr!/blah\@example\.com/raw\z!,
170 ".txt redirected to /raw");
179 while (my ($t, $e) = each %umap) {
180 test_psgi($app, sub {
182 my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
183 is(301, $res->code, "redirect for old $t .html link");
184 my $location = $res->header('Location');
186 qr!/blah\@example\.com/$e(?:#u)?\z!,
187 ".html redirected to new location");
190 foreach my $sfx (qw(mbox mbox.gz)) {
191 test_psgi($app, sub {
193 my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
194 is(301, $res->code, 'redirect for old thread link');
195 my $location = $res->header('Location');
197 qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
198 "$sfx redirected to /mbox.gz");