1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # FIXME: this test is too slow and most non-CGI-requirements
4 # should be moved over to things which use test_psgi
9 use File::Temp qw/tempdir/;
10 eval { require IPC::Run };
11 plan skip_all => "missing IPC::Run for t/cgi.t" if $@;
13 use constant CGI => "blib/script/public-inbox.cgi";
14 my $tmpdir = tempdir('pi-cgi-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $home = "$tmpdir/pi-home";
16 my $pi_home = "$home/.public-inbox";
17 my $pi_config = "$pi_home/config";
18 my $maindir = "$tmpdir/main.git";
19 my $addr = 'test-public@example.com';
20 my $cfgpfx = "publicinbox.test";
23 is(1, mkdir($home, 0755), "setup ~/ for testing");
24 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
25 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
27 open my $fh, '>', "$maindir/description" or die "open: $!\n";
28 print $fh "test for public-inbox\n";
29 close $fh or die "close: $!\n";
31 "$cfgpfx.address" => $addr,
32 "$cfgpfx.mainrepo" => $maindir,
34 while (my ($k,$v) = each %cfg) {
35 is(0, system(qw(git config --file), $pi_config, $k, $v),
40 use_ok 'PublicInbox::Git';
41 use_ok 'PublicInbox::Import';
43 my $git = PublicInbox::Git->new($maindir);
44 my $im = PublicInbox::Import->new($git, 'test', $addr);
47 local $ENV{HOME} = $home;
49 # inject some messages:
50 my $mime = Email::MIME->new(<<EOF);
51 From: Me <me\@example.com>
52 To: You <you\@example.com>
54 Message-Id: <blah\@example.com>
56 Date: Thu, 01 Jan 1970 00:00:00 +0000
62 # deliver a reply, too
63 my $reply = Email::MIME->new(<<EOF);
64 From: You <you\@example.com>
65 To: Me <me\@example.com>
67 In-Reply-To: <blah\@example.com>
68 Message-Id: <blahblah\@example.com>
70 Date: Thu, 01 Jan 1970 00:00:01 +0000
79 my $slashy_mid = 'slashy/asdf@example.com';
80 my $slashy = Email::MIME->new(<<EOF);
81 From: You <you\@example.com>
82 To: Me <me\@example.com>
84 Message-Id: <$slashy_mid>
86 Date: Thu, 01 Jan 1970 00:00:01 +0000
93 my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
94 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
95 "slashy mid raw hit");
98 # retrieve thread as an mbox
100 local $ENV{HOME} = $home;
101 my $path = "/test/blahblah\@example.com/t.mbox.gz";
102 my $res = cgi_run($path);
103 like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
106 require PublicInbox::SearchIdx;
107 my $s = PublicInbox::SearchIdx->new($maindir, 1);
112 $res = cgi_run($path);
113 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
115 require IO::Uncompress::Gunzip;
116 my $in = $res->{body};
118 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
119 like($out, qr/^From /m, "From lines in mbox");
122 like($res->{head}, qr/^Status: 501 /, "search not available");
125 my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
126 if ($have_xml_feed) {
127 $path = "/test/blahblah\@example.com/t.atom";
128 $res = cgi_run($path);
129 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
130 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
131 "search returned atom");
132 my $p = XML::Feed->parse(\($res->{body}));
133 is($p->format, "Atom", "parsed atom feed");
134 is(scalar $p->entries, 3, "parsed three entries");
141 my ($env, @args) = @_;
142 IPC::Run::run(@args, init => sub { %ENV = (%ENV, %$env) });
148 QUERY_STRING => $_[1] || "",
150 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
151 REQUEST_METHOD => $_[2] || "GET",
152 GATEWAY_INTERFACE => 'CGI/1.1',
153 HTTP_ACCEPT => '*/*',
154 HTTP_HOST => 'test.example.com',
156 my ($in, $out, $err) = ("", "", "");
157 my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
158 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
159 { head => $head, body => $body, rc => $rc, err => $err }