]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_mount.t
update copyrights for 2021
[public-inbox.git] / t / psgi_mount.t
1 # Copyright (C) 2016-2021 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 $cfg = 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($cfg);
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,
71                 qr/^Message-Id: <blah\@example\.com>\n/sm,
72                 'headers appear in /raw');
73
74         # redirects
75         $res = $cb->(GET('/a/test/m/blah%40example.com.html'));
76         is($res->header('Location'),
77                 'http://localhost/a/test/blah@example.com/',
78                 'redirect functions properly under mount');
79
80         $res = $cb->(GET('/test/blah%40example.com/'));
81         is($res->code, 404, 'intentional 404 with URLMap mount');
82 });
83
84 SKIP: {
85         require_mods(qw(DBD::SQLite Search::Xapian IO::Uncompress::Gunzip), 3);
86         my $ibx = $cfg->lookup_name('test');
87         require_ok 'PublicInbox::SearchIdx';
88         PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
89         test_psgi($app, sub {
90                 my ($cb) = @_;
91                 my $res = $cb->(GET('/a/test/blah@example.com/t.mbox.gz'));
92                 my $gz = $res->content;
93                 my $raw;
94                 IO::Uncompress::Gunzip::gunzip(\$gz => \$raw);
95                 like($raw, qr!^Message-Id:\x20<blah\@example\.com>\n!sm,
96                         'headers appear in /t.mbox.gz mboxrd');
97         });
98 }
99
100 done_testing();