]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
www: redirect /$MESSAGE_ID/f/ endpoints
[public-inbox.git] / t / plack.t
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)
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use File::Temp qw/tempdir/;
8 use Cwd;
9 use IPC::Run qw/run/;
10 my $psgi = "examples/public-inbox.psgi";
11 my $mda = "blib/script/public-inbox-mda";
12 my $tmpdir = tempdir('pi-plack-XXXXXX', TMPDIR => 1, CLEANUP => 1);
13 my $home = "$tmpdir/pi-home";
14 my $pi_home = "$home/.public-inbox";
15 my $pi_config = "$pi_home/config";
16 my $maindir = "$tmpdir/main.git";
17 my $main_bin = getcwd()."/t/main-bin";
18 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
19 my $addr = 'test-public@example.com';
20 my $cfgpfx = "publicinbox.test";
21 my $failbox = "$home/fail.mbox";
22 local $ENV{PI_EMERGENCY} = $failbox;
23 my @mods = qw(HTTP::Request::Common Plack::Request Plack::Test
24         Mail::Thread URI::Escape);
25 foreach my $mod (@mods) {
26         eval "require $mod";
27         plan skip_all => "$mod missing for plack.t" if $@;
28 }
29
30 foreach my $mod (@mods) { use_ok $mod; }
31 {
32         ok(-f $psgi, "psgi example file found");
33         ok(-x "$main_bin/spamc",
34                 "spamc ham mock found (run in top of source tree");
35         ok(-x $mda, "$mda is executable");
36         is(1, mkdir($home, 0755), "setup ~/ for testing");
37         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
38         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
39         open my $fh, '>', "$maindir/description" or die "open: $!\n";
40         print $fh "test for public-inbox\n";
41         close $fh or die "close: $!\n";
42         my %cfg = (
43                 "$cfgpfx.address" => $addr,
44                 "$cfgpfx.mainrepo" => $maindir,
45         );
46         while (my ($k,$v) = each %cfg) {
47                 is(0, system(qw(git config --file), $pi_config, $k, $v),
48                         "setup $k");
49         }
50
51         local $ENV{HOME} = $home;
52         local $ENV{ORIGINAL_RECIPIENT} = $addr;
53
54         # ensure successful message delivery
55         {
56                 my $simple = Email::Simple->new(<<EOF);
57 From: Me <me\@example.com>
58 To: You <you\@example.com>
59 Cc: $addr
60 Message-Id: <blah\@example.com>
61 Subject: hihi
62 Date: Thu, 01 Jan 1970 00:00:00 +0000
63
64 zzzzzz
65 EOF
66                 my $in = $simple->as_string;
67                 run_with_env({PATH => $main_path}, [$mda], \$in);
68                 local $ENV{GIT_DIR} = $maindir;
69                 my $rev = `git rev-list HEAD`;
70                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
71         }
72         my $app = require $psgi;
73
74         # redirect with trailing /
75         test_psgi($app, sub {
76                 my ($cb) = @_;
77                 my $from = 'http://example.com/test';
78                 my $to = "$from/";
79                 my $res = $cb->(GET($from));
80                 is(301, $res->code, 'is permanent redirect');
81                 is($to, $res->header('Location'), 'redirect location matches');
82         });
83
84         my $pfx = 'http://example.com/test';
85         foreach my $t (qw(t T)) {
86                 test_psgi($app, sub {
87                         my ($cb) = @_;
88                         my $u = $pfx . "/blah%40example.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 /');
94                 });
95         }
96         foreach my $t (qw(f)) {
97                 test_psgi($app, sub {
98                         my ($cb) = @_;
99                         my $u = $pfx . "/blah%40example.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%40example\.com/\z!,
104                                 'redirected with missing /');
105                 });
106         }
107
108         test_psgi($app, sub {
109                 my ($cb) = @_;
110                 my $atomurl = 'http://example.com/test/new.atom';
111                 my $res = $cb->(GET('http://example.com/test/'));
112                 is(200, $res->code, 'success response received');
113                 like($res->content, qr!href="\Q$atomurl\E"!,
114                         'atom URL generated');
115                 like($res->content, qr!href="blah%40example\.com/"!,
116                         'index generated');
117         });
118
119         test_psgi($app, sub {
120                 my ($cb) = @_;
121                 my $res = $cb->(GET($pfx . '/atom.xml'));
122                 is(200, $res->code, 'success response received for atom');
123                 like($res->content,
124                         qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
125                         'atom feed generated correct URL');
126         });
127
128         test_psgi($app, sub {
129                 my ($cb) = @_;
130                 my $path = '/blah%40example.com/';
131                 my $res = $cb->(GET($pfx . $path));
132                 is(200, $res->code, "success for $path");
133                 like($res->content, qr!<title>hihi - Me</title>!,
134                         "HTML returned");
135
136                 $path .= 'f/';
137                 $res = $cb->(GET($pfx . $path));
138                 is(301, $res->code, "redirect for $path");
139                 my $location = $res->header('Location');
140                 like($location, qr!/blah%40example\.com/\z!,
141                         '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
142         });
143
144         test_psgi($app, sub {
145                 my ($cb) = @_;
146                 my $res = $cb->(GET($pfx . '/blah%40example.com/raw'));
147                 is(200, $res->code, 'success response received for /*/raw');
148                 like($res->content, qr!^From !sm, "mbox returned");
149         });
150
151         # legacy redirects
152         foreach my $t (qw(m f)) {
153                 test_psgi($app, sub {
154                         my ($cb) = @_;
155                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.txt"));
156                         is(301, $res->code, "redirect for old $t .txt link");
157                         my $location = $res->header('Location');
158                         like($location, qr!/blah%40example\.com/raw\z!,
159                                 ".txt redirected to /raw");
160                 });
161         }
162
163         my %umap = (
164                 'm' => '',
165                 'f' => '',
166                 't' => 't/',
167         );
168         while (my ($t, $e) = each %umap) {
169                 test_psgi($app, sub {
170                         my ($cb) = @_;
171                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.html"));
172                         is(301, $res->code, "redirect for old $t .html link");
173                         my $location = $res->header('Location');
174                         like($location,
175                                 qr!/blah%40example\.com/$e(?:#u)?\z!,
176                                 ".html redirected to new location");
177                 });
178         }
179         foreach my $sfx (qw(mbox mbox.gz)) {
180                 test_psgi($app, sub {
181                         my ($cb) = @_;
182                         my $res = $cb->(GET($pfx . "/t/blah%40example.com.$sfx"));
183                         is(301, $res->code, 'redirect for old thread link');
184                         my $location = $res->header('Location');
185                         like($location,
186                              qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
187                              "$sfx redirected to /mbox.gz");
188                 });
189         }
190 }
191
192 done_testing();
193
194 sub run_with_env {
195         my ($env, @args) = @_;
196         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
197         run(@args, init => $init);
198 }