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 149 150 151 152 153 154 155 156 157 158 159 160
|
#!/usr/bin/env perl
#
# Test conversions between Mail::Message and MIME::Entity
#
# MIME::Parser::Filer produces msg-????-1.txt files in the
# test directory :(
#
use strict;
use warnings;
use Mail::Message;
use Mail::Message::Test;
use Test::More;
BEGIN
{ # MIME::Entity requires a VERSION on MailTools modules, but
# MailTools is version-less in my devel environment, hence
# MIME::Entity is "not found" without the next lines.
$Mail::Internet::VERSION ||= '2.21';
$Mail::Field::VERSION ||= '2.21';
$Mail::Head::VERSION ||= '2.21';
$Mail::Header::VERSION ||= '2.21';
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> ]
);
close 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);
$back->purge;
$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!
|