]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_attach.t
msg_iter: workaround broken Email::MIME versions
[public-inbox.git] / t / psgi_attach.t
1 # Copyright (C) 2016 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 File::Temp qw/tempdir/;
8 my $tmpdir = tempdir('psgi-attach-XXXXXX', TMPDIR => 1, CLEANUP => 1);
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::Request Plack::Test URI::Escape);
13 foreach my $mod (@mods) {
14         eval "require $mod";
15         plan skip_all => "$mod missing for plack.t" if $@;
16 }
17 use_ok $_ foreach @mods;
18 use PublicInbox::Import;
19 use PublicInbox::Git;
20 use PublicInbox::Config;
21 use PublicInbox::WWW;
22 use_ok 'PublicInbox::WwwAttach';
23 use Plack::Builder;
24 my $config = PublicInbox::Config->new({
25         "$cfgpfx.address" => $addr,
26         "$cfgpfx.mainrepo" => $maindir,
27 });
28 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
29 my $git = PublicInbox::Git->new($maindir);
30 my $im = PublicInbox::Import->new($git, 'test', $addr);
31
32 {
33         open my $fh, '<', '/dev/urandom' or die "unable to open urandom: $!\n";
34         sysread($fh, my $buf, 8);
35         is(8, length($buf), 'read some random data');
36         my $qp = "abcdef=g\n==blah\n";
37         my $b64 = 'b64'.$buf."\n";
38         my $txt = "plain\ntext\npass\nthrough\n";
39         my $parts = [
40                 Email::MIME->create(
41                         attributes => {
42                                 filename => 'queue-pee',
43                                 content_type => 'text/plain',
44                                 encoding => 'quoted-printable'
45                         },
46                         body => $qp),
47                 Email::MIME->create(
48                         attributes => {
49                                 filename => 'bayce-sixty-four',
50                                 content_type => 'appication/octet-stream',
51                                 encoding => 'base64',
52                         },
53                         body => $b64),
54                 Email::MIME->create(
55                         attributes => {
56                                 filename => 'noop',
57                                 content_type => 'text/plain',
58                         },
59                         body => $txt),
60         ];
61         my $mime = Email::MIME->create(
62                 parts => $parts,
63                 header_str => [ From => 'root@z', 'Message-Id' => '<Z@B>',
64                         Subject => 'hi']
65         );
66         $mime = $mime->as_string;
67         $mime =~ s/\r\n/\n/g; # normalize to LF only
68         $mime = Email::MIME->new($mime);
69         $im->add($mime);
70         $im->done;
71
72         my $www = PublicInbox::WWW->new($config);
73         test_psgi(sub { $www->call(@_) }, sub {
74                 my ($cb) = @_;
75                 my $res;
76
77                 $res = $cb->(GET('/test/Z%40B/1-a.txt'));
78                 my $qp_res = $res->content;
79                 ok(length($qp_res) >= length($qp), 'QP length is close');
80                 like($qp_res, qr/\n\z/s, 'trailing newline exists');
81                 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
82                 $qp_res =~ s/\r\n/\n/g;
83                 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
84
85                 $res = $cb->(GET('/test/Z%40B/2-a.txt'));
86                 is(quotemeta($res->content), quotemeta($b64),
87                         'Base64 matches exactly');
88
89                 $res = $cb->(GET('/test/Z%40B/3-a.txt'));
90                 my $txt_res = $res->content;
91                 ok(length($txt_res) >= length($txt), 'plain text almost matches');
92                 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
93                 is(index($txt_res, $txt), 0, 'plain text not truncated');
94         });
95 }
96 done_testing();