]> Sergey Matveev's repositories - public-inbox.git/blob - t/extindex-psgi.t
extindex: add some validation and config knobs for WWW
[public-inbox.git] / t / extindex-psgi.t
1 #!perl -w
2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Config;
8 use File::Copy qw(cp);
9 use IO::Handle ();
10 require_git(2.6);
11 require_mods(qw(json DBD::SQLite Search::Xapian
12                 HTTP::Request::Common Plack::Test URI::Escape Plack::Builder));
13 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
14 require PublicInbox::WWW;
15 my ($ro_home, $cfg_path) = setup_public_inboxes;
16 my ($tmpdir, $for_destroy) = tmpdir;
17 my $home = "$tmpdir/home";
18 mkdir $home or BAIL_OUT $!;
19 mkdir "$home/.public-inbox" or BAIL_OUT $!;
20 my $pi_config = "$home/.public-inbox/config";
21 cp("$ro_home/.public-inbox/config", $pi_config) or BAIL_OUT;
22 my $env = { HOME => $home };
23 run_script([qw(-extindex --all), "$tmpdir/eidx"], $env) or BAIL_OUT;
24 {
25         open my $cfgfh, '>', $pi_config or BAIL_OUT;
26         $cfgfh->autoflush(1);
27         print $cfgfh <<EOM or BAIL_OUT;
28 [extindex "all"]
29         topdir = $tmpdir/eidx
30         url = http://bogus.example.com/all
31 EOM
32 }
33 my $www = PublicInbox::WWW->new(PublicInbox::Config->new($pi_config));
34 my $client = sub {
35         my ($cb) = @_;
36         my $res = $cb->(GET('/all/'));
37         is($res->code, 200, '/all/ good');
38         $res = $cb->(GET('/all/new.atom', Host => 'usethis.example.com'));
39         like($res->content, qr!http://usethis\.example\.com/!s,
40                 'Host: header respected in Atom feed');
41         unlike($res->content, qr!http://bogus\.example\.com/!s,
42                 'default URL ignored with different host header');
43 };
44 test_psgi(sub { $www->call(@_) }, $client);
45 %$env = (%$env, TMPDIR => $tmpdir, PI_CONFIG => $pi_config);
46 test_httpd($env, $client);
47
48 done_testing;