]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_mount.t
doc: note -compact and -xcpdb are rarely used
[public-inbox.git] / t / psgi_mount.t
1 # Copyright (C) 2016-2020 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 PublicInbox::Eml;
7 use PublicInbox::TestCommon;
8 my ($tmpdir, $for_destroy) = tmpdir();
9 my $maindir = "$tmpdir/main.git";
10 my $addr = 'test-public@example.com';
11 my $cfgpfx = "publicinbox.test";
12 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape
13         Plack::Builder Plack::App::URLMap);
14 require_mods(@mods);
15 use_ok $_ foreach @mods;
16 use_ok 'PublicInbox::WWW';
17 use PublicInbox::Import;
18 use PublicInbox::Git;
19 use PublicInbox::Config;
20 my $config = PublicInbox::Config->new(\<<EOF);
21 $cfgpfx.address=$addr
22 $cfgpfx.inboxdir=$maindir
23 EOF
24 my $git = PublicInbox::Git->new($maindir);
25 my $im = PublicInbox::Import->new($git, 'test', $addr);
26 $im->init_bare;
27 {
28         my $mime = PublicInbox::Eml->new(<<EOF);
29 From: Me <me\@example.com>
30 To: You <you\@example.com>
31 Cc: $addr
32 Message-Id: <blah\@example.com>
33 Subject: hihi
34 Date: Thu, 01 Jan 1970 00:00:00 +0000
35
36 zzzzzz
37 EOF
38         $im->add($mime);
39         $im->done;
40 }
41
42 my $www = PublicInbox::WWW->new($config);
43 my $app = builder(sub {
44         enable('Head');
45         mount('/a' => builder(sub { sub { $www->call(@_) } }));
46         mount('/b' => builder(sub { sub { $www->call(@_) } }));
47 });
48
49 test_psgi($app, sub {
50         my ($cb) = @_;
51         my $res;
52         # Atom feed:
53         $res = $cb->(GET('/a/test/new.atom'));
54         like($res->content, qr!\bhttp://[^/]+/a/test/!,
55                 'URLs which exist in Atom feed are mount-aware');
56         unlike($res->content, qr!\b\Qhttp://[^/]+/test/\E!,
57                 'No URLs which are not mount-aware');
58
59         $res = $cb->(GET('/a/test/new.html'));
60         like($res->content, qr!git clone --mirror http://[^/]+/a/test\b!,
61                 'clone URL in new.html is mount-aware');
62
63         $res = $cb->(GET('/a/test/blah%40example.com/'));
64         is($res->code, 200, 'OK with URLMap mount');
65         like($res->content, qr!git clone --mirror http://[^/]+/a/test\b!,
66                 'clone URL in /$INBOX/$MESSAGE_ID/ is mount-aware');
67
68         $res = $cb->(GET('/a/test/blah%40example.com/raw'));
69         is($res->code, 200, 'OK with URLMap mount');
70         like($res->content, qr!^List-Archive: <http://[^/]+/a/test/>!m,
71                 'List-Archive set in /raw mboxrd');
72         like($res->content,
73                 qr!^Archived-At: <http://[^/]+/a/test/blah\@example\.com/>!m,
74                 'Archived-At set in /raw mboxrd');
75
76         # redirects
77         $res = $cb->(GET('/a/test/m/blah%40example.com.html'));
78         is($res->header('Location'),
79                 'http://localhost/a/test/blah@example.com/',
80                 'redirect functions properly under mount');
81
82         $res = $cb->(GET('/test/blah%40example.com/'));
83         is($res->code, 404, 'intentional 404 with URLMap mount');
84 });
85
86 SKIP: {
87         require_mods(qw(DBD::SQLite Search::Xapian IO::Uncompress::Gunzip), 3);
88         my $ibx = $config->lookup_name('test');
89         require_ok 'PublicInbox::SearchIdx';
90         PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
91         test_psgi($app, sub {
92                 my ($cb) = @_;
93                 my $res = $cb->(GET('/a/test/blah@example.com/t.mbox.gz'));
94                 my $gz = $res->content;
95                 my $raw;
96                 IO::Uncompress::Gunzip::gunzip(\$gz => \$raw);
97                 like($raw, qr!^List-Archive: <http://[^/]+/a/test/>!m,
98                         'List-Archive set in /t.mbox.gz mboxrd');
99                 like($raw,
100                         qr!^Archived-At:\x20
101                                 <http://[^/]+/a/test/blah\@example\.com/>!mx,
102                         'Archived-At set in /t.mbox.gz mboxrd');
103         });
104 }
105
106 done_testing();