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