]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
t/cgi.t: fix broken test for dumb HTTP
[public-inbox.git] / t / cgi.t
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 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use File::Temp qw/tempdir/;
8 use Cwd;
9 use IPC::Run qw/run/;
10
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(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";
23
24 {
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)");
31
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";
35         my %cfg = (
36                 "$cfgpfx.address" => $addr,
37                 "$cfgpfx.mainrepo" => $maindir,
38         );
39         while (my ($k,$v) = each %cfg) {
40                 is(0, system(qw(git config --file), $pi_config, $k, $v),
41                         "setup $k");
42         }
43 }
44
45 my $failbox = "$home/fail.mbox";
46 local $ENV{PI_EMERGENCY} = $failbox;
47 {
48         local $ENV{HOME} = $home;
49         local $ENV{ORIGINAL_RECIPIENT} = $addr;
50
51         # ensure successful message delivery
52         {
53                 my $simple = Email::Simple->new(<<EOF);
54 From: Me <me\@example.com>
55 To: You <you\@example.com>
56 Cc: $addr
57 Message-Id: <blah\@example.com>
58 Subject: hihi
59 Date: Thu, 01 Jan 1970 00:00:00 +0000
60
61 zzzzzz
62 EOF
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");
68         }
69
70         # deliver a reply, too
71         {
72                 my $reply = Email::Simple->new(<<EOF);
73 From: You <you\@example.com>
74 To: Me <me\@example.com>
75 Cc: $addr
76 In-Reply-To: <blah\@example.com>
77 Message-Id: <blahblah\@example.com>
78 Subject: Re: hihi
79 Date: Thu, 01 Jan 1970 00:00:01 +0000
80
81 Me wrote:
82 > zzzzzz
83
84 what?
85 EOF
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");
91         }
92
93 }
94
95 # obvious failures, first
96 {
97         local $ENV{HOME} = $home;
98         my $res = cgi_run("/", "", "PUT");
99         like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
100
101         $res = cgi_run("/");
102         like($res->{head}, qr/Status:\s*404/i, "index returns 404");
103 }
104
105 # dumb HTTP support
106 {
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};
112
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');
117
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');
122 }
123
124 # atom feeds
125 {
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");
130         like($res->{body},
131                 qr!http://test\.example\.com/test/blah%40example\.com/!,
132                 "link id set");
133         like($res->{body}, qr/what\?/, "reply included");
134 }
135
136 # indices
137 {
138         local $ENV{HOME} = $home;
139         my $res = cgi_run("/test/");
140         like($res->{head}, qr/Status: 200 OK/, "index returns 200");
141
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
149 }
150
151 # message-id pages
152 {
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>
158 Cc: $addr
159 Message-Id: <$slashy_mid>
160 Subject: Re: hihi
161 Date: Thu, 01 Jan 1970 00:00:01 +0000
162
163 slashy
164 EOF
165         my $in = $reply->as_string;
166
167         {
168                 local $ENV{HOME} = $home;
169                 local $ENV{ORIGINAL_RECIPIENT} = $addr;
170                 run_with_env({PATH => $main_path}, [$mda], \$in);
171         }
172         local $ENV{GIT_DIR} = $maindir;
173
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");
177
178         $res = cgi_run("/test/blahblah\@example.com/raw");
179         like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
180                 "mid raw hit");
181         $res = cgi_run("/test/blahblah\@example.con/raw");
182         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid raw miss");
183
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");
189
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");
195
196         $res = cgi_run("/test/");
197         like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
198                 "slashy URL generated correctly");
199 }
200
201 # retrieve thread as an mbox
202 {
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;
209         if ($indexed) {
210                 $res = cgi_run($path);
211                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
212                 eval {
213                         require IO::Uncompress::Gunzip;
214                         my $in = $res->{body};
215                         my $out;
216                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
217                         like($out, qr/^From /m, "From lines in mbox");
218                 };
219         } else {
220                 like($res->{head}, qr/^Status: 501 /, "search not available");
221         }
222
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");
233         }
234 }
235
236 # redirect list-name-only URLs
237 {
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");
242 }
243
244 done_testing();
245
246 sub run_with_env {
247         my ($env, @args) = @_;
248         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
249         run(@args, init => $init);
250 }
251
252 sub cgi_run {
253         my %env = (
254                 PATH_INFO => $_[0],
255                 QUERY_STRING => $_[1] || "",
256                 REQUEST_METHOD => $_[2] || "GET",
257                 GATEWAY_INTERFACE => 'CGI/1.1',
258                 HTTP_ACCEPT => '*/*',
259                 HTTP_HOST => 'test.example.com',
260         );
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 }
265 }