1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 my ($tmpdir, $for_destroy) = tmpdir();
8 my $inboxdir = "$tmpdir/main.git";
9 my $addr = 'test-public@example.com';
10 my @mods = qw(HTTP::Request::Common Plack::Builder Plack::Test URI::Escape);
12 use_ok $_ foreach @mods;
13 use_ok 'PublicInbox::WWW';
14 use PublicInbox::Import;
16 use PublicInbox::Config;
18 use_ok 'PublicInbox::WwwAttach';
20 my $cfgpath = "$tmpdir/config";
21 open my $fh, '>', $cfgpath or BAIL_OUT $!;
22 print $fh <<EOF or BAIL_OUT $!;
27 close $fh or BAIL_OUT $!;
28 my $config = PublicInbox::Config->new($cfgpath);
29 my $git = PublicInbox::Git->new($inboxdir);
30 my $im = PublicInbox::Import->new($git, 'test', $addr);
33 my $qp = "abcdef=g\n==blah\n";
34 my $b64 = "b64\xde\xad\xbe\xef\n";
35 my $txt = "plain\ntext\npass\nthrough\n";
36 my $dot = "dotfile\n";
37 $im->add(eml_load('t/psgi_attach.eml'));
38 $im->add(eml_load('t/data/message_embed.eml'));
41 my $www = PublicInbox::WWW->new($config);
45 $res = $cb->(GET('/test/Z%40B/'));
46 my @href = ($res->content =~ /^href="([^"]+)"/gms);
47 @href = grep(/\A[\d\.]+-/, @href);
48 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
50 \@href, 'attachment links generated');
52 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
53 my $qp_res = $res->content;
54 ok(length($qp_res) >= length($qp), 'QP length is close');
55 like($qp_res, qr/\n\z/s, 'trailing newline exists');
56 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
57 $qp_res =~ s/\r\n/\n/g;
58 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
60 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
61 is(quotemeta($res->content), quotemeta($b64),
62 'Base64 matches exactly');
64 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
65 my $txt_res = $res->content;
66 ok(length($txt_res) >= length($txt),
67 'plain text almost matches');
68 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
69 is(index($txt_res, $txt), 0, 'plain text not truncated');
71 $res = $cb->(GET('/test/Z%40B/4-a.txt'));
72 my $dot_res = $res->content;
73 ok(length($dot_res) >= length($dot), 'dot almost matches');
74 $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
75 is($res->content, $dot_res, 'user-specified filename is OK');
77 my $mid = '20200418222508.GA13918@dcvr';
78 my $irt = '20200418222020.GA2745@dcvr';
79 $res = $cb->(GET("/test/$mid/"));
80 unlike($res->content, qr! multipart/mixed, Size: 0 bytes!,
81 '0-byte download not offered');
82 like($res->content, qr/\bhref="2-embed2x\.eml"/s,
83 'href to message/rfc822 attachment visible');
84 like($res->content, qr/\bhref="2\.1\.2-test\.eml"/s,
85 'href to nested message/rfc822 attachment visible');
87 $res = $cb->(GET("/test/$mid/2-embed2x.eml"));
88 my $eml = PublicInbox::Eml->new(\($res->content));
89 is_deeply([ $eml->header_raw('Message-ID') ], [ "<$irt>" ],
91 my @subs = $eml->subparts;
92 is(scalar(@subs), 2, 'attachment had 2 subparts');
93 like($subs[0]->body_str, qr/^testing embedded message\n*\z/sm,
94 '1st attachment is as expected');
95 is($subs[1]->header('Content-Type'), 'message/rfc822',
96 '2nd attachment is as expected');
98 $res = $cb->(GET("/test/$mid/2.1.2-test.eml"));
99 $eml = PublicInbox::Eml->new(\($res->content));
100 is_deeply([ $eml->header_raw('Message-ID') ],
101 [ '<20200418214114.7575-1-e@yhbt.net>' ],
102 'nested eml retrieved');
105 test_psgi(sub { $www->call(@_) }, $client);
107 diag 'testing with index indexed';
108 require_mods('DBD::SQLite', 19);
109 my $env = { PI_CONFIG => $cfgpath };
110 ok(run_script(['-index', $inboxdir], $env), 'indexed');
112 test_psgi(sub { $www->call(@_) }, $client);
114 require_mods(qw(Plack::Test::ExternalServer), 18);
115 my $sock = tcp_server() or die;
116 my ($out, $err) = map { "$inboxdir/std$_.log" } qw(out err);
117 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
118 my $td = start_script($cmd, $env, { 3 => $sock });
119 my ($h, $p) = ($sock->sockhost, $sock->sockport);
120 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
121 Plack::Test::ExternalServer::test_psgi(client => $client);