Revert "daemon: check connections WIP"
[public-inbox.git] / t / extindex-psgi.t
blob5fa0b99d1f8a82633d2fdf7df6f06c8eeffb0660
1 #!perl -w
2 # Copyright (C) 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 Xapian psgi);
12 use IO::Uncompress::Gunzip qw(gunzip);
13 require PublicInbox::WWW;
14 my ($ro_home, $cfg_path) = setup_public_inboxes;
15 my ($tmpdir, $for_destroy) = tmpdir;
16 my $home = "$tmpdir/home";
17 mkdir $home or BAIL_OUT $!;
18 mkdir "$home/.public-inbox" or BAIL_OUT $!;
19 my $pi_config = "$home/.public-inbox/config";
20 cp($cfg_path, $pi_config) or BAIL_OUT;
21 my $env = { HOME => $home };
22 my $m2t = create_inbox 'mid2tid', version => 2, indexlevel => 'basic', sub {
23         my ($im, $ibx) = @_;
24         for my $n (1..3) {
25                 $im->add(PublicInbox::Eml->new(<<EOM)) or xbail 'add';
26 Date: Fri, 02 Oct 1993 00:0$n:00 +0000
27 Message-ID: <t\@$n>
28 Subject: tid $n
29 From: x\@example.com
30 References: <a-mid\@b>
33 EOM
34                 $im->add(PublicInbox::Eml->new(<<EOM)) or xbail 'add';
35 Date: Fri, 02 Oct 1993 00:0$n:00 +0000
36 Message-ID: <ut\@$n>
37 Subject: unrelated tid $n
38 From: x\@example.com
39 References: <b-mid\@b>
41 EOM
42         }
45         open my $cfgfh, '>>', $pi_config or BAIL_OUT;
46         $cfgfh->autoflush(1);
47         print $cfgfh <<EOM or BAIL_OUT;
48 [extindex "all"]
49         topdir = $tmpdir/eidx
50         url = http://bogus.example.com/all
51 [publicinbox]
52         wwwlisting = all
53         grokManifest = all
54 [publicinbox "m2t"]
55         inboxdir = $m2t->{inboxdir}
56         address = $m2t->{-primary_address}
57 EOM
58         close $cfgfh or xbail "close: $!";
61 run_script([qw(-extindex --all), "$tmpdir/eidx"], $env) or BAIL_OUT;
62 my $www = PublicInbox::WWW->new(PublicInbox::Config->new($pi_config));
63 my $client = sub {
64         my ($cb) = @_;
65         my $res = $cb->(GET('/all/'));
66         is($res->code, 200, '/all/ good');
67         $res = $cb->(GET('/all/new.atom', Host => 'usethis.example.com'));
68         like($res->content, qr!http://usethis\.example\.com/!s,
69                 'Host: header respected in Atom feed');
70         unlike($res->content, qr!http://bogus\.example\.com/!s,
71                 'default URL ignored with different host header');
73         $res = $cb->(GET('/all/_/text/config/'));
74         is($res->code, 200, '/text/config HTML');
75         $res = $cb->(GET('/all/_/text/config/raw'));
76         is($res->code, 200, '/text/config raw');
77         my $f = "$tmpdir/extindex.config";
78         open my $fh, '>', $f or xbail $!;
79         print $fh $res->content or xbail $!;
80         close $fh or xbail $!;
81         my $cfg = PublicInbox::Config->git_config_dump($f);
82         is($?, 0, 'no errors from git-config parsing');
83         ok($cfg->{'extindex.all.topdir'}, 'extindex.topdir defined');
85         $res = $cb->(GET('/all/all.mbox.gz'));
86         is($res->code, 200, 'all.mbox.gz');
88         $res = $cb->(GET('/'));
89         like($res->content, qr!\Qhttp://bogus.example.com/all\E!,
90                 '/all listed');
91         $res = $cb->(GET('/?q='));
92         is($res->code, 200, 'no query means all inboxes');
93         $res = $cb->(GET('/?q=nonexistent'));
94         is($res->code, 404, 'no inboxes matched');
95         unlike($res->content, qr!no inboxes, yet!,
96                 'we have inboxes, just no matches');
98         my $m = {};
99         for my $pfx (qw(/t1 /t2), '') {
100                 $res = $cb->(GET($pfx.'/manifest.js.gz'));
101                 gunzip(\($res->content) => \(my $js));
102                 $m->{$pfx} = json_utf8->decode($js);
103         }
104         is_deeply([sort keys %{$m->{''}}],
105                 [ sort(keys %{$m->{'/t1'}}, keys %{$m->{'/t2'}}) ],
106                 't1 + t2 = all');
107         is_deeply([ sort keys %{$m->{'/t2'}} ], [ '/t2/git/0.git' ],
108                 't2 manifest');
109         is_deeply([ sort keys %{$m->{'/t1'}} ], [ '/t1' ],
110                 't2 manifest');
112         # ensure ibx->{isrch}->{es}->over is used instead of ibx->over:
113         $res = $cb->(POST("/m2t/t\@1/?q=dt:19931002000259..&x=m"));
114         is($res->code, 200, 'hit on mid2tid query');
115         $res = $cb->(POST("/m2t/t\@1/?q=dt:19931002000400..&x=m"));
116         is($res->code, 404, '404 on out-of-range mid2tid query');
117         $res = $cb->(POST("/m2t/t\@1/?q=s:unrelated&x=m"));
118         is($res->code, 404, '404 on cross-thread search');
121         for my $c (qw(new active)) {
122                 $res = $cb->(GET("/m2t/topics_$c.html"));
123                 is($res->code, 200, "topics_$c.html on basic v2");
124                 $res = $cb->(GET("/all/topics_$c.html"));
125                 is($res->code, 200, "topics_$c.html on extindex");
126         }
128 test_psgi(sub { $www->call(@_) }, $client);
129 %$env = (%$env, TMPDIR => $tmpdir, PI_CONFIG => $pi_config);
130 test_httpd($env, $client);
132 done_testing;