]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
ec45b02cc7297ec213ea44fbe8a0d58f00b928fd
[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 PublicInbox::TestCommon;
8 my $psgi = "./examples/public-inbox.psgi";
9 my ($tmpdir, $for_destroy) = tmpdir();
10 my $pi_config = "$tmpdir/config";
11 my $maindir = "$tmpdir/main.git";
12 my $addr = 'test-public@example.com';
13 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
14 require_mods(@mods);
15 use_ok 'PublicInbox::Import';
16 use_ok 'PublicInbox::Git';
17 my @ls;
18
19 foreach my $mod (@mods) { use_ok $mod; }
20 {
21         ok(-f $psgi, "psgi example file found");
22         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
23         open my $fh, '>', "$maindir/description" or die "open: $!\n";
24         print $fh "test for public-inbox\n";
25         close $fh or die "close: $!\n";
26         open $fh, '>>', $pi_config or die;
27         print $fh <<EOF or die;
28 [publicinbox "test"]
29         address = $addr
30         inboxdir = $maindir
31         url = http://example.com/test/
32         newsgroup = inbox.test
33 EOF
34         close $fh or die;
35
36         # ensure successful message delivery
37         {
38                 my $mime = Email::MIME->new(<<EOF);
39 From: Me <me\@example.com>
40 To: You <you\@example.com>
41 Cc: $addr
42 Message-Id: <blah\@example.com>
43 Subject: hihi
44 Date: Fri, 02 Oct 1993 00:00:00 +0000
45
46 zzzzzz
47 EOF
48                 my $git = PublicInbox::Git->new($maindir);
49                 my $im = PublicInbox::Import->new($git, 'test', $addr);
50                 $im->add($mime);
51                 $im->done;
52                 my $rev = $git->qx(qw(rev-list HEAD));
53                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
54                 @ls = $git->qx(qw(ls-tree -r --name-only HEAD));
55                 chomp @ls;
56         }
57         my $app = eval {
58                 local $ENV{PI_CONFIG} = $pi_config;
59                 require $psgi;
60         };
61
62         test_psgi($app, sub {
63                 my ($cb) = @_;
64                 foreach my $u (qw(robots.txt favicon.ico .well-known/foo)) {
65                         my $res = $cb->(GET("http://example.com/$u"));
66                         is($res->code, 404, "$u is missing");
67                 }
68         });
69
70         # redirect with newsgroup
71         test_psgi($app, sub {
72                 my ($cb) = @_;
73                 my $from = 'http://example.com/inbox.test';
74                 my $to = 'http://example.com/test/';
75                 my $res = $cb->(GET($from));
76                 is($res->code, 301, 'newsgroup name is permanent redirect');
77                 is($to, $res->header('Location'), 'redirect location matches');
78                 $from .= '/';
79                 is($res->code, 301, 'newsgroup name/ is permanent redirect');
80                 is($to, $res->header('Location'), 'redirect location matches');
81         });
82
83         # redirect with trailing /
84         test_psgi($app, sub {
85                 my ($cb) = @_;
86                 my $from = 'http://example.com/test';
87                 my $to = "$from/";
88                 my $res = $cb->(GET($from));
89                 is(301, $res->code, 'is permanent redirect');
90                 is($to, $res->header('Location'),
91                         'redirect location matches with trailing slash');
92         });
93
94         my $pfx = 'http://example.com/test';
95         foreach my $t (qw(t T)) {
96                 test_psgi($app, sub {
97                         my ($cb) = @_;
98                         my $u = $pfx . "/blah\@example.com/$t";
99                         my $res = $cb->(GET($u));
100                         is(301, $res->code, "redirect for missing /");
101                         my $location = $res->header('Location');
102                         like($location, qr!/\Q$t\E/#u\z!,
103                                 'redirected with missing /');
104                 });
105         }
106         foreach my $t (qw(f)) {
107                 test_psgi($app, sub {
108                         my ($cb) = @_;
109                         my $u = $pfx . "/blah\@example.com/$t";
110                         my $res = $cb->(GET($u));
111                         is(301, $res->code, "redirect for legacy /f");
112                         my $location = $res->header('Location');
113                         like($location, qr!/blah\@example\.com/\z!,
114                                 'redirected with missing /');
115                 });
116         }
117
118         test_psgi($app, sub {
119                 my ($cb) = @_;
120                 my $atomurl = 'http://example.com/test/new.atom';
121                 my $res = $cb->(GET('http://example.com/test/new.html'));
122                 is(200, $res->code, 'success response received');
123                 like($res->content, qr!href="new\.atom"!,
124                         'atom URL generated');
125                 like($res->content, qr!href="blah\@example\.com/"!,
126                         'index generated');
127                 like($res->content, qr!1993-10-02!, 'date set');
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                 my $body = $res->content;
135                 like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
136                         'atom feed generated correct URL');
137                 like($body, qr/<title>test for public-inbox/,
138                         "set title in XML feed");
139                 like($body, qr/zzzzzz/, 'body included');
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         # dumb HTTP clone/fetch support
221         test_psgi($app, sub {
222                 my ($cb) = @_;
223                 my $path = '/test/info/refs';
224                 my $req = HTTP::Request->new('GET' => $path);
225                 my $res = $cb->($req);
226                 is(200, $res->code, 'refs readable');
227                 my $orig = $res->content;
228
229                 $req->header('Range', 'bytes=5-10');
230                 $res = $cb->($req);
231                 is(206, $res->code, 'got partial response');
232                 is($res->content, substr($orig, 5, 6), 'partial body OK');
233
234                 $req->header('Range', 'bytes=5-');
235                 $res = $cb->($req);
236                 is(206, $res->code, 'got partial another response');
237                 is($res->content, substr($orig, 5), 'partial body OK past end');
238         });
239
240         # things which should fail
241         test_psgi($app, sub {
242                 my ($cb) = @_;
243
244                 my $res = $cb->(PUT('/'));
245                 is(405, $res->code, 'no PUT to / allowed');
246                 $res = $cb->(PUT('/test/'));
247                 is(405, $res->code, 'no PUT /$INBOX allowed');
248
249                 # TODO
250                 # $res = $cb->(GET('/'));
251         });
252 }
253
254 done_testing();