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