1 # Copyright (C) 2016-2018 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::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";
39 my $dot = "dotfile\n";
43 filename => 'queue-pee',
44 content_type => 'text/plain',
45 encoding => 'quoted-printable'
50 filename => 'bayce-sixty-four',
51 content_type => 'appication/octet-stream',
57 filename => 'noop.txt',
58 content_type => 'text/plain',
63 filename => '.dotfile',
64 content_type => 'text/plain',
68 my $mime = Email::MIME->create(
70 header_str => [ From => 'root@z', 'Message-Id' => '<Z@B>',
73 $mime = $mime->as_string;
74 $mime =~ s/\r\n/\n/g; # normalize to LF only
75 $mime = Email::MIME->new($mime);
79 my $www = PublicInbox::WWW->new($config);
80 test_psgi(sub { $www->call(@_) }, sub {
83 $res = $cb->(GET('/test/Z%40B/'));
84 my @href = ($res->content =~ /^href="([^"]+)"/gms);
85 @href = grep(/\A[\d\.]+-/, @href);
86 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
88 \@href, 'attachment links generated');
90 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
91 my $qp_res = $res->content;
92 ok(length($qp_res) >= length($qp), 'QP length is close');
93 like($qp_res, qr/\n\z/s, 'trailing newline exists');
94 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
95 $qp_res =~ s/\r\n/\n/g;
96 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
98 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
99 is(quotemeta($res->content), quotemeta($b64),
100 'Base64 matches exactly');
102 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
103 my $txt_res = $res->content;
104 ok(length($txt_res) >= length($txt),
105 'plain text almost matches');
106 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
107 is(index($txt_res, $txt), 0, 'plain text not truncated');
109 $res = $cb->(GET('/test/Z%40B/4-a.txt'));
110 my $dot_res = $res->content;
111 ok(length($dot_res) >= length($dot), 'dot almost matches');
112 $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
113 is($res->content, $dot_res, 'user-specified filename is OK');