]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
9d2d87fefff6bc508646f8debe078e4909dad4f7
[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 use IPC::Run qw/run/;
12
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";
22
23 {
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)");
27
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";
31         my %cfg = (
32                 "$cfgpfx.address" => $addr,
33                 "$cfgpfx.mainrepo" => $maindir,
34         );
35         while (my ($k,$v) = each %cfg) {
36                 is(0, system(qw(git config --file), $pi_config, $k, $v),
37                         "setup $k");
38         }
39 }
40
41 use_ok 'PublicInbox::Git';
42 use_ok 'PublicInbox::Import';
43 use_ok 'Email::MIME';
44 my $git = PublicInbox::Git->new($maindir);
45 my $im = PublicInbox::Import->new($git, 'test', $addr);
46
47 {
48         local $ENV{HOME} = $home;
49
50         # ensure successful message delivery
51         {
52                 my $mime = Email::MIME->new(<<EOF);
53 From: Me <me\@example.com>
54 To: You <you\@example.com>
55 Cc: $addr
56 Message-Id: <blah\@example.com>
57 Subject: hihi
58 Date: Thu, 01 Jan 1970 00:00:00 +0000
59
60 zzzzzz
61 EOF
62                 $im->add($mime);
63                 $im->done;
64                 my $rev = `git --git-dir=$maindir rev-list HEAD`;
65                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
66         }
67
68         # deliver a reply, too
69         {
70                 my $reply = Email::MIME->new(<<EOF);
71 From: You <you\@example.com>
72 To: Me <me\@example.com>
73 Cc: $addr
74 In-Reply-To: <blah\@example.com>
75 Message-Id: <blahblah\@example.com>
76 Subject: Re: hihi
77 Date: Thu, 01 Jan 1970 00:00:01 +0000
78
79 Me wrote:
80 > zzzzzz
81
82 what?
83 EOF
84                 $im->add($reply);
85                 $im->done;
86                 my $rev = `git --git-dir=$maindir rev-list HEAD`;
87                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
88         }
89
90 }
91
92 # obvious failures, first
93 {
94         local $ENV{HOME} = $home;
95         my $res = cgi_run("/", "", "PUT");
96         like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
97
98         $res = cgi_run("/");
99         like($res->{head}, qr/Status:\s*404/i, "index returns 404");
100 }
101
102 # dumb HTTP support
103 {
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};
109
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');
114
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');
119 }
120
121 # atom feeds
122 {
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");
127         like($res->{body},
128                 qr!http://test\.example\.com/test/blah%40example\.com/!,
129                 "link id set");
130         like($res->{body}, qr/what\?/, "reply included");
131 }
132
133 # indices
134 {
135         local $ENV{HOME} = $home;
136         my $res = cgi_run("/test/");
137         like($res->{head}, qr/Status: 200 OK/, "index returns 200");
138
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
146 }
147
148 # message-id pages
149 {
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>
155 Cc: $addr
156 Message-Id: <$slashy_mid>
157 Subject: Re: hihi
158 Date: Thu, 01 Jan 1970 00:00:01 +0000
159
160 slashy
161 EOF
162         $im->add($reply);
163         $im->done;
164
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");
168
169         $res = cgi_run("/test/blahblah\@example.com/raw");
170         like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
171                 "mid raw hit");
172         $res = cgi_run("/test/blahblah\@example.con/raw");
173         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid raw miss");
174
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");
180
181         $res = cgi_run("/test/blahblah\@example.com/f/");
182         like($res->{head}, qr/Status: 301 Moved/, "301 response");
183         like($res->{head},
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");
188
189         $res = cgi_run("/test/");
190         like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
191                 "slashy URL generated correctly");
192 }
193
194 # retrieve thread as an mbox
195 {
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;
201         if ($indexed) {
202                 $res = cgi_run($path);
203                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
204                 eval {
205                         require IO::Uncompress::Gunzip;
206                         my $in = $res->{body};
207                         my $out;
208                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
209                         like($out, qr/^From /m, "From lines in mbox");
210                 };
211         } else {
212                 like($res->{head}, qr/^Status: 501 /, "search not available");
213         }
214
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");
225         }
226 }
227
228 # redirect list-name-only URLs
229 {
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");
234 }
235
236 done_testing();
237
238 sub run_with_env {
239         my ($env, @args) = @_;
240         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
241         run(@args, init => $init);
242 }
243
244 sub cgi_run {
245         my %env = (
246                 PATH_INFO => $_[0],
247                 QUERY_STRING => $_[1] || "",
248                 REQUEST_METHOD => $_[2] || "GET",
249                 GATEWAY_INTERFACE => 'CGI/1.1',
250                 HTTP_ACCEPT => '*/*',
251                 HTTP_HOST => 'test.example.com',
252         );
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 }
257 }