]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
run update-copyrights from gnulib for 2019
[public-inbox.git] / t / plack.t
1 # Copyright (C) 2014-2019 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: Fri, 02 Oct 1993 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'),
96                         'redirect location matches with trailing slash');
97         });
98
99         my $pfx = 'http://example.com/test';
100         foreach my $t (qw(t T)) {
101                 test_psgi($app, sub {
102                         my ($cb) = @_;
103                         my $u = $pfx . "/blah\@example.com/$t";
104                         my $res = $cb->(GET($u));
105                         is(301, $res->code, "redirect for missing /");
106                         my $location = $res->header('Location');
107                         like($location, qr!/\Q$t\E/#u\z!,
108                                 'redirected with missing /');
109                 });
110         }
111         foreach my $t (qw(f)) {
112                 test_psgi($app, sub {
113                         my ($cb) = @_;
114                         my $u = $pfx . "/blah\@example.com/$t";
115                         my $res = $cb->(GET($u));
116                         is(301, $res->code, "redirect for legacy /f");
117                         my $location = $res->header('Location');
118                         like($location, qr!/blah\@example\.com/\z!,
119                                 'redirected with missing /');
120                 });
121         }
122
123         test_psgi($app, sub {
124                 my ($cb) = @_;
125                 my $atomurl = 'http://example.com/test/new.atom';
126                 my $res = $cb->(GET('http://example.com/test/new.html'));
127                 is(200, $res->code, 'success response received');
128                 like($res->content, qr!href="new\.atom"!,
129                         'atom URL generated');
130                 like($res->content, qr!href="blah\@example\.com/"!,
131                         'index generated');
132                 like($res->content, qr!1993-10-02!, 'date set');
133         });
134
135         test_psgi($app, sub {
136                 my ($cb) = @_;
137                 my $res = $cb->(GET($pfx . '/atom.xml'));
138                 is(200, $res->code, 'success response received for atom');
139                 my $body = $res->content;
140                 like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
141                         'atom feed generated correct URL');
142                 like($body, qr/<title>test for public-inbox/,
143                         "set title in XML feed");
144                 like($body, qr/zzzzzz/, 'body included');
145         });
146
147         test_psgi($app, sub {
148                 my ($cb) = @_;
149                 my $path = '/blah@example.com/';
150                 my $res = $cb->(GET($pfx . $path));
151                 is(200, $res->code, "success for $path");
152                 like($res->content, qr!<title>hihi - Me</title>!,
153                         "HTML returned");
154
155                 $path .= 'f/';
156                 $res = $cb->(GET($pfx . $path));
157                 is(301, $res->code, "redirect for $path");
158                 my $location = $res->header('Location');
159                 like($location, qr!/blah\@example\.com/\z!,
160                         '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
161         });
162
163         test_psgi($app, sub {
164                 my ($cb) = @_;
165                 my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
166                 is(200, $res->code, 'success response received for /*/raw');
167                 like($res->content, qr!^From !sm, "mbox returned");
168         });
169
170         # legacy redirects
171         foreach my $t (qw(m f)) {
172                 test_psgi($app, sub {
173                         my ($cb) = @_;
174                         my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
175                         is(301, $res->code, "redirect for old $t .txt link");
176                         my $location = $res->header('Location');
177                         like($location, qr!/blah\@example\.com/raw\z!,
178                                 ".txt redirected to /raw");
179                 });
180         }
181
182         my %umap = (
183                 'm' => '',
184                 'f' => '',
185                 't' => 't/',
186         );
187         while (my ($t, $e) = each %umap) {
188                 test_psgi($app, sub {
189                         my ($cb) = @_;
190                         my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
191                         is(301, $res->code, "redirect for old $t .html link");
192                         my $location = $res->header('Location');
193                         like($location,
194                                 qr!/blah\@example\.com/$e(?:#u)?\z!,
195                                 ".html redirected to new location");
196                 });
197         }
198         foreach my $sfx (qw(mbox mbox.gz)) {
199                 test_psgi($app, sub {
200                         my ($cb) = @_;
201                         my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
202                         is(301, $res->code, 'redirect for old thread link');
203                         my $location = $res->header('Location');
204                         like($location,
205                              qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
206                              "$sfx redirected to /mbox.gz");
207                 });
208         }
209         test_psgi($app, sub {
210                 my ($cb) = @_;
211                 # for a while, we used to support /$INBOX/$X40/
212                 # when we "compressed" long Message-IDs to SHA-1
213                 # Now we're stuck supporting them forever :<
214                 foreach my $path (@ls) {
215                         $path =~ tr!/!!d;
216                         my $from = "http://example.com/test/$path/";
217                         my $res = $cb->(GET($from));
218                         is(301, $res->code, 'is permanent redirect');
219                         like($res->header('Location'),
220                                 qr!/test/blah\@example\.com/!,
221                                 'redirect from x40 MIDs works');
222                 }
223         });
224
225         # dumb HTTP clone/fetch support
226         test_psgi($app, sub {
227                 my ($cb) = @_;
228                 my $path = '/test/info/refs';
229                 my $req = HTTP::Request->new('GET' => $path);
230                 my $res = $cb->($req);
231                 is(200, $res->code, 'refs readable');
232                 my $orig = $res->content;
233
234                 $req->header('Range', 'bytes=5-10');
235                 $res = $cb->($req);
236                 is(206, $res->code, 'got partial response');
237                 is($res->content, substr($orig, 5, 6), 'partial body OK');
238
239                 $req->header('Range', 'bytes=5-');
240                 $res = $cb->($req);
241                 is(206, $res->code, 'got partial another response');
242                 is($res->content, substr($orig, 5), 'partial body OK past end');
243         });
244
245         # things which should fail
246         test_psgi($app, sub {
247                 my ($cb) = @_;
248
249                 my $res = $cb->(PUT('/'));
250                 is(405, $res->code, 'no PUT to / allowed');
251                 $res = $cb->(PUT('/test/'));
252                 is(405, $res->code, 'no PUT /$INBOX allowed');
253
254                 # TODO
255                 # $res = $cb->(GET('/'));
256         });
257 }
258
259 done_testing();