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/;
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 my @mods = qw(HTTP::Request::Common Plack::Request Plack::Test
24 Mail::Thread URI::Escape);
25 foreach my $mod (@mods) {
27 plan skip_all => "$mod missing for plack.t" if $@;
30 foreach my $mod (@mods) { use_ok $mod; }
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";
43 "$cfgpfx.address" => $addr,
44 "$cfgpfx.mainrepo" => $maindir,
46 while (my ($k,$v) = each %cfg) {
47 is(0, system(qw(git config --file), $pi_config, $k, $v),
51 local $ENV{HOME} = $home;
52 local $ENV{ORIGINAL_RECIPIENT} = $addr;
54 # ensure successful message delivery
56 my $simple = Email::Simple->new(<<EOF);
57 From: Me <me\@example.com>
58 To: You <you\@example.com>
60 Message-Id: <blah\@example.com>
62 Date: Thu, 01 Jan 1970 00:00:00 +0000
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");
72 my $app = require $psgi;
74 # redirect with trailing /
77 my $from = 'http://example.com/test';
79 my $res = $cb->(GET($from));
80 is(301, $res->code, 'is permanent redirect');
81 is($to, $res->header('Location'), 'redirect location matches');
84 my $pfx = 'http://example.com/test';
85 foreach my $t (qw(t T)) {
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 /');
96 foreach my $t (qw(f)) {
99 my $u = $pfx . "/blah%40example.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/\z!,
104 'redirected with missing /');
108 test_psgi($app, sub {
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/"!,
119 test_psgi($app, sub {
121 my $res = $cb->(GET($pfx . '/atom.xml'));
122 is(200, $res->code, 'success response received for atom');
124 qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
125 'atom feed generated correct URL');
128 foreach my $t (('', 'f/')) {
129 test_psgi($app, sub {
131 my $path = "/blah%40example.com/$t";
132 my $res = $cb->(GET($pfx . $path));
133 is(200, $res->code, "success for $path");
134 like($res->content, qr!<title>hihi - Me</title>!,
138 test_psgi($app, sub {
140 my $res = $cb->(GET($pfx . '/blah%40example.com/raw'));
141 is(200, $res->code, 'success response received for /*/raw');
142 like($res->content, qr!^From !sm, "mbox returned");
146 foreach my $t (qw(m f)) {
147 test_psgi($app, sub {
149 my $res = $cb->(GET($pfx . "/$t/blah%40example.com.txt"));
150 is(301, $res->code, "redirect for old $t .txt link");
151 my $location = $res->header('Location');
152 like($location, qr!/blah%40example\.com/raw\z!,
153 ".txt redirected to /raw");
162 while (my ($t, $e) = each %umap) {
163 test_psgi($app, sub {
165 my $res = $cb->(GET($pfx . "/$t/blah%40example.com.html"));
166 is(301, $res->code, "redirect for old $t .html link");
167 my $location = $res->header('Location');
169 qr!/blah%40example\.com/$e(?:#u)?\z!,
170 ".html redirected to new location");
173 foreach my $sfx (qw(mbox mbox.gz)) {
174 test_psgi($app, sub {
176 my $res = $cb->(GET($pfx . "/t/blah%40example.com.$sfx"));
177 is(301, $res->code, 'redirect for old thread link');
178 my $location = $res->header('Location');
180 qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
181 "$sfx redirected to /mbox.gz");
189 my ($env, @args) = @_;
190 my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
191 run(@args, init => $init);