]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_attach.t
0dde9323dc3943a265284be192abf63022caa48b
[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 my $git = PublicInbox::Git->new($maindir);
25 my $im = PublicInbox::Import->new($git, 'test', $addr);
26 $im->init_bare;
27
28 {
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 {
34         my $parts = [
35                 Email::MIME->create(
36                         attributes => {
37                                 filename => 'queue-pee',
38                                 content_type => 'text/plain',
39                                 encoding => 'quoted-printable'
40                         },
41                         body => $qp),
42                 Email::MIME->create(
43                         attributes => {
44                                 filename => 'bayce-sixty-four',
45                                 content_type => 'appication/octet-stream',
46                                 encoding => 'base64',
47                         },
48                         body => $b64),
49                 Email::MIME->create(
50                         attributes => {
51                                 filename => 'noop.txt',
52                                 content_type => 'text/plain',
53                         },
54                         body => $txt),
55                 Email::MIME->create(
56                         attributes => {
57                                 filename => '.dotfile',
58                                 content_type => 'text/plain',
59                         },
60                         body => $dot),
61         ];
62         Email::MIME->create(
63                 parts => $parts,
64                 header_str => [ From => 'root@z', 'Message-Id' => '<Z@B>',
65                         Subject => 'hi']
66         )}; # mime_load sub
67         $im->add($mime);
68         $im->done;
69
70         my $www = PublicInbox::WWW->new($config);
71         test_psgi(sub { $www->call(@_) }, sub {
72                 my ($cb) = @_;
73                 my $res;
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
78                                 4-a.txt)],
79                         \@href, 'attachment links generated');
80
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');
88
89                 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
90                 is(quotemeta($res->content), quotemeta($b64),
91                         'Base64 matches exactly');
92
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');
99
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');
105         });
106 }
107 done_testing();