File: 20mimeent.t

package info (click to toggle)
libmail-box-perl 2.068-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,576 kB
  • ctags: 1,493
  • sloc: perl: 22,358; makefile: 49
file content (148 lines) | stat: -rw-r--r-- 3,195 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/perl -T
#
# Test conversions between Mail::Message and MIME::Entity
#

use strict;
use warnings;

use lib qw(. .. tests);
use Tools;

use Test::More;

use Mail::Message;

BEGIN
{   eval {require MIME::Entity};
    if($@)
    {   plan skip_all => "requires MIME::Entity.";
        exit 0;
    }

    require Mail::Message::Convert::MimeEntity;
    plan tests => 28;
}

my $me   = MIME::Entity->build
 ( From          => 'mailtools@overmeer.net'
 , To            => 'the users'
 , Subject       => 'use Mail::Box'
 , 'In-Reply-To' => '<023984hjlur29420@sruoiu.nl>'
 , 'X-Again'     => 'repeating header'
 , 'X-Again'     => 'repeating header again'
 , 'X-Again'     => 'repeating header and again'
 , Data          => [ <DATA> ]
 );

ok($me);

my $convert = Mail::Message::Convert::MimeEntity->new;
ok($convert);

#
# Convert MIME::Entity to Mail::Message
#

my $msg = $convert->from($me);
ok($msg);

my $head = $msg->head;
ok($head);

# MIME::Entity makes a mess on the headers: not usefull to test the
# order of the returned.

my @from  = $head->get('From');
cmp_ok(@from, "==", 1);

my @again = $head->get('X-again');
#  cmp_ok(@again, "==", 3);   # Should be 3, but bug in MIME::Entity
cmp_ok(@again, "==", 1);      # Wrong, but to check improvements in ME

my $body  = $msg->body;
ok($body);

my @lines = $body->lines;
cmp_ok(@lines, "==", 6);
is($lines[-1], "use it anymore!\n");

#
# Convert message back to a MIME::Entity
#

my $back = $convert->export($msg);
ok(defined $back);
$head    = $back->head;

is($head->get('to'), "the users\n");

@from    = $head->get('from');
cmp_ok(@from, "==", 1);

@again   = $head->get('x-again');
cmp_ok(@again, "==", 1);

$body = $back->bodyhandle;
ok($body);

@lines = $body->as_lines;
cmp_ok(@lines, "==", 6);

$me->purge;

#
# and now: MULTIPARTS!  Convert MIME::Entity to Mail::Message
#

$me = MIME::Entity->build
 ( From => 'me', To => 'you', Type => 'multipart/mixed'
 , Subject => 'Test mp conv'
 , Data => [ "Some\n", "Lines\n" ]
 );
$me->preamble( [ "Pre1\n", "Pre2\n" ]);
$me->attach(Data => [ "First part\n" ] );
$me->attach(Data => [ "Second part\n" ] );
$me->epilogue( [ "Epi1\n", "Epi2\n" ]);

$msg = $convert->from($me);
ok(defined $msg);
ok($msg->isMultipart);

my @parts = $msg->parts;
cmp_ok(@parts, "==", 2);
isa_ok($msg, 'Mail::Message');
isa_ok($parts[0], 'Mail::Message::Part');
isa_ok($parts[1], 'Mail::Message::Part');

$body = $msg->body;
cmp_ok($body->preamble->nrLines, "==", 2);
cmp_ok($body->epilogue->nrLines, "==", 2);
#$msg->print(\*STDERR);

$me->purge;

#
# Convert MULTIPART message back to a MIME::Entity
#

$me = $convert->export($msg);
#$me->print;
isa_ok($me, 'MIME::Entity');
ok($me->is_multipart);
@parts = $me->parts;
cmp_ok(@parts, "==", 2);
isa_ok($parts[0], 'MIME::Entity');
isa_ok($parts[1], 'MIME::Entity');

$me->purge;

1;

__DATA__
MIME::Entity is written by Eriq, and extends Mail::Internet with many
new capabilities, like multipart bodies.  Actually, although it says
to extend, it more or less reimplements most methods and conflicts
with the other.  Even the Mail::Internet constructor does not work:
only the build() can be used to safely construct a message.  Do not
use it anymore!