1 # Copyright (C) 2016-2020 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 my $git = PublicInbox::Git->new($maindir);
25 my $im = PublicInbox::Import->new($git, 'test', $addr);
29 my $qp = "abcdef=g\n==blah\n";
30 my $b64 = "b64\xde\xad\xbe\xef\n";
31 my $txt = "plain\ntext\npass\nthrough\n";
32 my $dot = "dotfile\n";
33 my $mime = mime_load 't/psgi_attach.eml', sub {
37 filename => 'queue-pee',
38 content_type => 'text/plain',
39 encoding => 'quoted-printable'
44 filename => 'bayce-sixty-four',
45 content_type => 'appication/octet-stream',
51 filename => 'noop.txt',
52 content_type => 'text/plain',
57 filename => '.dotfile',
58 content_type => 'text/plain',
64 header_str => [ From => 'root@z', 'Message-Id' => '<Z@B>',
70 my $www = PublicInbox::WWW->new($config);
71 test_psgi(sub { $www->call(@_) }, sub {
74 $res = $cb->(GET('/test/Z%40B/'));
75 my @href = ($res->content =~ /^href="([^"]+)"/gms);
76 @href = grep(/\A[\d\.]+-/, @href);
77 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
79 \@href, 'attachment links generated');
81 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
82 my $qp_res = $res->content;
83 ok(length($qp_res) >= length($qp), 'QP length is close');
84 like($qp_res, qr/\n\z/s, 'trailing newline exists');
85 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
86 $qp_res =~ s/\r\n/\n/g;
87 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
89 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
90 is(quotemeta($res->content), quotemeta($b64),
91 'Base64 matches exactly');
93 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
94 my $txt_res = $res->content;
95 ok(length($txt_res) >= length($txt),
96 'plain text almost matches');
97 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
98 is(index($txt_res, $txt), 0, 'plain text not truncated');
100 $res = $cb->(GET('/test/Z%40B/4-a.txt'));
101 my $dot_res = $res->content;
102 ok(length($dot_res) >= length($dot), 'dot almost matches');
103 $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
104 is($res->content, $dot_res, 'user-specified filename is OK');