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