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::Request Plack::Test
15 Mail::Thread URI::Escape);
16 foreach my $mod (@mods) {
18 plan skip_all => "$mod missing for plack.t" if $@;
20 use_ok 'PublicInbox::Import';
21 use_ok 'PublicInbox::Git';
23 foreach my $mod (@mods) { use_ok $mod; }
25 ok(-f $psgi, "psgi example file found");
26 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
27 open my $fh, '>', "$maindir/description" or die "open: $!\n";
28 print $fh "test for public-inbox\n";
29 close $fh or die "close: $!\n";
31 "$cfgpfx.address" => $addr,
32 "$cfgpfx.mainrepo" => $maindir,
33 "$cfgpfx.url" => 'http://example.com/test/',
34 "$cfgpfx.newsgroup" => 'inbox.test',
36 while (my ($k,$v) = each %cfg) {
37 is(0, system(qw(git config --file), $pi_config, $k, $v),
41 # ensure successful message delivery
43 my $mime = Email::MIME->new(<<EOF);
44 From: Me <me\@example.com>
45 To: You <you\@example.com>
47 Message-Id: <blah\@example.com>
49 Date: Thu, 01 Jan 1970 00:00:00 +0000
53 my $git = PublicInbox::Git->new($maindir);
54 my $im = PublicInbox::Import->new($git, 'test', $addr);
57 my $rev = `git --git-dir="$maindir" rev-list HEAD`;
58 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
61 local $ENV{PI_CONFIG} = $pi_config;
67 foreach my $u (qw(robots.txt favicon.ico .well-known/foo)) {
68 my $res = $cb->(GET("http://example.com/$u"));
69 is($res->code, 404, "$u is missing");
73 # redirect with newsgroup
76 my $from = 'http://example.com/inbox.test';
77 my $to = 'http://example.com/test/';
78 my $res = $cb->(GET($from));
79 is($res->code, 301, 'newsgroup name is permanent redirect');
80 is($to, $res->header('Location'), 'redirect location matches');
82 is($res->code, 301, 'newsgroup name/ is permanent redirect');
83 is($to, $res->header('Location'), 'redirect location matches');
86 # redirect with trailing /
89 my $from = 'http://example.com/test';
91 my $res = $cb->(GET($from));
92 is(301, $res->code, 'is permanent redirect');
93 is($to, $res->header('Location'), 'redirect location matches');
96 my $pfx = 'http://example.com/test';
97 foreach my $t (qw(t T)) {
100 my $u = $pfx . "/blah%40example.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%40example.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%40example\.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/'));
124 is(200, $res->code, 'success response received');
125 like($res->content, qr!href="\Q$atomurl\E"!,
126 'atom URL generated');
127 like($res->content, qr!href="blah%40example\.com/"!,
131 test_psgi($app, sub {
133 my $res = $cb->(GET($pfx . '/atom.xml'));
134 is(200, $res->code, 'success response received for atom');
136 qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
137 'atom feed generated correct URL');
140 test_psgi($app, sub {
142 my $path = '/blah%40example.com/';
143 my $res = $cb->(GET($pfx . $path));
144 is(200, $res->code, "success for $path");
145 like($res->content, qr!<title>hihi - Me</title>!,
149 $res = $cb->(GET($pfx . $path));
150 is(301, $res->code, "redirect for $path");
151 my $location = $res->header('Location');
152 like($location, qr!/blah%40example\.com/\z!,
153 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
156 test_psgi($app, sub {
158 my $res = $cb->(GET($pfx . '/blah%40example.com/raw'));
159 is(200, $res->code, 'success response received for /*/raw');
160 like($res->content, qr!^From !sm, "mbox returned");
164 foreach my $t (qw(m f)) {
165 test_psgi($app, sub {
167 my $res = $cb->(GET($pfx . "/$t/blah%40example.com.txt"));
168 is(301, $res->code, "redirect for old $t .txt link");
169 my $location = $res->header('Location');
170 like($location, qr!/blah%40example\.com/raw\z!,
171 ".txt redirected to /raw");
180 while (my ($t, $e) = each %umap) {
181 test_psgi($app, sub {
183 my $res = $cb->(GET($pfx . "/$t/blah%40example.com.html"));
184 is(301, $res->code, "redirect for old $t .html link");
185 my $location = $res->header('Location');
187 qr!/blah%40example\.com/$e(?:#u)?\z!,
188 ".html redirected to new location");
191 foreach my $sfx (qw(mbox mbox.gz)) {
192 test_psgi($app, sub {
194 my $res = $cb->(GET($pfx . "/t/blah%40example.com.$sfx"));
195 is(301, $res->code, 'redirect for old thread link');
196 my $location = $res->header('Location');
198 qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
199 "$sfx redirected to /mbox.gz");