]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
update copyright headers and email addresses
[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(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
24 our $have_plack;
25 eval {
26         require Plack::Request;
27         eval 'use Plack::Test; use HTTP::Request::Common';
28         $have_plack = 1;
29 };
30 SKIP: {
31         skip 'Plack not installed', 1 unless $have_plack;
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         test_psgi($app, sub {
85                 my ($cb) = @_;
86                 my $atomurl = 'http://example.com/test/new.atom';
87                 my $res = $cb->(GET('http://example.com/test/'));
88                 is(200, $res->code, 'success response received');
89                 like($res->content, qr!href="\Q$atomurl\E"!,
90                         'atom URL generated');
91                 like($res->content, qr!href="blah%40example\.com/"!,
92                         'index generated');
93         });
94
95         my $pfx = 'http://example.com/test';
96         test_psgi($app, sub {
97                 my ($cb) = @_;
98                 my $res = $cb->(GET($pfx . '/atom.xml'));
99                 is(200, $res->code, 'success response received for atom');
100                 like($res->content,
101                         qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
102                         'atom feed generated correct URL');
103         });
104
105         foreach my $t (('', 'f/')) {
106                 test_psgi($app, sub {
107                         my ($cb) = @_;
108                         my $path = "/blah%40example.com/$t";
109                         my $res = $cb->(GET($pfx . $path));
110                         is(200, $res->code, "success for $path");
111                         like($res->content, qr!<title>hihi - Me</title>!,
112                                 "HTML returned");
113                 });
114         }
115         test_psgi($app, sub {
116                 my ($cb) = @_;
117                 my $res = $cb->(GET($pfx . '/blah%40example.com/raw'));
118                 is(200, $res->code, 'success response received for /*/raw');
119                 like($res->content, qr!\AFrom !, "mbox returned");
120         });
121
122         # legacy redirects
123         foreach my $t (qw(m f)) {
124                 test_psgi($app, sub {
125                         my ($cb) = @_;
126                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.txt"));
127                         is(301, $res->code, "redirect for old $t .txt link");
128                         my $location = $res->header('Location');
129                         like($location, qr!/blah%40example\.com/raw\z!,
130                                 ".txt redirected to /raw");
131                 });
132         }
133
134         my %umap = (
135                 'm' => '',
136                 'f' => 'f/',
137                 't' => 't/',
138         );
139         while (my ($t, $e) = each %umap) {
140                 test_psgi($app, sub {
141                         my ($cb) = @_;
142                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.html"));
143                         is(301, $res->code, "redirect for old $t .html link");
144                         my $location = $res->header('Location');
145                         like($location,
146                                 qr!/blah%40example\.com/$e(?:#u)?\z!,
147                                 ".html redirected to new location");
148                 });
149         }
150         foreach my $sfx (qw(mbox mbox.gz)) {
151                 test_psgi($app, sub {
152                         my ($cb) = @_;
153                         my $res = $cb->(GET($pfx . "/t/blah%40example.com.$sfx"));
154                         is(301, $res->code, 'redirect for old thread link');
155                         my $location = $res->header('Location');
156                         like($location,
157                              qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
158                              "$sfx redirected to /mbox.gz");
159                 });
160         }
161 }
162
163 done_testing();
164
165 sub run_with_env {
166         my ($env, @args) = @_;
167         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
168         run(@args, init => $init);
169 }