]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
public-inbox 1.1.0-pre1
[public-inbox.git] / t / plack.t
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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 my @ls;
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                 "$cfgpfx.url" => 'http://example.com/test/',
34                 "$cfgpfx.newsgroup" => 'inbox.test',
35         );
36         while (my ($k,$v) = each %cfg) {
37                 is(0, system(qw(git config --file), $pi_config, $k, $v),
38                         "setup $k");
39         }
40
41         # ensure successful message delivery
42         {
43                 my $mime = Email::MIME->new(<<EOF);
44 From: Me <me\@example.com>
45 To: You <you\@example.com>
46 Cc: $addr
47 Message-Id: <blah\@example.com>
48 Subject: hihi
49 Date: Thu, 01 Jan 1970 00:00:00 +0000
50
51 zzzzzz
52 EOF
53                 my $git = PublicInbox::Git->new($maindir);
54                 my $im = PublicInbox::Import->new($git, 'test', $addr);
55                 $im->add($mime);
56                 $im->done;
57                 my $rev = `git --git-dir="$maindir" rev-list HEAD`;
58                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
59                 @ls = `git --git-dir="$maindir" ls-tree -r --name-only HEAD`;
60                 chomp @ls;
61         }
62         my $app = eval {
63                 local $ENV{PI_CONFIG} = $pi_config;
64                 require $psgi;
65         };
66
67         test_psgi($app, sub {
68                 my ($cb) = @_;
69                 foreach my $u (qw(robots.txt favicon.ico .well-known/foo)) {
70                         my $res = $cb->(GET("http://example.com/$u"));
71                         is($res->code, 404, "$u is missing");
72                 }
73         });
74
75         # redirect with newsgroup
76         test_psgi($app, sub {
77                 my ($cb) = @_;
78                 my $from = 'http://example.com/inbox.test';
79                 my $to = 'http://example.com/test/';
80                 my $res = $cb->(GET($from));
81                 is($res->code, 301, 'newsgroup name is permanent redirect');
82                 is($to, $res->header('Location'), 'redirect location matches');
83                 $from .= '/';
84                 is($res->code, 301, 'newsgroup name/ is permanent redirect');
85                 is($to, $res->header('Location'), 'redirect location matches');
86         });
87
88         # redirect with trailing /
89         test_psgi($app, sub {
90                 my ($cb) = @_;
91                 my $from = 'http://example.com/test';
92                 my $to = "$from/";
93                 my $res = $cb->(GET($from));
94                 is(301, $res->code, 'is permanent redirect');
95                 is($to, $res->header('Location'), 'redirect location matches');
96         });
97
98         my $pfx = 'http://example.com/test';
99         foreach my $t (qw(t T)) {
100                 test_psgi($app, sub {
101                         my ($cb) = @_;
102                         my $u = $pfx . "/blah\@example.com/$t";
103                         my $res = $cb->(GET($u));
104                         is(301, $res->code, "redirect for missing /");
105                         my $location = $res->header('Location');
106                         like($location, qr!/\Q$t\E/#u\z!,
107                                 'redirected with missing /');
108                 });
109         }
110         foreach my $t (qw(f)) {
111                 test_psgi($app, sub {
112                         my ($cb) = @_;
113                         my $u = $pfx . "/blah\@example.com/$t";
114                         my $res = $cb->(GET($u));
115                         is(301, $res->code, "redirect for legacy /f");
116                         my $location = $res->header('Location');
117                         like($location, qr!/blah\@example\.com/\z!,
118                                 'redirected with missing /');
119                 });
120         }
121
122         test_psgi($app, sub {
123                 my ($cb) = @_;
124                 my $atomurl = 'http://example.com/test/new.atom';
125                 my $res = $cb->(GET('http://example.com/test/new.html'));
126                 is(200, $res->code, 'success response received');
127                 like($res->content, qr!href="new\.atom"!,
128                         'atom URL generated');
129                 like($res->content, qr!href="blah\@example\.com/"!,
130                         'index generated');
131         });
132
133         test_psgi($app, sub {
134                 my ($cb) = @_;
135                 my $res = $cb->(GET($pfx . '/atom.xml'));
136                 is(200, $res->code, 'success response received for atom');
137                 like($res->content,
138                         qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
139                         'atom feed generated correct URL');
140         });
141
142         test_psgi($app, sub {
143                 my ($cb) = @_;
144                 my $path = '/blah@example.com/';
145                 my $res = $cb->(GET($pfx . $path));
146                 is(200, $res->code, "success for $path");
147                 like($res->content, qr!<title>hihi - Me</title>!,
148                         "HTML returned");
149
150                 $path .= 'f/';
151                 $res = $cb->(GET($pfx . $path));
152                 is(301, $res->code, "redirect for $path");
153                 my $location = $res->header('Location');
154                 like($location, qr!/blah\@example\.com/\z!,
155                         '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
156         });
157
158         test_psgi($app, sub {
159                 my ($cb) = @_;
160                 my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
161                 is(200, $res->code, 'success response received for /*/raw');
162                 like($res->content, qr!^From !sm, "mbox returned");
163         });
164
165         # legacy redirects
166         foreach my $t (qw(m f)) {
167                 test_psgi($app, sub {
168                         my ($cb) = @_;
169                         my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
170                         is(301, $res->code, "redirect for old $t .txt link");
171                         my $location = $res->header('Location');
172                         like($location, qr!/blah\@example\.com/raw\z!,
173                                 ".txt redirected to /raw");
174                 });
175         }
176
177         my %umap = (
178                 'm' => '',
179                 'f' => '',
180                 't' => 't/',
181         );
182         while (my ($t, $e) = each %umap) {
183                 test_psgi($app, sub {
184                         my ($cb) = @_;
185                         my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
186                         is(301, $res->code, "redirect for old $t .html link");
187                         my $location = $res->header('Location');
188                         like($location,
189                                 qr!/blah\@example\.com/$e(?:#u)?\z!,
190                                 ".html redirected to new location");
191                 });
192         }
193         foreach my $sfx (qw(mbox mbox.gz)) {
194                 test_psgi($app, sub {
195                         my ($cb) = @_;
196                         my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
197                         is(301, $res->code, 'redirect for old thread link');
198                         my $location = $res->header('Location');
199                         like($location,
200                              qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
201                              "$sfx redirected to /mbox.gz");
202                 });
203         }
204         test_psgi($app, sub {
205                 my ($cb) = @_;
206                 # for a while, we used to support /$INBOX/$X40/
207                 # when we "compressed" long Message-IDs to SHA-1
208                 # Now we're stuck supporting them forever :<
209                 foreach my $path (@ls) {
210                         $path =~ tr!/!!d;
211                         my $from = "http://example.com/test/$path/";
212                         my $res = $cb->(GET($from));
213                         is(301, $res->code, 'is permanent redirect');
214                         like($res->header('Location'),
215                                 qr!/test/blah\@example\.com/!,
216                                 'redirect from x40 MIDs works');
217                 }
218         });
219 }
220
221 done_testing();