]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
testcommon: add require_mods method and use it
[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 $cfgpfx = "publicinbox.test";
14 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
15 require_mods(@mods);
16 use_ok 'PublicInbox::Import';
17 use_ok 'PublicInbox::Git';
18 my @ls;
19
20 foreach my $mod (@mods) { use_ok $mod; }
21 {
22         ok(-f $psgi, "psgi example file found");
23         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
24         open my $fh, '>', "$maindir/description" or die "open: $!\n";
25         print $fh "test for public-inbox\n";
26         close $fh or die "close: $!\n";
27         my %cfg = (
28                 "$cfgpfx.address" => $addr,
29                 "$cfgpfx.inboxdir" => $maindir,
30                 "$cfgpfx.url" => 'http://example.com/test/',
31                 "$cfgpfx.newsgroup" => 'inbox.test',
32         );
33         while (my ($k,$v) = each %cfg) {
34                 is(0, system(qw(git config --file), $pi_config, $k, $v),
35                         "setup $k");
36         }
37
38         # ensure successful message delivery
39         {
40                 my $mime = Email::MIME->new(<<EOF);
41 From: Me <me\@example.com>
42 To: You <you\@example.com>
43 Cc: $addr
44 Message-Id: <blah\@example.com>
45 Subject: hihi
46 Date: Fri, 02 Oct 1993 00:00:00 +0000
47
48 zzzzzz
49 EOF
50                 my $git = PublicInbox::Git->new($maindir);
51                 my $im = PublicInbox::Import->new($git, 'test', $addr);
52                 $im->add($mime);
53                 $im->done;
54                 my $rev = $git->qx(qw(rev-list HEAD));
55                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
56                 @ls = $git->qx(qw(ls-tree -r --name-only HEAD));
57                 chomp @ls;
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'),
93                         'redirect location matches with trailing slash');
94         });
95
96         my $pfx = 'http://example.com/test';
97         foreach my $t (qw(t T)) {
98                 test_psgi($app, sub {
99                         my ($cb) = @_;
100                         my $u = $pfx . "/blah\@example.com/$t";
101                         my $res = $cb->(GET($u));
102                         is(301, $res->code, "redirect for missing /");
103                         my $location = $res->header('Location');
104                         like($location, qr!/\Q$t\E/#u\z!,
105                                 'redirected with missing /');
106                 });
107         }
108         foreach my $t (qw(f)) {
109                 test_psgi($app, sub {
110                         my ($cb) = @_;
111                         my $u = $pfx . "/blah\@example.com/$t";
112                         my $res = $cb->(GET($u));
113                         is(301, $res->code, "redirect for legacy /f");
114                         my $location = $res->header('Location');
115                         like($location, qr!/blah\@example\.com/\z!,
116                                 'redirected with missing /');
117                 });
118         }
119
120         test_psgi($app, sub {
121                 my ($cb) = @_;
122                 my $atomurl = 'http://example.com/test/new.atom';
123                 my $res = $cb->(GET('http://example.com/test/new.html'));
124                 is(200, $res->code, 'success response received');
125                 like($res->content, qr!href="new\.atom"!,
126                         'atom URL generated');
127                 like($res->content, qr!href="blah\@example\.com/"!,
128                         'index generated');
129                 like($res->content, qr!1993-10-02!, 'date set');
130         });
131
132         test_psgi($app, sub {
133                 my ($cb) = @_;
134                 my $res = $cb->(GET($pfx . '/atom.xml'));
135                 is(200, $res->code, 'success response received for atom');
136                 my $body = $res->content;
137                 like($body, qr!link\s+href="\Q$pfx\E/blah\@example\.com/"!s,
138                         'atom feed generated correct URL');
139                 like($body, qr/<title>test for public-inbox/,
140                         "set title in XML feed");
141                 like($body, qr/zzzzzz/, 'body included');
142         });
143
144         test_psgi($app, sub {
145                 my ($cb) = @_;
146                 my $path = '/blah@example.com/';
147                 my $res = $cb->(GET($pfx . $path));
148                 is(200, $res->code, "success for $path");
149                 like($res->content, qr!<title>hihi - Me</title>!,
150                         "HTML returned");
151
152                 $path .= 'f/';
153                 $res = $cb->(GET($pfx . $path));
154                 is(301, $res->code, "redirect for $path");
155                 my $location = $res->header('Location');
156                 like($location, qr!/blah\@example\.com/\z!,
157                         '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
158         });
159
160         test_psgi($app, sub {
161                 my ($cb) = @_;
162                 my $res = $cb->(GET($pfx . '/blah@example.com/raw'));
163                 is(200, $res->code, 'success response received for /*/raw');
164                 like($res->content, qr!^From !sm, "mbox returned");
165         });
166
167         # legacy redirects
168         foreach my $t (qw(m f)) {
169                 test_psgi($app, sub {
170                         my ($cb) = @_;
171                         my $res = $cb->(GET($pfx . "/$t/blah\@example.com.txt"));
172                         is(301, $res->code, "redirect for old $t .txt link");
173                         my $location = $res->header('Location');
174                         like($location, qr!/blah\@example\.com/raw\z!,
175                                 ".txt redirected to /raw");
176                 });
177         }
178
179         my %umap = (
180                 'm' => '',
181                 'f' => '',
182                 't' => 't/',
183         );
184         while (my ($t, $e) = each %umap) {
185                 test_psgi($app, sub {
186                         my ($cb) = @_;
187                         my $res = $cb->(GET($pfx . "/$t/blah\@example.com.html"));
188                         is(301, $res->code, "redirect for old $t .html link");
189                         my $location = $res->header('Location');
190                         like($location,
191                                 qr!/blah\@example\.com/$e(?:#u)?\z!,
192                                 ".html redirected to new location");
193                 });
194         }
195         foreach my $sfx (qw(mbox mbox.gz)) {
196                 test_psgi($app, sub {
197                         my ($cb) = @_;
198                         my $res = $cb->(GET($pfx . "/t/blah\@example.com.$sfx"));
199                         is(301, $res->code, 'redirect for old thread link');
200                         my $location = $res->header('Location');
201                         like($location,
202                              qr!/blah\@example\.com/t\.mbox(?:\.gz)?\z!,
203                              "$sfx redirected to /mbox.gz");
204                 });
205         }
206         test_psgi($app, sub {
207                 my ($cb) = @_;
208                 # for a while, we used to support /$INBOX/$X40/
209                 # when we "compressed" long Message-IDs to SHA-1
210                 # Now we're stuck supporting them forever :<
211                 foreach my $path (@ls) {
212                         $path =~ tr!/!!d;
213                         my $from = "http://example.com/test/$path/";
214                         my $res = $cb->(GET($from));
215                         is(301, $res->code, 'is permanent redirect');
216                         like($res->header('Location'),
217                                 qr!/test/blah\@example\.com/!,
218                                 'redirect from x40 MIDs works');
219                 }
220         });
221
222         # dumb HTTP clone/fetch support
223         test_psgi($app, sub {
224                 my ($cb) = @_;
225                 my $path = '/test/info/refs';
226                 my $req = HTTP::Request->new('GET' => $path);
227                 my $res = $cb->($req);
228                 is(200, $res->code, 'refs readable');
229                 my $orig = $res->content;
230
231                 $req->header('Range', 'bytes=5-10');
232                 $res = $cb->($req);
233                 is(206, $res->code, 'got partial response');
234                 is($res->content, substr($orig, 5, 6), 'partial body OK');
235
236                 $req->header('Range', 'bytes=5-');
237                 $res = $cb->($req);
238                 is(206, $res->code, 'got partial another response');
239                 is($res->content, substr($orig, 5), 'partial body OK past end');
240         });
241
242         # things which should fail
243         test_psgi($app, sub {
244                 my ($cb) = @_;
245
246                 my $res = $cb->(PUT('/'));
247                 is(405, $res->code, 'no PUT to / allowed');
248                 $res = $cb->(PUT('/test/'));
249                 is(405, $res->code, 'no PUT /$INBOX allowed');
250
251                 # TODO
252                 # $res = $cb->(GET('/'));
253         });
254 }
255
256 done_testing();