1 # Copyright (C) 2014-2019 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 my ($tmpdir, $for_destroy) = tmpdir();
11 my $home = "$tmpdir/pi-home";
12 my $pi_home = "$home/.public-inbox";
13 my $pi_config = "$pi_home/config";
14 my $maindir = "$tmpdir/main.git";
15 my $addr = 'test-public@example.com';
18 is(1, mkdir($home, 0755), "setup ~/ for testing");
19 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
20 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
22 open my $fh, '>', "$maindir/description" or die "open: $!\n";
23 print $fh "test for public-inbox\n";
24 close $fh or die "close: $!\n";
25 open $fh, '>>', $pi_config or die;
26 print $fh <<EOF or die;
32 close $fh or die "close: $!\n";
35 use_ok 'PublicInbox::Git';
36 use_ok 'PublicInbox::Import';
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;
45 local $ENV{HOME} = $home;
47 # inject some messages:
48 my $mime = Email::MIME->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
60 # deliver a reply, too
61 my $reply = Email::MIME->new(<<EOF);
62 From: You <you\@example.com>
63 To: Me <me\@example.com>
65 In-Reply-To: <blah\@example.com>
66 Message-Id: <blahblah\@example.com>
68 Date: Thu, 01 Jan 1970 00:00:01 +0000
77 my $slashy_mid = 'slashy/asdf@example.com';
78 my $slashy = Email::MIME->new(<<EOF);
79 From: You <you\@example.com>
80 To: Me <me\@example.com>
82 Message-Id: <$slashy_mid>
84 Date: Thu, 01 Jan 1970 00:00:01 +0000
91 my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
92 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
93 "slashy mid raw hit");
96 # retrieve thread as an mbox
98 local $ENV{HOME} = $home;
99 my $path = "/test/blahblah\@example.com/t.mbox.gz";
100 my $res = cgi_run($path);
101 like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
105 require PublicInbox::SearchIdx;
106 my $s = PublicInbox::SearchIdx->new($ibx, 1);
111 $res = cgi_run($path);
112 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
114 require IO::Uncompress::Gunzip;
115 my $in = $res->{body};
117 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
118 like($out, qr/^From /m, "From lines in mbox");
121 like($res->{head}, qr/^Status: 501 /, "search not available");
122 SKIP: { skip 'DBD::SQLite not available', 2 };
125 my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
126 if ($have_xml_feed) {
127 $path = "/test/blahblah\@example.com/t.atom";
128 $res = cgi_run($path);
129 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
130 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
131 "search returned atom");
132 my $p = XML::Feed->parse(\($res->{body}));
133 is($p->format, "Atom", "parsed atom feed");
134 is(scalar $p->entries, 3, "parsed three entries");
136 SKIP: { skip 'DBD::SQLite or XML::Feed missing', 2 };
145 QUERY_STRING => $_[1] || "",
147 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
148 REQUEST_METHOD => $_[2] || "GET",
149 GATEWAY_INTERFACE => 'CGI/1.1',
150 HTTP_ACCEPT => '*/*',
151 HTTP_HOST => 'test.example.com',
153 my ($in, $out, $err) = ("", "", "");
154 my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
155 run_script(['.cgi'], \%env, $rdr);
156 die "unexpected error: \$?=$?" if $?;
157 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
158 { head => $head, body => $body, err => $err }