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;
65 # redirect with newsgroup
68 my $from = 'http://example.com/inbox.test';
69 my $to = 'http://example.com/test/';
70 my $res = $cb->(GET($from));
71 is($res->code, 301, 'is permanent redirect');
72 is($to, $res->header('Location'), 'redirect location matches');
74 is($res->code, 301, 'is permanent redirect');
75 is($to, $res->header('Location'), 'redirect location matches');
78 # redirect with trailing /
81 my $from = 'http://example.com/test';
83 my $res = $cb->(GET($from));
84 is(301, $res->code, 'is permanent redirect');
85 is($to, $res->header('Location'), 'redirect location matches');
88 my $pfx = 'http://example.com/test';
89 foreach my $t (qw(t T)) {
92 my $u = $pfx . "/blah%40example.com/$t";
93 my $res = $cb->(GET($u));
94 is(301, $res->code, "redirect for missing /");
95 my $location = $res->header('Location');
96 like($location, qr!/\Q$t\E/#u\z!,
97 'redirected with missing /');
100 foreach my $t (qw(f)) {
101 test_psgi($app, sub {
103 my $u = $pfx . "/blah%40example.com/$t";
104 my $res = $cb->(GET($u));
105 is(301, $res->code, "redirect for legacy /f");
106 my $location = $res->header('Location');
107 like($location, qr!/blah%40example\.com/\z!,
108 'redirected with missing /');
112 test_psgi($app, sub {
114 my $atomurl = 'http://example.com/test/new.atom';
115 my $res = $cb->(GET('http://example.com/test/'));
116 is(200, $res->code, 'success response received');
117 like($res->content, qr!href="\Q$atomurl\E"!,
118 'atom URL generated');
119 like($res->content, qr!href="blah%40example\.com/"!,
123 test_psgi($app, sub {
125 my $res = $cb->(GET($pfx . '/atom.xml'));
126 is(200, $res->code, 'success response received for atom');
128 qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
129 'atom feed generated correct URL');
132 test_psgi($app, sub {
134 my $path = '/blah%40example.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%40example\.com/\z!,
145 '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
148 test_psgi($app, sub {
150 my $res = $cb->(GET($pfx . '/blah%40example.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%40example.com.txt"));
160 is(301, $res->code, "redirect for old $t .txt link");
161 my $location = $res->header('Location');
162 like($location, qr!/blah%40example\.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%40example.com.html"));
176 is(301, $res->code, "redirect for old $t .html link");
177 my $location = $res->header('Location');
179 qr!/blah%40example\.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%40example.com.$sfx"));
187 is(301, $res->code, 'redirect for old thread link');
188 my $location = $res->header('Location');
190 qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
191 "$sfx redirected to /mbox.gz");