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/;
11 eval { require IPC::Run };
12 plan skip_all => "missing IPC::Run for t/cgi.t" if $@;
14 use constant CGI => "blib/script/public-inbox.cgi";
15 my $index = "blib/script/public-inbox-index";
16 my $tmpdir = tempdir('pi-cgi-XXXXXX', TMPDIR => 1, CLEANUP => 1);
17 my $home = "$tmpdir/pi-home";
18 my $pi_home = "$home/.public-inbox";
19 my $pi_config = "$pi_home/config";
20 my $maindir = "$tmpdir/main.git";
21 my $addr = 'test-public@example.com';
22 my $cfgpfx = "publicinbox.test";
25 is(1, mkdir($home, 0755), "setup ~/ for testing");
26 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
27 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
29 open my $fh, '>', "$maindir/description" or die "open: $!\n";
30 print $fh "test for public-inbox\n";
31 close $fh or die "close: $!\n";
33 "$cfgpfx.address" => $addr,
34 "$cfgpfx.mainrepo" => $maindir,
36 while (my ($k,$v) = each %cfg) {
37 is(0, system(qw(git config --file), $pi_config, $k, $v),
42 use_ok 'PublicInbox::Git';
43 use_ok 'PublicInbox::Import';
45 my $git = PublicInbox::Git->new($maindir);
46 my $im = PublicInbox::Import->new($git, 'test', $addr);
49 local $ENV{HOME} = $home;
51 # inject some messages:
52 my $mime = Email::MIME->new(<<EOF);
53 From: Me <me\@example.com>
54 To: You <you\@example.com>
56 Message-Id: <blah\@example.com>
58 Date: Thu, 01 Jan 1970 00:00:00 +0000
64 # deliver a reply, too
65 my $reply = Email::MIME->new(<<EOF);
66 From: You <you\@example.com>
67 To: Me <me\@example.com>
69 In-Reply-To: <blah\@example.com>
70 Message-Id: <blahblah\@example.com>
72 Date: Thu, 01 Jan 1970 00:00:01 +0000
83 # obvious failures, first
85 local $ENV{HOME} = $home;
86 my $res = cgi_run("/", "", "PUT");
87 like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
90 like($res->{head}, qr/Status:\s*404/i, "index returns 404");
95 local $ENV{HOME} = $home;
96 my $path = "/test/info/refs";
97 my $res = cgi_run($path);
98 like($res->{head}, qr/Status:\s*200/i, "info/refs readable");
99 my $orig = $res->{body};
101 local $ENV{HTTP_RANGE} = 'bytes=5-10';
102 $res = cgi_run($path);
103 like($res->{head}, qr/Status:\s*206/i, "info/refs partial OK");
104 is($res->{body}, substr($orig, 5, 6), 'partial body OK');
106 local $ENV{HTTP_RANGE} = 'bytes=5-';
107 $res = cgi_run($path);
108 like($res->{head}, qr/Status:\s*206/i, "info/refs partial past end OK");
109 is($res->{body}, substr($orig, 5), 'partial body OK past end');
114 local $ENV{HOME} = $home;
115 my $res = cgi_run("/test/atom.xml");
116 like($res->{body}, qr/<title>test for public-inbox/,
117 "set title in XML feed");
119 qr!http://test\.example\.com/test/blah\@example\.com/!,
121 like($res->{body}, qr/what\?/, "reply included");
126 local $ENV{HOME} = $home;
127 my $slashy_mid = 'slashy/asdf@example.com';
128 my $reply = Email::MIME->new(<<EOF);
129 From: You <you\@example.com>
130 To: Me <me\@example.com>
132 Message-Id: <$slashy_mid>
134 Date: Thu, 01 Jan 1970 00:00:01 +0000
141 my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
142 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
143 "slashy mid raw hit");
145 $res = cgi_run("/test/blahblah\@example.com/raw");
146 like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
149 $res = cgi_run("/test/blahblah\@example.com/");
150 like($res->{body}, qr/\A<html>/, "mid html hit");
151 like($res->{head}, qr/Status: 200 OK/, "200 response");
153 $res = cgi_run("/test/blahblah\@example.com/f/");
154 like($res->{head}, qr/Status: 301 Moved/, "301 response");
156 qr!^Location: http://[^/]+/test/blahblah\@example\.com/\r\n!ms,
157 '301 redirect location');
159 $res = cgi_run("/test/new.html");
160 like($res->{body}, qr/slashy%2Fasdf\@example\.com/,
161 "slashy URL generated correctly");
164 # retrieve thread as an mbox
166 local $ENV{HOME} = $home;
167 my $path = "/test/blahblah\@example.com/t.mbox.gz";
168 my $res = cgi_run($path);
169 like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
170 my $indexed = system($index, $maindir) == 0;
172 $res = cgi_run($path);
173 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
175 require IO::Uncompress::Gunzip;
176 my $in = $res->{body};
178 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
179 like($out, qr/^From /m, "From lines in mbox");
182 like($res->{head}, qr/^Status: 501 /, "search not available");
185 my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
186 if ($have_xml_feed) {
187 $path = "/test/blahblah\@example.com/t.atom";
188 $res = cgi_run($path);
189 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
190 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
191 "search returned atom");
192 my $p = XML::Feed->parse(\($res->{body}));
193 is($p->format, "Atom", "parsed atom feed");
194 is(scalar $p->entries, 3, "parsed three entries");
198 # redirect list-name-only URLs
200 local $ENV{HOME} = $home;
201 my $res = cgi_run("/test");
202 like($res->{head}, qr/Status: 301 Moved/, "redirected status");
203 like($res->{head}, qr!/test/!, "redirected with slash");
209 my ($env, @args) = @_;
210 IPC::Run::run(@args, init => sub { %ENV = (%ENV, %$env) });
216 QUERY_STRING => $_[1] || "",
218 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
219 REQUEST_METHOD => $_[2] || "GET",
220 GATEWAY_INTERFACE => 'CGI/1.1',
221 HTTP_ACCEPT => '*/*',
222 HTTP_HOST => 'test.example.com',
224 my ($in, $out, $err) = ("", "", "");
225 my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
226 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
227 { head => $head, body => $body, rc => $rc, err => $err }