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)
7 use File::Temp qw/tempdir/;
11 use constant CGI => "blib/script/public-inbox.cgi";
12 my $mda = "blib/script/public-inbox-mda";
13 my $index = "blib/script/public-inbox-index";
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 $main_bin = getcwd()."/t/main-bin";
20 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
21 my $addr = 'test-public@example.com';
22 my $cfgpfx = "publicinbox.test";
25 ok(-x "$main_bin/spamc",
26 "spamc ham mock found (run in top of source tree");
27 ok(-x $mda, "$mda is executable");
28 is(1, mkdir($home, 0755), "setup ~/ for testing");
29 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
30 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
32 open my $fh, '>', "$maindir/description" or die "open: $!\n";
33 print $fh "test for public-inbox\n";
34 close $fh or die "close: $!\n";
36 "$cfgpfx.address" => $addr,
37 "$cfgpfx.mainrepo" => $maindir,
39 while (my ($k,$v) = each %cfg) {
40 is(0, system(qw(git config --file), $pi_config, $k, $v),
45 my $failbox = "$home/fail.mbox";
46 local $ENV{PI_EMERGENCY} = $failbox;
48 local $ENV{HOME} = $home;
49 local $ENV{ORIGINAL_RECIPIENT} = $addr;
51 # ensure successful message delivery
53 my $simple = Email::Simple->new(<<EOF);
54 From: Me <me\@example.com>
55 To: You <you\@example.com>
57 Message-Id: <blah\@example.com>
59 Date: Thu, 01 Jan 1970 00:00:00 +0000
63 my $in = $simple->as_string;
64 run_with_env({PATH => $main_path}, [$mda], \$in);
65 local $ENV{GIT_DIR} = $maindir;
66 my $rev = `git rev-list HEAD`;
67 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
70 # deliver a reply, too
72 my $reply = Email::Simple->new(<<EOF);
73 From: You <you\@example.com>
74 To: Me <me\@example.com>
76 In-Reply-To: <blah\@example.com>
77 Message-Id: <blahblah\@example.com>
79 Date: Thu, 01 Jan 1970 00:00:01 +0000
86 my $in = $reply->as_string;
87 run_with_env({PATH => $main_path}, [$mda], \$in);
88 local $ENV{GIT_DIR} = $maindir;
89 my $rev = `git rev-list HEAD`;
90 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
95 # obvious failures, first
97 local $ENV{HOME} = $home;
98 my $res = cgi_run("/", "", "PUT");
99 like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
102 like($res->{head}, qr/Status:\s*404/i, "index returns 404");
107 local $ENV{HOME} = $home;
108 my $path = "/test/info/refs";
109 my $res = cgi_run($path);
110 like($res->{head}, qr/Status:\s*200/i, "info/refs readable");
111 my $orig = $res->{body};
113 local $ENV{HTTP_RANGE} = 'bytes=5-10';
114 $res = cgi_run($path);
115 like($res->{head}, qr/Status:\s*206/i, "info/refs partial OK");
116 is($res->{body}, substr($orig, 5, 6), 'partial body OK');
118 local $ENV{HTTP_RANGE} = 'bytes=5-';
119 $res = cgi_run($path);
120 like($res->{head}, qr/Status:\s*206/i, "info/refs partial past end OK");
121 is($res->{body}, substr($orig, 5), 'partial body OK past end');
126 local $ENV{HOME} = $home;
127 my $res = cgi_run("/test/atom.xml");
128 like($res->{body}, qr/<title>test for public-inbox/,
129 "set title in XML feed");
131 qr!http://test\.example\.com/test/blah%40example\.com/!,
133 like($res->{body}, qr/what\?/, "reply included");
138 local $ENV{HOME} = $home;
139 my $res = cgi_run("/test/");
140 like($res->{head}, qr/Status: 200 OK/, "index returns 200");
142 my $idx = cgi_run("/test/index.html");
143 $idx->{body} =~ s!/index.html(\?r=)!/$1!g; # dirty...
144 $idx->{body} = [ split(/\n/, $idx->{body}) ];
145 $res->{body} = [ split(/\n/, $res->{body}) ];
146 is_deeply($res, $idx,
147 '/$LISTNAME/ and /$LISTNAME/index.html are nearly identical');
148 # more checks in t/feed.t
153 local $ENV{HOME} = $home;
154 my $slashy_mid = 'slashy/asdf@example.com';
155 my $reply = Email::Simple->new(<<EOF);
156 From: You <you\@example.com>
157 To: Me <me\@example.com>
159 Message-Id: <$slashy_mid>
161 Date: Thu, 01 Jan 1970 00:00:01 +0000
165 my $in = $reply->as_string;
168 local $ENV{HOME} = $home;
169 local $ENV{ORIGINAL_RECIPIENT} = $addr;
170 run_with_env({PATH => $main_path}, [$mda], \$in);
172 local $ENV{GIT_DIR} = $maindir;
174 my $res = cgi_run("/test/slashy%2fasdf%40example.com/raw");
175 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
176 "slashy mid raw hit");
178 $res = cgi_run("/test/blahblah\@example.com/raw");
179 like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
181 $res = cgi_run("/test/blahblah\@example.con/raw");
182 like($res->{head}, qr/Status: 300 Multiple Choices/, "mid raw miss");
184 $res = cgi_run("/test/blahblah\@example.com/");
185 like($res->{body}, qr/\A<html>/, "mid html hit");
186 like($res->{head}, qr/Status: 200 OK/, "200 response");
187 $res = cgi_run("/test/blahblah\@example.con/");
188 like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
190 $res = cgi_run("/test/blahblah\@example.com/f/");
191 like($res->{body}, qr/\A<html>/, "mid html");
192 like($res->{head}, qr/Status: 200 OK/, "200 response");
193 $res = cgi_run("/test/blahblah\@example.con/f/");
194 like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
196 $res = cgi_run("/test/");
197 like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
198 "slashy URL generated correctly");
201 # retrieve thread as an mbox
203 local $ENV{HOME} = $home;
204 local $ENV{PATH} = $main_path;
205 my $path = "/test/blahblah%40example.com/t.mbox.gz";
206 my $res = cgi_run($path);
207 like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
208 my $indexed = system($index, $maindir) == 0;
210 $res = cgi_run($path);
211 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
213 require IO::Uncompress::Gunzip;
214 my $in = $res->{body};
216 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
217 like($out, qr/^From /m, "From lines in mbox");
220 like($res->{head}, qr/^Status: 501 /, "search not available");
223 my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
224 if ($have_xml_feed) {
225 $path = "/test/blahblah%40example.com/t.atom";
226 $res = cgi_run($path);
227 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
228 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
229 "search returned atom");
230 my $p = XML::Feed->parse(\($res->{body}));
231 is($p->format, "Atom", "parsed atom feed");
232 is(scalar $p->entries, 3, "parsed three entries");
236 # redirect list-name-only URLs
238 local $ENV{HOME} = $home;
239 my $res = cgi_run("/test");
240 like($res->{head}, qr/Status: 301 Moved/, "redirected status");
241 like($res->{head}, qr!/test/!, "redirected with slash");
247 my ($env, @args) = @_;
248 my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
249 run(@args, init => $init);
255 QUERY_STRING => $_[1] || "",
256 REQUEST_METHOD => $_[2] || "GET",
257 GATEWAY_INTERFACE => 'CGI/1.1',
258 HTTP_ACCEPT => '*/*',
259 HTTP_HOST => 'test.example.com',
261 my ($in, $out, $err) = ("", "", "");
262 my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
263 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
264 { head => $head, body => $body, rc => $rc, err => $err }