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',
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 {
77 $res = $cb->(GET('/test/Z%40B/1-a.txt'));
78 my $qp_res = $res->content;
79 ok(length($qp_res) >= length($qp), 'QP length is close');
80 like($qp_res, qr/\n\z/s, 'trailing newline exists');
81 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
82 $qp_res =~ s/\r\n/\n/g;
83 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
85 $res = $cb->(GET('/test/Z%40B/2-a.txt'));
86 is(quotemeta($res->content), quotemeta($b64),
87 'Base64 matches exactly');
89 $res = $cb->(GET('/test/Z%40B/3-a.txt'));
90 my $txt_res = $res->content;
91 ok(length($txt_res) >= length($txt), 'plain text almost matches');
92 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
93 is(index($txt_res, $txt), 0, 'plain text not truncated');