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