]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / cgi.t
1 #!perl -w
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>
4 use strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 use IO::Uncompress::Gunzip qw(gunzip);
8 use PublicInbox::Eml;
9 use IO::Handle;
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 {
14         my ($im, $ibx) = @_;
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>
22 Subject: hihi
23 Date: Thu, 01 Jan 1970 00:00:00 +0000
24
25 zzzzzz
26 EOF
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;
31
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>
38 Subject: Re: hihi
39 Date: Thu, 01 Jan 1970 00:00:01 +0000
40
41 Me wrote:
42 > zzzzzz
43
44 what?
45 EOF
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>
52 Subject: Re: hihi
53 Date: Thu, 01 Jan 1970 00:00:01 +0000
54
55 slashy
56 EOF
57         $im->add($eml) or BAIL_OUT;
58 }; # create_inbox
59
60 my $home = "$ibx->{inboxdir}/home";
61 open my $cfgfh, '>>', "$home/.public-inbox/config" or BAIL_OUT $!;
62 print $cfgfh <<EOF or BAIL_OUT $!;
63 [publicinbox "test"]
64         address = $ibx->{-primary_address}
65         inboxdir = $ibx->{inboxdir}
66 EOF
67 $cfgfh->flush or BAIL_OUT $!;
68
69 {
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");
74 }
75
76 # retrieve thread as an mbox
77 SKIP: {
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);
87         if ($indexed) {
88                 $res = cgi_run($path);
89                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
90                 my $in = $res->{body};
91                 my $out;
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');
99         } else {
100                 like($res->{head}, qr/^Status: 501 /, "search not available");
101                 skip('DBD::SQLite not available', 7); # (4 - 1) above, 4 below
102         }
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');
113 }
114
115 done_testing();
116
117 sub cgi_run {
118         my $env = {
119                 PATH_INFO => $_[0],
120                 QUERY_STRING => $_[1] || "",
121                 SCRIPT_NAME => '',
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',
127         };
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 }
134 }