]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_attach.t
No ext_urls
[public-inbox.git] / t / psgi_attach.t
1 #!perl -w
2 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 my @mods = qw(HTTP::Request::Common Plack::Builder Plack::Test URI::Escape);
8 require_mods(@mods);
9 use_ok $_ foreach @mods;
10 use_ok 'PublicInbox::WWW';
11 use PublicInbox::Config;
12 use PublicInbox::Eml;
13 use_ok 'PublicInbox::WwwAttach';
14 my $cfgpath;
15 my $creat_cb = sub {
16         my ($im, $ibx) = @_;
17         $im->add(eml_load('t/psgi_attach.eml')) or BAIL_OUT;
18         $im->add(eml_load('t/data/message_embed.eml')) or BAIL_OUT;
19         $cfgpath = "$ibx->{inboxdir}/pi_config";
20         open my $fh, '>', $cfgpath or BAIL_OUT $!;
21         print $fh <<EOF or BAIL_OUT $!;
22 [publicinbox "test"]
23         address = $ibx->{-primary_address}
24         inboxdir = $ibx->{inboxdir}
25 EOF
26         close $fh or BAIL_OUT $!;
27 };
28 my $ibx = create_inbox 'test', $creat_cb;
29 $cfgpath //= "$ibx->{inboxdir}/pi_config";
30 my $qp = "abcdef=g\n==blah\n";
31 my $b64 = "b64\xde\xad\xbe\xef\n";
32 my $txt = "plain\ntext\npass\nthrough\n";
33 my $dot = "dotfile\n";
34 my $www = PublicInbox::WWW->new(PublicInbox::Config->new($cfgpath));
35 my $client = sub {
36         my ($cb) = @_;
37         my $res;
38         $res = $cb->(GET('/test/Z%40B/'));
39         my @href = ($res->content =~ /^href="([^"]+)"/gms);
40         @href = grep(/\A[\d\.]+-/, @href);
41         is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
42                         4-a.txt)],
43                 \@href, 'attachment links generated');
44
45         $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
46         my $qp_res = $res->content;
47         ok(length($qp_res) >= length($qp), 'QP length is close');
48         like($qp_res, qr/\n\z/s, 'trailing newline exists');
49         # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
50         $qp_res =~ s/\r\n/\n/g;
51         is(index($qp_res, $qp), 0, 'QP trailing newline is there');
52
53         $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
54         is(quotemeta($res->content), quotemeta($b64),
55                 'Base64 matches exactly');
56
57         $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
58         my $txt_res = $res->content;
59         ok(length($txt_res) >= length($txt),
60                 'plain text almost matches');
61         like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
62         is(index($txt_res, $txt), 0, 'plain text not truncated');
63
64         $res = $cb->(GET('/test/Z%40B/4-a.txt'));
65         my $dot_res = $res->content;
66         ok(length($dot_res) >= length($dot), 'dot almost matches');
67         $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
68         is($res->content, $dot_res, 'user-specified filename is OK');
69
70         my $mid = '20200418222508.GA13918@dcvr';
71         my $irt = '20200418222020.GA2745@dcvr';
72         $res = $cb->(GET("/test/$mid/"));
73         unlike($res->content, qr! multipart/mixed, Size: 0 bytes!,
74                 '0-byte download not offered');
75         like($res->content, qr/\bhref="2-embed2x\.eml"/s,
76                 'href to message/rfc822 attachment visible');
77         like($res->content, qr/\bhref="2\.1\.2-test\.eml"/s,
78                 'href to nested message/rfc822 attachment visible');
79
80         $res = $cb->(GET("/test/$mid/2-embed2x.eml"));
81         my $eml = PublicInbox::Eml->new(\($res->content));
82         is_deeply([ $eml->header_raw('Message-ID') ], [ "<$irt>" ],
83                 'got attached eml');
84         my @subs = $eml->subparts;
85         is(scalar(@subs), 2, 'attachment had 2 subparts');
86         like($subs[0]->body_str, qr/^testing embedded message\n*\z/sm,
87                 '1st attachment is as expected');
88         is($subs[1]->header('Content-Type'), 'message/rfc822',
89                 '2nd attachment is as expected');
90
91         $res = $cb->(GET("/test/$mid/2.1.2-test.eml"));
92         $eml = PublicInbox::Eml->new(\($res->content));
93         is_deeply([ $eml->header_raw('Message-ID') ],
94                 [ '<20200418214114.7575-1-e@yhbt.net>' ],
95                 'nested eml retrieved');
96 };
97
98 test_psgi(sub { $www->call(@_) }, $client);
99 SKIP: {
100         require_mods(qw(DBD::SQLite Plack::Test::ExternalServer), 18);
101         $ibx = create_inbox 'test-indexed', indexlevel => 'basic', $creat_cb;
102         $cfgpath = "$ibx->{inboxdir}/pi_config";
103         my $env = { PI_CONFIG => $cfgpath };
104         $www = PublicInbox::WWW->new(PublicInbox::Config->new($cfgpath));
105         test_psgi(sub { $www->call(@_) }, $client);
106         my $sock = tcp_server() or die;
107         my ($tmpdir, $for_destroy) = tmpdir();
108         my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
109         my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
110         my $td = start_script($cmd, $env, { 3 => $sock });
111         my ($h, $p) = tcp_host_port($sock);
112         local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
113         Plack::Test::ExternalServer::test_psgi(client => $client);
114 }
115 done_testing;