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