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