]> Sergey Matveev's repositories - public-inbox.git/blob - t/view.t
6c0859915e89c9ae2d1384d69c2bd35a5c8d5ed0
[public-inbox.git] / t / view.t
1 # Copyright (C) 2013-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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::View;
8
9 sub msg_html ($) {
10         my ($mime) = @_;
11
12         my $s = '';
13         my $body = PublicInbox::View::msg_html(undef, $mime);
14         while (defined(my $buf = $body->getline)) {
15                 $s .= $buf;
16         }
17         $body->close;
18         $s;
19 }
20
21 # plain text
22 {
23         my $body = <<EOF;
24 So and so wrote:
25 > keep this inline
26
27 OK
28
29 > Long and wordy reply goes here and it is split across multiple lines.
30 > We generate links to a separate full page where quoted-text is inline.
31 > This is
32 >
33 > Currently 12 lines
34 > See MAX_INLINE_QUOTED
35 > See MAX_INLINE_QUOTED
36 > See MAX_INLINE_QUOTED
37 > See MAX_INLINE_QUOTED
38 > See MAX_INLINE_QUOTED
39 > See MAX_INLINE_QUOTED
40 > See MAX_INLINE_QUOTED
41 > See MAX_INLINE_QUOTED
42
43 hello world
44 EOF
45         my $s = Email::Simple->create(
46                 header => [
47                         From => 'a@example.com',
48                         To => 'b@example.com',
49                         'Content-Type' => 'text/plain',
50                         'Message-ID' => '<hello@example.com>',
51                         Subject => 'this is a subject',
52                 ],
53                 body => $body,
54         )->as_string;
55         my $mime = Email::MIME->new($s);
56         my $html = msg_html($mime);
57
58         # ghetto tests
59         like($html, qr!<a\nhref="raw"!s, "raw link present");
60         like($html, qr/hello world\b/, "body present");
61         like($html, qr/&gt; keep this inline/, "short quoted text is inline");
62 }
63
64 # multipart crap
65 {
66         my $parts = [
67                 Email::MIME->create(
68                         attributes => { content_type => 'text/plain', },
69                         body => 'hi',
70                 ),
71                 Email::MIME->create(
72                         attributes => { content_type => 'text/plain', },
73                         body => 'bye',
74                 )
75         ];
76         my $mime = Email::MIME->create(
77                 header_str => [
78                         From => 'a@example.com',
79                         Subject => 'blargh',
80                         'Message-ID' => '<blah@example.com>',
81                         'In-Reply-To' => '<irp@example.com>',
82                         ],
83                 parts => $parts,
84         );
85
86         my $html = msg_html($mime);
87         like($html, qr/hi\n.*-- Attachment #2.*\nbye\n/s, "multipart split");
88 }
89
90 # multipart email with attached patch
91 {
92         my $parts = [
93                 Email::MIME->create(
94                         attributes => { content_type => 'text/plain', },
95                         body => 'hi, see attached patch',
96                 ),
97                 Email::MIME->create(
98                         attributes => {
99                                 content_type => 'text/plain',
100                                 filename => "foo.patch",
101                         },
102                         body => "--- a/file\n+++ b/file\n" .
103                                 "@@ -49, 7 +49,34 @@\n",
104                 ),
105         ];
106         my $mime = Email::MIME->create(
107                 header_str => [
108                         From => 'a@example.com',
109                         Subject => '[PATCH] asdf',
110                         'Message-ID' => '<patch@example.com>',
111                         ],
112                 parts => $parts,
113         );
114
115         my $html = msg_html($mime);
116         like($html, qr!.*Attachment #2: foo\.patch --!,
117                 "parts split with filename");
118 }
119
120 # multipart collapsed to single quoted-printable text/plain
121 {
122         my $parts = [
123                 Email::MIME->create(
124                         attributes => {
125                                 content_type => 'text/plain',
126                                 encoding => 'quoted-printable',
127                         },
128                         body => 'hi = bye',
129                 )
130         ];
131         my $mime = Email::MIME->create(
132                 header_str => [
133                         From => 'qp@example.com',
134                         Subject => 'QP',
135                         'Message-ID' => '<qp@example.com>',
136                         ],
137                 parts => $parts,
138         );
139
140         my $orig = $mime->body_raw;
141         my $html = msg_html($mime);
142         like($orig, qr/hi =3D bye=/, "our test used QP correctly");
143         like($html, qr/\bhi = bye\b/, "HTML output decoded QP");
144 }
145
146 {
147         use PublicInbox::MID qw/id_compress/;
148
149         # n.b: this is probably invalid since we dropped CGI for PSGI:
150         like(id_compress('foo%bar@wtf'), qr/\A[a-f0-9]{40}\z/,
151                 "percent always converted to sha1 to workaround buggy httpds");
152
153         is(id_compress('foobar-wtf'), 'foobar-wtf',
154                 'regular ID not compressed');
155 }
156
157 done_testing();