]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_attach.t
remove most internal Email::MIME usage
[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 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);
12 require_mods(@mods);
13 use_ok $_ foreach @mods;
14 use_ok 'PublicInbox::WWW';
15 use PublicInbox::Import;
16 use PublicInbox::Git;
17 use PublicInbox::Config;
18 use_ok 'PublicInbox::WwwAttach';
19 my $config = PublicInbox::Config->new(\<<EOF);
20 $cfgpfx.address=$addr
21 $cfgpfx.inboxdir=$maindir
22 EOF
23 my $git = PublicInbox::Git->new($maindir);
24 my $im = PublicInbox::Import->new($git, 'test', $addr);
25 $im->init_bare;
26
27 {
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(eml_load('t/psgi_attach.eml'));
33         $im->done;
34
35         my $www = PublicInbox::WWW->new($config);
36         test_psgi(sub { $www->call(@_) }, sub {
37                 my ($cb) = @_;
38                 my $res;
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
43                                 4-a.txt)],
44                         \@href, 'attachment links generated');
45
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');
53
54                 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
55                 is(quotemeta($res->content), quotemeta($b64),
56                         'Base64 matches exactly');
57
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');
64
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');
70         });
71 }
72 done_testing();