1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use File::Temp qw/tempdir/;
8 my $tmpdir = tempdir('psgi-attach-XXXXXX', TMPDIR => 1, CLEANUP => 1);
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::Request Plack::Test URI::Escape);
13 foreach my $mod (@mods) {
15 plan skip_all => "$mod missing for plack.t" if $@;
17 use_ok $_ foreach @mods;
18 use PublicInbox::Import;
20 use PublicInbox::Config;
22 use_ok 'PublicInbox::WwwAttach';
24 my $config = PublicInbox::Config->new({
25 "$cfgpfx.address" => $addr,
26 "$cfgpfx.mainrepo" => $maindir,
28 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
29 my $git = PublicInbox::Git->new($maindir);
30 my $im = PublicInbox::Import->new($git, 'test', $addr);
33 open my $fh, '<', '/dev/urandom' or die "unable to open urandom: $!\n";
34 sysread($fh, my $buf, 8);
35 is(8, length($buf), 'read some random data');
36 my $qp = "abcdef=g\n==blah\n";
37 my $b64 = 'b64'.$buf."\n";
38 my $txt = "plain\ntext\npass\nthrough\n";
42 filename => 'queue-pee',
43 content_type => 'text/plain',
44 encoding => 'quoted-printable'
49 filename => 'bayce-sixty-four',
50 content_type => 'appication/octet-stream',
56 filename => 'noop.txt',
57 content_type => 'text/plain',
61 my $mime = Email::MIME->create(
63 header_str => [ From => 'root@z', 'Message-Id' => '<Z@B>',
66 $mime = $mime->as_string;
67 $mime =~ s/\r\n/\n/g; # normalize to LF only
68 $mime = Email::MIME->new($mime);
72 my $www = PublicInbox::WWW->new($config);
73 test_psgi(sub { $www->call(@_) }, sub {
76 $res = $cb->(GET('/test/Z%40B/'));
77 my @href = ($res->content =~ /^href="([^"]+)"/gms);
78 @href = grep(/\A[\d\.]+-/, @href);
79 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt)],
80 \@href, 'attachment links generated');
82 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
83 my $qp_res = $res->content;
84 ok(length($qp_res) >= length($qp), 'QP length is close');
85 like($qp_res, qr/\n\z/s, 'trailing newline exists');
86 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
87 $qp_res =~ s/\r\n/\n/g;
88 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
90 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
91 is(quotemeta($res->content), quotemeta($b64),
92 'Base64 matches exactly');
94 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
95 my $txt_res = $res->content;
96 ok(length($txt_res) >= length($txt),
97 'plain text almost matches');
98 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
99 is(index($txt_res, $txt), 0, 'plain text not truncated');