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>
6 use PublicInbox::TestCommon;
7 my ($tmpdir, $for_destroy) = tmpdir();
8 my $maindir = "$tmpdir/main.git";
9 my $addr = 'test-public@example.com';
10 my $cfgpfx = "publicinbox.test";
11 my @mods = qw(HTTP::Request::Common Plack::Builder Plack::Test URI::Escape);
13 use_ok $_ foreach @mods;
14 use_ok 'PublicInbox::WWW';
15 use PublicInbox::Import;
17 use PublicInbox::Config;
18 use_ok 'PublicInbox::WwwAttach';
19 my $config = PublicInbox::Config->new(\<<EOF);
21 $cfgpfx.inboxdir=$maindir
23 my $git = PublicInbox::Git->new($maindir);
24 my $im = PublicInbox::Import->new($git, 'test', $addr);
28 my $qp = "abcdef=g\n==blah\n";
29 my $b64 = "b64\xde\xad\xbe\xef\n";
30 my $txt = "plain\ntext\npass\nthrough\n";
31 my $dot = "dotfile\n";
32 $im->add(mime_load('t/psgi_attach.eml'));
35 my $www = PublicInbox::WWW->new($config);
36 test_psgi(sub { $www->call(@_) }, sub {
39 $res = $cb->(GET('/test/Z%40B/'));
40 my @href = ($res->content =~ /^href="([^"]+)"/gms);
41 @href = grep(/\A[\d\.]+-/, @href);
42 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
44 \@href, 'attachment links generated');
46 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
47 my $qp_res = $res->content;
48 ok(length($qp_res) >= length($qp), 'QP length is close');
49 like($qp_res, qr/\n\z/s, 'trailing newline exists');
50 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
51 $qp_res =~ s/\r\n/\n/g;
52 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
54 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
55 is(quotemeta($res->content), quotemeta($b64),
56 'Base64 matches exactly');
58 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
59 my $txt_res = $res->content;
60 ok(length($txt_res) >= length($txt),
61 'plain text almost matches');
62 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
63 is(index($txt_res, $txt), 0, 'plain text not truncated');
65 $res = $cb->(GET('/test/Z%40B/4-a.txt'));
66 my $dot_res = $res->content;
67 ok(length($dot_res) >= length($dot), 'dot almost matches');
68 $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
69 is($res->content, $dot_res, 'user-specified filename is OK');