]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
t/*.t: reduce -mda calls
[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 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) {
17         eval "require $mod";
18         plan skip_all => "$mod missing for plack.t" if $@;
19 }
20 use_ok 'PublicInbox::Import';
21 use_ok 'PublicInbox::Git';
22
23 foreach my $mod (@mods) { use_ok $mod; }
24 {
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";
30         my %cfg = (
31                 "$cfgpfx.address" => $addr,
32                 "$cfgpfx.mainrepo" => $maindir,
33         );
34         while (my ($k,$v) = each %cfg) {
35                 is(0, system(qw(git config --file), $pi_config, $k, $v),
36                         "setup $k");
37         }
38
39         # ensure successful message delivery
40         {
41                 my $mime = Email::MIME->new(<<EOF);
42 From: Me <me\@example.com>
43 To: You <you\@example.com>
44 Cc: $addr
45 Message-Id: <blah\@example.com>
46 Subject: hihi
47 Date: Thu, 01 Jan 1970 00:00:00 +0000
48
49 zzzzzz
50 EOF
51                 my $git = PublicInbox::Git->new($maindir);
52                 my $im = PublicInbox::Import->new($git, 'test', $addr);
53                 $im->add($mime);
54                 $im->done;
55                 my $rev = `git --git-dir="$maindir" rev-list HEAD`;
56                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
57         }
58         my $app = eval {
59                 local $ENV{PI_CONFIG} = $pi_config;
60                 require $psgi;
61         };
62
63         # redirect with trailing /
64         test_psgi($app, sub {
65                 my ($cb) = @_;
66                 my $from = 'http://example.com/test';
67                 my $to = "$from/";
68                 my $res = $cb->(GET($from));
69                 is(301, $res->code, 'is permanent redirect');
70                 is($to, $res->header('Location'), 'redirect location matches');
71         });
72
73         my $pfx = 'http://example.com/test';
74         foreach my $t (qw(t T)) {
75                 test_psgi($app, sub {
76                         my ($cb) = @_;
77                         my $u = $pfx . "/blah%40example.com/$t";
78                         my $res = $cb->(GET($u));
79                         is(301, $res->code, "redirect for missing /");
80                         my $location = $res->header('Location');
81                         like($location, qr!/\Q$t\E/#u\z!,
82                                 'redirected with missing /');
83                 });
84         }
85         foreach my $t (qw(f)) {
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 legacy /f");
91                         my $location = $res->header('Location');
92                         like($location, qr!/blah%40example\.com/\z!,
93                                 'redirected with missing /');
94                 });
95         }
96
97         test_psgi($app, sub {
98                 my ($cb) = @_;
99                 my $atomurl = 'http://example.com/test/new.atom';
100                 my $res = $cb->(GET('http://example.com/test/'));
101                 is(200, $res->code, 'success response received');
102                 like($res->content, qr!href="\Q$atomurl\E"!,
103                         'atom URL generated');
104                 like($res->content, qr!href="blah%40example\.com/"!,
105                         'index generated');
106         });
107
108         test_psgi($app, sub {
109                 my ($cb) = @_;
110                 my $res = $cb->(GET($pfx . '/atom.xml'));
111                 is(200, $res->code, 'success response received for atom');
112                 like($res->content,
113                         qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
114                         'atom feed generated correct URL');
115         });
116
117         test_psgi($app, sub {
118                 my ($cb) = @_;
119                 my $path = '/blah%40example.com/';
120                 my $res = $cb->(GET($pfx . $path));
121                 is(200, $res->code, "success for $path");
122                 like($res->content, qr!<title>hihi - Me</title>!,
123                         "HTML returned");
124
125                 $path .= 'f/';
126                 $res = $cb->(GET($pfx . $path));
127                 is(301, $res->code, "redirect for $path");
128                 my $location = $res->header('Location');
129                 like($location, qr!/blah%40example\.com/\z!,
130                         '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
131         });
132
133         test_psgi($app, sub {
134                 my ($cb) = @_;
135                 my $res = $cb->(GET($pfx . '/blah%40example.com/raw'));
136                 is(200, $res->code, 'success response received for /*/raw');
137                 like($res->content, qr!^From !sm, "mbox returned");
138         });
139
140         # legacy redirects
141         foreach my $t (qw(m f)) {
142                 test_psgi($app, sub {
143                         my ($cb) = @_;
144                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.txt"));
145                         is(301, $res->code, "redirect for old $t .txt link");
146                         my $location = $res->header('Location');
147                         like($location, qr!/blah%40example\.com/raw\z!,
148                                 ".txt redirected to /raw");
149                 });
150         }
151
152         my %umap = (
153                 'm' => '',
154                 'f' => '',
155                 't' => 't/',
156         );
157         while (my ($t, $e) = each %umap) {
158                 test_psgi($app, sub {
159                         my ($cb) = @_;
160                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.html"));
161                         is(301, $res->code, "redirect for old $t .html link");
162                         my $location = $res->header('Location');
163                         like($location,
164                                 qr!/blah%40example\.com/$e(?:#u)?\z!,
165                                 ".html redirected to new location");
166                 });
167         }
168         foreach my $sfx (qw(mbox mbox.gz)) {
169                 test_psgi($app, sub {
170                         my ($cb) = @_;
171                         my $res = $cb->(GET($pfx . "/t/blah%40example.com.$sfx"));
172                         is(301, $res->code, 'redirect for old thread link');
173                         my $location = $res->header('Location');
174                         like($location,
175                              qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
176                              "$sfx redirected to /mbox.gz");
177                 });
178         }
179 }
180
181 done_testing();