]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_attach.t
view: remove mhref arg from multipart_text_as_html
[public-inbox.git] / t / psgi_attach.t
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>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
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);
13 require_mods(@mods);
14 use_ok $_ foreach @mods;
15 use_ok 'PublicInbox::WWW';
16 use PublicInbox::Import;
17 use PublicInbox::Git;
18 use PublicInbox::Config;
19 use_ok 'PublicInbox::WwwAttach';
20 my $config = PublicInbox::Config->new(\<<EOF);
21 $cfgpfx.address=$addr
22 $cfgpfx.inboxdir=$maindir
23 EOF
24 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
25 my $git = PublicInbox::Git->new($maindir);
26 my $im = PublicInbox::Import->new($git, 'test', $addr);
27
28 {
29         open my $fh, '<', '/dev/urandom' or die "unable to open urandom: $!\n";
30         sysread($fh, my $buf, 8);
31         is(8, length($buf), 'read some random data');
32         my $qp = "abcdef=g\n==blah\n";
33         my $b64 = 'b64'.$buf."\n";
34         my $txt = "plain\ntext\npass\nthrough\n";
35         my $dot = "dotfile\n";
36         my $parts = [
37                 Email::MIME->create(
38                         attributes => {
39                                 filename => 'queue-pee',
40                                 content_type => 'text/plain',
41                                 encoding => 'quoted-printable'
42                         },
43                         body => $qp),
44                 Email::MIME->create(
45                         attributes => {
46                                 filename => 'bayce-sixty-four',
47                                 content_type => 'appication/octet-stream',
48                                 encoding => 'base64',
49                         },
50                         body => $b64),
51                 Email::MIME->create(
52                         attributes => {
53                                 filename => 'noop.txt',
54                                 content_type => 'text/plain',
55                         },
56                         body => $txt),
57                 Email::MIME->create(
58                         attributes => {
59                                 filename => '.dotfile',
60                                 content_type => 'text/plain',
61                         },
62                         body => $dot),
63         ];
64         my $mime = Email::MIME->create(
65                 parts => $parts,
66                 header_str => [ From => 'root@z', 'Message-Id' => '<Z@B>',
67                         Subject => 'hi']
68         );
69         $mime = $mime->as_string;
70         $mime =~ s/\r\n/\n/g; # normalize to LF only
71         $mime = Email::MIME->new($mime);
72         $im->add($mime);
73         $im->done;
74
75         my $www = PublicInbox::WWW->new($config);
76         test_psgi(sub { $www->call(@_) }, sub {
77                 my ($cb) = @_;
78                 my $res;
79                 $res = $cb->(GET('/test/Z%40B/'));
80                 my @href = ($res->content =~ /^href="([^"]+)"/gms);
81                 @href = grep(/\A[\d\.]+-/, @href);
82                 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
83                                 4-a.txt)],
84                         \@href, 'attachment links generated');
85
86                 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
87                 my $qp_res = $res->content;
88                 ok(length($qp_res) >= length($qp), 'QP length is close');
89                 like($qp_res, qr/\n\z/s, 'trailing newline exists');
90                 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
91                 $qp_res =~ s/\r\n/\n/g;
92                 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
93
94                 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
95                 is(quotemeta($res->content), quotemeta($b64),
96                         'Base64 matches exactly');
97
98                 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
99                 my $txt_res = $res->content;
100                 ok(length($txt_res) >= length($txt),
101                         'plain text almost matches');
102                 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
103                 is(index($txt_res, $txt), 0, 'plain text not truncated');
104
105                 $res = $cb->(GET('/test/Z%40B/4-a.txt'));
106                 my $dot_res = $res->content;
107                 ok(length($dot_res) >= length($dot), 'dot almost matches');
108                 $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
109                 is($res->content, $dot_res, 'user-specified filename is OK');
110         });
111 }
112 done_testing();