1 # Copyright (C) 2014-2020 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
9 use PublicInbox::TestCommon;
10 use PublicInbox::Import;
11 require_mods(qw(Plack::Handler::CGI Plack::Util));
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $home = "$tmpdir/pi-home";
14 my $pi_home = "$home/.public-inbox";
15 my $pi_config = "$pi_home/config";
16 my $maindir = "$tmpdir/main.git";
17 my $addr = 'test-public@example.com';
20 is(1, mkdir($home, 0755), "setup ~/ for testing");
21 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
22 PublicInbox::Import::init_bare($maindir);
24 open my $fh, '>', "$maindir/description" or die "open: $!\n";
25 print $fh "test for public-inbox\n";
26 close $fh or die "close: $!\n";
27 open $fh, '>>', $pi_config or die;
28 print $fh <<EOF or die;
34 close $fh or die "close: $!\n";
37 use_ok 'PublicInbox::Inbox';
38 use_ok 'PublicInbox::InboxWritable';
39 use_ok 'PublicInbox::Config';
40 my $cfg = PublicInbox::Config->new($pi_config);
41 my $ibx = $cfg->lookup_name('test');
42 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
45 local $ENV{HOME} = $home;
47 # inject some messages:
48 my $mime = PublicInbox::Eml->new(<<EOF);
49 From: Me <me\@example.com>
50 To: You <you\@example.com>
52 Message-Id: <blah\@example.com>
54 Date: Thu, 01 Jan 1970 00:00:00 +0000
58 ok($im->add($mime), 'added initial message');
60 $mime->header_set('Message-ID', '<toobig@example.com>');
61 $mime->body_str_set("z\n" x 1024);
62 ok($im->add($mime), 'added big message');
64 # deliver a reply, too
65 $mime = PublicInbox::Eml->new(<<EOF);
66 From: You <you\@example.com>
67 To: Me <me\@example.com>
69 In-Reply-To: <blah\@example.com>
70 Message-Id: <blahblah\@example.com>
72 Date: Thu, 01 Jan 1970 00:00:01 +0000
79 ok($im->add($mime), 'added reply');
81 my $slashy_mid = 'slashy/asdf@example.com';
82 my $slashy = PublicInbox::Eml->new(<<EOF);
83 From: You <you\@example.com>
84 To: Me <me\@example.com>
86 Message-Id: <$slashy_mid>
88 Date: Thu, 01 Jan 1970 00:00:01 +0000
92 ok($im->add($slashy), 'added slash');
95 my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
96 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
97 "slashy mid raw hit");
100 # retrieve thread as an mbox
102 local $ENV{HOME} = $home;
103 my $path = "/test/blahblah\@example.com/t.mbox.gz";
104 my $res = cgi_run($path);
105 like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
106 my $cmd = ['-index', $ibx->{inboxdir}, '--max-size=2k'];
107 my $opt = { 2 => \(my $err) };
108 my $indexed = run_script($cmd, undef, $opt);
110 $res = cgi_run($path);
111 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
113 require IO::Uncompress::Gunzip;
114 my $in = $res->{body};
116 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
117 like($out, qr/^From /m, "From lines in mbox");
119 $res = cgi_run('/test/toobig@example.com/');
120 like($res->{head}, qr/^Status: 300 /,
121 'did not index or return >max-size message');
122 like($err, qr/skipping [a-f0-9]{40,}/,
123 'warned about skipping large OID');
125 like($res->{head}, qr/^Status: 501 /, "search not available");
126 SKIP: { skip 'DBD::SQLite not available', 4 };
129 my $have_xml_treepp = eval { require XML::TreePP; 1 } if $indexed;
130 if ($have_xml_treepp) {
131 $path = "/test/blahblah\@example.com/t.atom";
132 $res = cgi_run($path);
133 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
134 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
135 "search returned atom");
136 my $t = XML::TreePP->new->parse($res->{body});
137 is(scalar @{$t->{feed}->{entry}}, 3, "parsed three entries");
138 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
139 'looks like an an Atom feed');
141 SKIP: { skip 'DBD::SQLite or XML::TreePP missing', 2 };
150 QUERY_STRING => $_[1] || "",
152 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
153 REQUEST_METHOD => $_[2] || "GET",
154 GATEWAY_INTERFACE => 'CGI/1.1',
155 HTTP_ACCEPT => '*/*',
156 HTTP_HOST => 'test.example.com',
158 my ($in, $out, $err) = ("", "", "");
159 my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
160 run_script(['.cgi'], \%env, $rdr);
161 die "unexpected error: \$?=$?" if $?;
162 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
163 { head => $head, body => $body, err => $err }