]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
remove dependency on IPC::Run
[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 # FIXME: this test is too slow and most non-CGI-requirements
4 # should be moved over to things which use test_psgi
5 use strict;
6 use warnings;
7 use Test::More;
8 use Email::MIME;
9 use File::Temp qw/tempdir/;
10 use Cwd;
11 eval { require IPC::Run };
12 plan skip_all => "missing IPC::Run for t/cgi.t" if $@;
13
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";
23
24 {
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)");
28
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";
32         my %cfg = (
33                 "$cfgpfx.address" => $addr,
34                 "$cfgpfx.mainrepo" => $maindir,
35         );
36         while (my ($k,$v) = each %cfg) {
37                 is(0, system(qw(git config --file), $pi_config, $k, $v),
38                         "setup $k");
39         }
40 }
41
42 use_ok 'PublicInbox::Git';
43 use_ok 'PublicInbox::Import';
44 use_ok 'Email::MIME';
45 my $git = PublicInbox::Git->new($maindir);
46 my $im = PublicInbox::Import->new($git, 'test', $addr);
47
48 {
49         local $ENV{HOME} = $home;
50
51         # ensure successful message delivery
52         {
53                 my $mime = Email::MIME->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                 $im->add($mime);
64                 $im->done;
65                 my $rev = `git --git-dir=$maindir rev-list HEAD`;
66                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
67         }
68
69         # deliver a reply, too
70         {
71                 my $reply = Email::MIME->new(<<EOF);
72 From: You <you\@example.com>
73 To: Me <me\@example.com>
74 Cc: $addr
75 In-Reply-To: <blah\@example.com>
76 Message-Id: <blahblah\@example.com>
77 Subject: Re: hihi
78 Date: Thu, 01 Jan 1970 00:00:01 +0000
79
80 Me wrote:
81 > zzzzzz
82
83 what?
84 EOF
85                 $im->add($reply);
86                 $im->done;
87                 my $rev = `git --git-dir=$maindir rev-list HEAD`;
88                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
89         }
90
91 }
92
93 # obvious failures, first
94 {
95         local $ENV{HOME} = $home;
96         my $res = cgi_run("/", "", "PUT");
97         like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
98
99         $res = cgi_run("/");
100         like($res->{head}, qr/Status:\s*404/i, "index returns 404");
101 }
102
103 # dumb HTTP support
104 {
105         local $ENV{HOME} = $home;
106         my $path = "/test/info/refs";
107         my $res = cgi_run($path);
108         like($res->{head}, qr/Status:\s*200/i, "info/refs readable");
109         my $orig = $res->{body};
110
111         local $ENV{HTTP_RANGE} = 'bytes=5-10';
112         $res = cgi_run($path);
113         like($res->{head}, qr/Status:\s*206/i, "info/refs partial OK");
114         is($res->{body}, substr($orig, 5, 6), 'partial body OK');
115
116         local $ENV{HTTP_RANGE} = 'bytes=5-';
117         $res = cgi_run($path);
118         like($res->{head}, qr/Status:\s*206/i, "info/refs partial past end OK");
119         is($res->{body}, substr($orig, 5), 'partial body OK past end');
120 }
121
122 # atom feeds
123 {
124         local $ENV{HOME} = $home;
125         my $res = cgi_run("/test/atom.xml");
126         like($res->{body}, qr/<title>test for public-inbox/,
127                 "set title in XML feed");
128         like($res->{body},
129                 qr!http://test\.example\.com/test/blah%40example\.com/!,
130                 "link id set");
131         like($res->{body}, qr/what\?/, "reply included");
132 }
133
134 # indices
135 {
136         local $ENV{HOME} = $home;
137         my $res = cgi_run("/test/");
138         like($res->{head}, qr/Status: 200 OK/, "index returns 200");
139
140         my $idx = cgi_run("/test/index.html");
141         $idx->{body} =~ s!/index.html(\?r=)!/$1!g; # dirty...
142         $idx->{body} = [ split(/\n/, $idx->{body}) ];
143         $res->{body} = [ split(/\n/, $res->{body}) ];
144         is_deeply($res, $idx,
145                 '/$INBOX/ and /$INBOX/index.html are nearly identical');
146         # more checks in t/feed.t
147 }
148
149 # message-id pages
150 {
151         local $ENV{HOME} = $home;
152         my $slashy_mid = 'slashy/asdf@example.com';
153         my $reply = Email::MIME->new(<<EOF);
154 From: You <you\@example.com>
155 To: Me <me\@example.com>
156 Cc: $addr
157 Message-Id: <$slashy_mid>
158 Subject: Re: hihi
159 Date: Thu, 01 Jan 1970 00:00:01 +0000
160
161 slashy
162 EOF
163         $im->add($reply);
164         $im->done;
165
166         my $res = cgi_run("/test/slashy%2fasdf%40example.com/raw");
167         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
168                 "slashy mid raw hit");
169
170         $res = cgi_run("/test/blahblah\@example.com/raw");
171         like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
172                 "mid raw hit");
173         $res = cgi_run("/test/blahblah\@example.con/raw");
174         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid raw miss");
175
176         $res = cgi_run("/test/blahblah\@example.com/");
177         like($res->{body}, qr/\A<html>/, "mid html hit");
178         like($res->{head}, qr/Status: 200 OK/, "200 response");
179         $res = cgi_run("/test/blahblah\@example.con/");
180         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
181
182         $res = cgi_run("/test/blahblah\@example.com/f/");
183         like($res->{head}, qr/Status: 301 Moved/, "301 response");
184         like($res->{head},
185                 qr!^Location: http://[^/]+/test/blahblah%40example\.com/\r\n!ms,
186                 '301 redirect location');
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/");
191         like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
192                 "slashy URL generated correctly");
193 }
194
195 # retrieve thread as an mbox
196 {
197         local $ENV{HOME} = $home;
198         my $path = "/test/blahblah%40example.com/t.mbox.gz";
199         my $res = cgi_run($path);
200         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
201         my $indexed = system($index, $maindir) == 0;
202         if ($indexed) {
203                 $res = cgi_run($path);
204                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
205                 eval {
206                         require IO::Uncompress::Gunzip;
207                         my $in = $res->{body};
208                         my $out;
209                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
210                         like($out, qr/^From /m, "From lines in mbox");
211                 };
212         } else {
213                 like($res->{head}, qr/^Status: 501 /, "search not available");
214         }
215
216         my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
217         if ($have_xml_feed) {
218                 $path = "/test/blahblah%40example.com/t.atom";
219                 $res = cgi_run($path);
220                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
221                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
222                         "search returned atom");
223                 my $p = XML::Feed->parse(\($res->{body}));
224                 is($p->format, "Atom", "parsed atom feed");
225                 is(scalar $p->entries, 3, "parsed three entries");
226         }
227 }
228
229 # redirect list-name-only URLs
230 {
231         local $ENV{HOME} = $home;
232         my $res = cgi_run("/test");
233         like($res->{head}, qr/Status: 301 Moved/, "redirected status");
234         like($res->{head}, qr!/test/!, "redirected with slash");
235 }
236
237 done_testing();
238
239 sub run_with_env {
240         my ($env, @args) = @_;
241         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
242         IPC::Run::run(@args, init => $init);
243 }
244
245 sub cgi_run {
246         my %env = (
247                 PATH_INFO => $_[0],
248                 QUERY_STRING => $_[1] || "",
249                 REQUEST_METHOD => $_[2] || "GET",
250                 GATEWAY_INTERFACE => 'CGI/1.1',
251                 HTTP_ACCEPT => '*/*',
252                 HTTP_HOST => 'test.example.com',
253         );
254         my ($in, $out, $err) = ("", "", "");
255         my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
256         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
257         { head => $head, body => $body, rc => $rc, err => $err }
258 }