]> Sergey Matveev's repositories - public-inbox.git/blob - t/extindex-psgi.t
www: drop --subject from "git send-email" instructions
[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 use IO::Uncompress::Gunzip qw(gunzip);
15 require PublicInbox::WWW;
16 my ($ro_home, $cfg_path) = setup_public_inboxes;
17 my ($tmpdir, $for_destroy) = tmpdir;
18 my $home = "$tmpdir/home";
19 mkdir $home or BAIL_OUT $!;
20 mkdir "$home/.public-inbox" or BAIL_OUT $!;
21 my $pi_config = "$home/.public-inbox/config";
22 cp($cfg_path, $pi_config) or BAIL_OUT;
23 my $env = { HOME => $home };
24 run_script([qw(-extindex --all), "$tmpdir/eidx"], $env) or BAIL_OUT;
25 {
26         open my $cfgfh, '>>', $pi_config or BAIL_OUT;
27         $cfgfh->autoflush(1);
28         print $cfgfh <<EOM or BAIL_OUT;
29 [extindex "all"]
30         topdir = $tmpdir/eidx
31         url = http://bogus.example.com/all
32 [publicinbox]
33         wwwlisting = all
34         grokManifest = all
35 EOM
36 }
37 my $www = PublicInbox::WWW->new(PublicInbox::Config->new($pi_config));
38 my $client = sub {
39         my ($cb) = @_;
40         my $res = $cb->(GET('/all/'));
41         is($res->code, 200, '/all/ good');
42         $res = $cb->(GET('/all/new.atom', Host => 'usethis.example.com'));
43         like($res->content, qr!http://usethis\.example\.com/!s,
44                 'Host: header respected in Atom feed');
45         unlike($res->content, qr!http://bogus\.example\.com/!s,
46                 'default URL ignored with different host header');
47
48         $res = $cb->(GET('/all/_/text/config/'));
49         is($res->code, 200, '/text/config HTML');
50         $res = $cb->(GET('/all/_/text/config/raw'));
51         is($res->code, 200, '/text/config raw');
52         my $f = "$tmpdir/extindex.config";
53         open my $fh, '>', $f or xbail $!;
54         print $fh $res->content or xbail $!;
55         close $fh or xbail $!;
56         my $cfg = PublicInbox::Config->git_config_dump($f);
57         is($?, 0, 'no errors from git-config parsing');
58         ok($cfg->{'extindex.all.topdir'}, 'extindex.topdir defined');
59
60         $res = $cb->(GET('/all/all.mbox.gz'));
61         is($res->code, 200, 'all.mbox.gz');
62
63         $res = $cb->(GET('/'));
64         like($res->content, qr!\Qhttp://bogus.example.com/all\E!,
65                 '/all listed');
66         $res = $cb->(GET('/?q='));
67         is($res->code, 200, 'no query means all inboxes');
68         $res = $cb->(GET('/?q=nonexistent'));
69         is($res->code, 404, 'no inboxes matched');
70         unlike($res->content, qr!no inboxes, yet!,
71                 'we have inboxes, just no matches');
72
73         my $m = {};
74         for my $pfx (qw(/t1 /t2), '') {
75                 $res = $cb->(GET($pfx.'/manifest.js.gz'));
76                 gunzip(\($res->content) => \(my $js));
77                 $m->{$pfx} = json_utf8->decode($js);
78         }
79         is_deeply([sort keys %{$m->{''}}],
80                 [ sort(keys %{$m->{'/t1'}}, keys %{$m->{'/t2'}}) ],
81                 't1 + t2 = all');
82         is_deeply([ sort keys %{$m->{'/t2'}} ], [ '/t2/git/0.git' ],
83                 't2 manifest');
84         is_deeply([ sort keys %{$m->{'/t1'}} ], [ '/t1' ],
85                 't2 manifest');
86 };
87 test_psgi(sub { $www->call(@_) }, $client);
88 %$env = (%$env, TMPDIR => $tmpdir, PI_CONFIG => $pi_config);
89 test_httpd($env, $client);
90
91 done_testing;