2 # Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use IO::Uncompress::Gunzip qw(gunzip);
10 my ($tmpdir, $for_destroy) = tmpdir();
11 require_mods(qw(Plack::Handler::CGI Plack::Util));
12 my $slashy_mid = 'slashy/asdf@example.com';
13 my $ibx = create_inbox 'test', tmpdir => "$tmpdir/test", sub {
15 mkdir "$ibx->{inboxdir}/home", 0755 or BAIL_OUT;
16 mkdir "$ibx->{inboxdir}/home/.public-inbox", 0755 or BAIL_OUT;
17 my $eml = PublicInbox::Eml->new(<<EOF);
18 From: Me <me\@example.com>
19 To: You <you\@example.com>
20 Cc: $ibx->{-primary_address}
21 Message-Id: <blah\@example.com>
23 Date: Thu, 01 Jan 1970 00:00:00 +0000
27 $im->add($eml) or BAIL_OUT;
28 $eml->header_set('Message-ID', '<toobig@example.com>');
29 $eml->body_set("z\n" x 1024);
30 $im->add($eml) or BAIL_OUT;
32 $eml = PublicInbox::Eml->new(<<EOF);
33 From: You <you\@example.com>
34 To: Me <me\@example.com>
35 Cc: $ibx->{-primary_address}
36 In-Reply-To: <blah\@example.com>
37 Message-Id: <blahblah\@example.com>
39 Date: Thu, 01 Jan 1970 00:00:01 +0000
46 $im->add($eml) or BAIL_OUT;
47 $eml = PublicInbox::Eml->new(<<EOF);
48 From: You <you\@example.com>
49 To: Me <me\@example.com>
50 Cc: $ibx->{-primary_address}
51 Message-Id: <$slashy_mid>
53 Date: Thu, 01 Jan 1970 00:00:01 +0000
57 $im->add($eml) or BAIL_OUT;
60 my $home = "$ibx->{inboxdir}/home";
61 open my $cfgfh, '>>', "$home/.public-inbox/config" or BAIL_OUT $!;
62 print $cfgfh <<EOF or BAIL_OUT $!;
64 address = $ibx->{-primary_address}
65 inboxdir = $ibx->{inboxdir}
67 $cfgfh->flush or BAIL_OUT $!;
70 local $ENV{HOME} = $home;
71 my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
72 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
73 "slashy mid raw hit");
76 # retrieve thread as an mbox
78 local $ENV{HOME} = $home;
79 my $path = "/test/blahblah\@example.com/t.mbox.gz";
80 my $res = cgi_run($path);
81 like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
82 my $cmd = ['-index', $ibx->{inboxdir}, '--max-size=2k'];
83 print $cfgfh "\tindexlevel = basic\n" or BAIL_OUT $!;
84 $cfgfh->flush or BAIL_OUT $!;
85 my $opt = { 2 => \(my $err) };
86 my $indexed = run_script($cmd, undef, $opt);
88 $res = cgi_run($path);
89 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
90 my $in = $res->{body};
92 gunzip(\$in => \$out);
93 like($out, qr/^From /m, "From lines in mbox");
94 $res = cgi_run('/test/toobig@example.com/');
95 like($res->{head}, qr/^Status: 300 /,
96 'did not index or return >max-size message');
97 like($err, qr/skipping [a-f0-9]{40,}/,
98 'warned about skipping large OID');
100 like($res->{head}, qr/^Status: 501 /, "search not available");
101 skip('DBD::SQLite not available', 7); # (4 - 1) above, 4 below
103 require_mods('XML::TreePP', 4);
104 $path = "/test/blahblah\@example.com/t.atom";
105 $res = cgi_run($path);
106 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
107 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
108 "search returned atom");
109 my $t = XML::TreePP->new->parse($res->{body});
110 is(scalar @{$t->{feed}->{entry}}, 3, "parsed three entries");
111 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
112 'looks like an an Atom feed');
120 QUERY_STRING => $_[1] || "",
122 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
123 REQUEST_METHOD => $_[2] || "GET",
124 GATEWAY_INTERFACE => 'CGI/1.1',
125 HTTP_ACCEPT => '*/*',
126 HTTP_HOST => 'test.example.com',
128 my ($in, $out, $err) = ("", "", "");
129 my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
130 run_script(['.cgi'], $env, $rdr);
131 fail "unexpected error: \$?=$? ($err)" if $?;
132 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
133 { head => $head, body => $body, err => $err }