]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_mount.t
public-inbox 1.1.0-pre1
[public-inbox.git] / t / psgi_mount.t
1 # Copyright (C) 2016-2018 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({
25         "$cfgpfx.address" => $addr,
26         "$cfgpfx.mainrepo" => $maindir,
27 });
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         # redirects
64         $res = $cb->(GET('/a/test/blah%40example.com/'));
65         is($res->code, 200, 'OK with URLMap mount');
66         $res = $cb->(GET('/a/test/blah%40example.com/raw'));
67         is($res->code, 200, 'OK with URLMap mount');
68         $res = $cb->(GET('/a/test/m/blah%40example.com.html'));
69         is($res->header('Location'),
70                 'http://localhost/a/test/blah@example.com/',
71                 'redirect functions properly under mount');
72
73         $res = $cb->(GET('/test/blah%40example.com/'));
74         is($res->code, 404, 'intentional 404 with URLMap mount');
75
76 });
77
78 done_testing();