]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
t/cgi.t: move dumb HTTP git clone/fetch tests to plack.t
[public-inbox.git] / t / cgi.t
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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         # inject some messages:
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
64         # deliver a reply, too
65         my $reply = Email::MIME->new(<<EOF);
66 From: You <you\@example.com>
67 To: Me <me\@example.com>
68 Cc: $addr
69 In-Reply-To: <blah\@example.com>
70 Message-Id: <blahblah\@example.com>
71 Subject: Re: hihi
72 Date: Thu, 01 Jan 1970 00:00:01 +0000
73
74 Me wrote:
75 > zzzzzz
76
77 what?
78 EOF
79         $im->add($reply);
80         $im->done;
81 }
82
83 # obvious failures, first
84 {
85         local $ENV{HOME} = $home;
86         my $res = cgi_run("/", "", "PUT");
87         like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
88
89         $res = cgi_run("/");
90         like($res->{head}, qr/Status:\s*404/i, "index returns 404");
91 }
92
93 # message-id pages
94 {
95         local $ENV{HOME} = $home;
96         my $slashy_mid = 'slashy/asdf@example.com';
97         my $reply = Email::MIME->new(<<EOF);
98 From: You <you\@example.com>
99 To: Me <me\@example.com>
100 Cc: $addr
101 Message-Id: <$slashy_mid>
102 Subject: Re: hihi
103 Date: Thu, 01 Jan 1970 00:00:01 +0000
104
105 slashy
106 EOF
107         $im->add($reply);
108         $im->done;
109
110         my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
111         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
112                 "slashy mid raw hit");
113
114         $res = cgi_run("/test/blahblah\@example.com/raw");
115         like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
116                 "mid raw hit");
117
118         $res = cgi_run("/test/blahblah\@example.com/");
119         like($res->{body}, qr/\A<html>/, "mid html hit");
120         like($res->{head}, qr/Status: 200 OK/, "200 response");
121
122         $res = cgi_run("/test/blahblah\@example.com/f/");
123         like($res->{head}, qr/Status: 301 Moved/, "301 response");
124         like($res->{head},
125                 qr!^Location: http://[^/]+/test/blahblah\@example\.com/\r\n!ms,
126                 '301 redirect location');
127
128         $res = cgi_run("/test/new.html");
129         like($res->{body}, qr/slashy%2Fasdf\@example\.com/,
130                 "slashy URL generated correctly");
131 }
132
133 # retrieve thread as an mbox
134 {
135         local $ENV{HOME} = $home;
136         my $path = "/test/blahblah\@example.com/t.mbox.gz";
137         my $res = cgi_run($path);
138         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
139         my $indexed = system($index, $maindir) == 0;
140         if ($indexed) {
141                 $res = cgi_run($path);
142                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
143                 eval {
144                         require IO::Uncompress::Gunzip;
145                         my $in = $res->{body};
146                         my $out;
147                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
148                         like($out, qr/^From /m, "From lines in mbox");
149                 };
150         } else {
151                 like($res->{head}, qr/^Status: 501 /, "search not available");
152         }
153
154         my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
155         if ($have_xml_feed) {
156                 $path = "/test/blahblah\@example.com/t.atom";
157                 $res = cgi_run($path);
158                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
159                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
160                         "search returned atom");
161                 my $p = XML::Feed->parse(\($res->{body}));
162                 is($p->format, "Atom", "parsed atom feed");
163                 is(scalar $p->entries, 3, "parsed three entries");
164         }
165 }
166
167 done_testing();
168
169 sub run_with_env {
170         my ($env, @args) = @_;
171         IPC::Run::run(@args, init => sub { %ENV = (%ENV, %$env) });
172 }
173
174 sub cgi_run {
175         my %env = (
176                 PATH_INFO => $_[0],
177                 QUERY_STRING => $_[1] || "",
178                 SCRIPT_NAME => '',
179                 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
180                 REQUEST_METHOD => $_[2] || "GET",
181                 GATEWAY_INTERFACE => 'CGI/1.1',
182                 HTTP_ACCEPT => '*/*',
183                 HTTP_HOST => 'test.example.com',
184         );
185         my ($in, $out, $err) = ("", "", "");
186         my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
187         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
188         { head => $head, body => $body, rc => $rc, err => $err }
189 }