1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
8 my ($tmpdir, $for_destroy) = tmpdir();
9 my $maindir = "$tmpdir/main.git";
10 my $addr = 'test-public@example.com';
11 my $cfgpfx = "publicinbox.test";
12 my @mods = qw(HTTP::Request::Common Plack::Builder Plack::Test URI::Escape);
14 use_ok $_ foreach @mods;
15 use_ok 'PublicInbox::WWW';
16 use PublicInbox::Import;
18 use PublicInbox::Config;
19 use_ok 'PublicInbox::WwwAttach';
20 my $config = PublicInbox::Config->new(\<<EOF);
22 $cfgpfx.inboxdir=$maindir
24 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
25 my $git = PublicInbox::Git->new($maindir);
26 my $im = PublicInbox::Import->new($git, 'test', $addr);
29 open my $fh, '<', '/dev/urandom' or die "unable to open urandom: $!\n";
30 sysread($fh, my $buf, 8);
31 is(8, length($buf), 'read some random data');
32 my $qp = "abcdef=g\n==blah\n";
33 my $b64 = 'b64'.$buf."\n";
34 my $txt = "plain\ntext\npass\nthrough\n";
35 my $dot = "dotfile\n";
39 filename => 'queue-pee',
40 content_type => 'text/plain',
41 encoding => 'quoted-printable'
46 filename => 'bayce-sixty-four',
47 content_type => 'appication/octet-stream',
53 filename => 'noop.txt',
54 content_type => 'text/plain',
59 filename => '.dotfile',
60 content_type => 'text/plain',
64 my $mime = Email::MIME->create(
66 header_str => [ From => 'root@z', 'Message-Id' => '<Z@B>',
69 $mime = $mime->as_string;
70 $mime =~ s/\r\n/\n/g; # normalize to LF only
71 $mime = Email::MIME->new($mime);
75 my $www = PublicInbox::WWW->new($config);
76 test_psgi(sub { $www->call(@_) }, sub {
79 $res = $cb->(GET('/test/Z%40B/'));
80 my @href = ($res->content =~ /^href="([^"]+)"/gms);
81 @href = grep(/\A[\d\.]+-/, @href);
82 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
84 \@href, 'attachment links generated');
86 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
87 my $qp_res = $res->content;
88 ok(length($qp_res) >= length($qp), 'QP length is close');
89 like($qp_res, qr/\n\z/s, 'trailing newline exists');
90 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
91 $qp_res =~ s/\r\n/\n/g;
92 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
94 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
95 is(quotemeta($res->content), quotemeta($b64),
96 'Base64 matches exactly');
98 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
99 my $txt_res = $res->content;
100 ok(length($txt_res) >= length($txt),
101 'plain text almost matches');
102 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
103 is(index($txt_res, $txt), 0, 'plain text not truncated');
105 $res = $cb->(GET('/test/Z%40B/4-a.txt'));
106 my $dot_res = $res->content;
107 ok(length($dot_res) >= length($dot), 'dot almost matches');
108 $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
109 is($res->content, $dot_res, 'user-specified filename is OK');