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