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::Test URI::Escape);
14 use_ok $_ foreach @mods;
15 use PublicInbox::Import;
17 use PublicInbox::Config;
19 use_ok 'PublicInbox::WwwAttach';
21 my $config = PublicInbox::Config->new(\<<EOF);
23 $cfgpfx.inboxdir=$maindir
25 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
26 my $git = PublicInbox::Git->new($maindir);
27 my $im = PublicInbox::Import->new($git, 'test', $addr);
30 open my $fh, '<', '/dev/urandom' or die "unable to open urandom: $!\n";
31 sysread($fh, my $buf, 8);
32 is(8, length($buf), 'read some random data');
33 my $qp = "abcdef=g\n==blah\n";
34 my $b64 = 'b64'.$buf."\n";
35 my $txt = "plain\ntext\npass\nthrough\n";
36 my $dot = "dotfile\n";
40 filename => 'queue-pee',
41 content_type => 'text/plain',
42 encoding => 'quoted-printable'
47 filename => 'bayce-sixty-four',
48 content_type => 'appication/octet-stream',
54 filename => 'noop.txt',
55 content_type => 'text/plain',
60 filename => '.dotfile',
61 content_type => 'text/plain',
65 my $mime = Email::MIME->create(
67 header_str => [ From => 'root@z', 'Message-Id' => '<Z@B>',
70 $mime = $mime->as_string;
71 $mime =~ s/\r\n/\n/g; # normalize to LF only
72 $mime = Email::MIME->new($mime);
76 my $www = PublicInbox::WWW->new($config);
77 test_psgi(sub { $www->call(@_) }, sub {
80 $res = $cb->(GET('/test/Z%40B/'));
81 my @href = ($res->content =~ /^href="([^"]+)"/gms);
82 @href = grep(/\A[\d\.]+-/, @href);
83 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
85 \@href, 'attachment links generated');
87 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
88 my $qp_res = $res->content;
89 ok(length($qp_res) >= length($qp), 'QP length is close');
90 like($qp_res, qr/\n\z/s, 'trailing newline exists');
91 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
92 $qp_res =~ s/\r\n/\n/g;
93 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
95 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
96 is(quotemeta($res->content), quotemeta($b64),
97 'Base64 matches exactly');
99 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
100 my $txt_res = $res->content;
101 ok(length($txt_res) >= length($txt),
102 'plain text almost matches');
103 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
104 is(index($txt_res, $txt), 0, 'plain text not truncated');
106 $res = $cb->(GET('/test/Z%40B/4-a.txt'));
107 my $dot_res = $res->content;
108 ok(length($dot_res) >= length($dot), 'dot almost matches');
109 $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
110 is($res->content, $dot_res, 'user-specified filename is OK');