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