1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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/;
13 use constant CGI => "blib/script/public-inbox.cgi";
14 my $index = "blib/script/public-inbox-index";
15 my $tmpdir = tempdir('pi-cgi-XXXXXX', TMPDIR => 1, CLEANUP => 1);
16 my $home = "$tmpdir/pi-home";
17 my $pi_home = "$home/.public-inbox";
18 my $pi_config = "$pi_home/config";
19 my $maindir = "$tmpdir/main.git";
20 my $addr = 'test-public@example.com';
21 my $cfgpfx = "publicinbox.test";
24 is(1, mkdir($home, 0755), "setup ~/ for testing");
25 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
26 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
28 open my $fh, '>', "$maindir/description" or die "open: $!\n";
29 print $fh "test for public-inbox\n";
30 close $fh or die "close: $!\n";
32 "$cfgpfx.address" => $addr,
33 "$cfgpfx.mainrepo" => $maindir,
35 while (my ($k,$v) = each %cfg) {
36 is(0, system(qw(git config --file), $pi_config, $k, $v),
41 use_ok 'PublicInbox::Git';
42 use_ok 'PublicInbox::Import';
44 my $git = PublicInbox::Git->new($maindir);
45 my $im = PublicInbox::Import->new($git, 'test', $addr);
48 local $ENV{HOME} = $home;
50 # ensure successful message delivery
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 my $rev = `git --git-dir=$maindir rev-list HEAD`;
65 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
68 # deliver a reply, too
70 my $reply = Email::MIME->new(<<EOF);
71 From: You <you\@example.com>
72 To: Me <me\@example.com>
74 In-Reply-To: <blah\@example.com>
75 Message-Id: <blahblah\@example.com>
77 Date: Thu, 01 Jan 1970 00:00:01 +0000
86 my $rev = `git --git-dir=$maindir rev-list HEAD`;
87 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
92 # obvious failures, first
94 local $ENV{HOME} = $home;
95 my $res = cgi_run("/", "", "PUT");
96 like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
99 like($res->{head}, qr/Status:\s*404/i, "index returns 404");
104 local $ENV{HOME} = $home;
105 my $path = "/test/info/refs";
106 my $res = cgi_run($path);
107 like($res->{head}, qr/Status:\s*200/i, "info/refs readable");
108 my $orig = $res->{body};
110 local $ENV{HTTP_RANGE} = 'bytes=5-10';
111 $res = cgi_run($path);
112 like($res->{head}, qr/Status:\s*206/i, "info/refs partial OK");
113 is($res->{body}, substr($orig, 5, 6), 'partial body OK');
115 local $ENV{HTTP_RANGE} = 'bytes=5-';
116 $res = cgi_run($path);
117 like($res->{head}, qr/Status:\s*206/i, "info/refs partial past end OK");
118 is($res->{body}, substr($orig, 5), 'partial body OK past end');
123 local $ENV{HOME} = $home;
124 my $res = cgi_run("/test/atom.xml");
125 like($res->{body}, qr/<title>test for public-inbox/,
126 "set title in XML feed");
128 qr!http://test\.example\.com/test/blah%40example\.com/!,
130 like($res->{body}, qr/what\?/, "reply included");
135 local $ENV{HOME} = $home;
136 my $res = cgi_run("/test/");
137 like($res->{head}, qr/Status: 200 OK/, "index returns 200");
139 my $idx = cgi_run("/test/index.html");
140 $idx->{body} =~ s!/index.html(\?r=)!/$1!g; # dirty...
141 $idx->{body} = [ split(/\n/, $idx->{body}) ];
142 $res->{body} = [ split(/\n/, $res->{body}) ];
143 is_deeply($res, $idx,
144 '/$INBOX/ and /$INBOX/index.html are nearly identical');
145 # more checks in t/feed.t
150 local $ENV{HOME} = $home;
151 my $slashy_mid = 'slashy/asdf@example.com';
152 my $reply = Email::MIME->new(<<EOF);
153 From: You <you\@example.com>
154 To: Me <me\@example.com>
156 Message-Id: <$slashy_mid>
158 Date: Thu, 01 Jan 1970 00:00:01 +0000
165 my $res = cgi_run("/test/slashy%2fasdf%40example.com/raw");
166 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
167 "slashy mid raw hit");
169 $res = cgi_run("/test/blahblah\@example.com/raw");
170 like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
172 $res = cgi_run("/test/blahblah\@example.con/raw");
173 like($res->{head}, qr/Status: 300 Multiple Choices/, "mid raw miss");
175 $res = cgi_run("/test/blahblah\@example.com/");
176 like($res->{body}, qr/\A<html>/, "mid html hit");
177 like($res->{head}, qr/Status: 200 OK/, "200 response");
178 $res = cgi_run("/test/blahblah\@example.con/");
179 like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
181 $res = cgi_run("/test/blahblah\@example.com/f/");
182 like($res->{head}, qr/Status: 301 Moved/, "301 response");
184 qr!^Location: http://[^/]+/test/blahblah%40example\.com/\r\n!ms,
185 '301 redirect location');
186 $res = cgi_run("/test/blahblah\@example.con/");
187 like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
189 $res = cgi_run("/test/");
190 like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
191 "slashy URL generated correctly");
194 # retrieve thread as an mbox
196 local $ENV{HOME} = $home;
197 my $path = "/test/blahblah%40example.com/t.mbox.gz";
198 my $res = cgi_run($path);
199 like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
200 my $indexed = system($index, $maindir) == 0;
202 $res = cgi_run($path);
203 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
205 require IO::Uncompress::Gunzip;
206 my $in = $res->{body};
208 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
209 like($out, qr/^From /m, "From lines in mbox");
212 like($res->{head}, qr/^Status: 501 /, "search not available");
215 my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
216 if ($have_xml_feed) {
217 $path = "/test/blahblah%40example.com/t.atom";
218 $res = cgi_run($path);
219 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
220 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
221 "search returned atom");
222 my $p = XML::Feed->parse(\($res->{body}));
223 is($p->format, "Atom", "parsed atom feed");
224 is(scalar $p->entries, 3, "parsed three entries");
228 # redirect list-name-only URLs
230 local $ENV{HOME} = $home;
231 my $res = cgi_run("/test");
232 like($res->{head}, qr/Status: 301 Moved/, "redirected status");
233 like($res->{head}, qr!/test/!, "redirected with slash");
239 my ($env, @args) = @_;
240 my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
241 run(@args, init => $init);
247 QUERY_STRING => $_[1] || "",
248 REQUEST_METHOD => $_[2] || "GET",
249 GATEWAY_INTERFACE => 'CGI/1.1',
250 HTTP_ACCEPT => '*/*',
251 HTTP_HOST => 'test.example.com',
253 my ($in, $out, $err) = ("", "", "");
254 my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
255 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
256 { head => $head, body => $body, rc => $rc, err => $err }