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 161 162 163 164 165
|
package TestBounceParser;
=encoding utf8
=head1 NAME
TestBounceParser - utility functions for L<Mail::DeliveryStatus::BounceParser>'s unittests
=head1 SYNOPSIS
use TestBounceParser;
check_report('file-in-corpus.msg',
is_bounce => 1,
addresses => [qw(foo@example.com bar@example.com)],
std_reason => 'domain_error',
smtp_code => 550
);
=head1 EXPORTED FUNCTIONS
=cut
use warnings;
use strict;
use Test::More;
use Mail::DeliveryStatus::BounceParser;
use Exporter 'import';
our @EXPORT = qw(readfile check_report);
=head2 readfile
my $content = readfile('path/to_file.msg');
Returns the content of the file passed as argument.
=cut
# FH because we're being backcompat to pre-lexical
sub readfile {
my $fn = shift;
open FH, "$fn" or die $!;
local $/;
my $text = <FH>;
close FH;
return $text;
}
=head2 check_report
my $bounce = check_report($filename, %expectations);
The following keys are supported in C<%expectations>:
=over 4
=item is_bounce
whether the message is expected to be a bounce
=item reports
the number of reports we are expecting
=item addresses
an Arrayref to the list of addresses we are expecting
=item smtp_code
the SMTP code we are expecting (e.g. "554")
=item status
the status code we are expecting (e.g. "5.4.4")
=item reason
the reason string we are expecting (e.g. "550 Host unknown")
=item orig_message_id
the parsed original message id we are expecting
=item todo_is_bounce
when set, the is_bounce check is guarded in a TODO block
=item todo_std_reason
when set, the std_reason check is guarded in a TODO block
=back
=cut
sub check_report {
my ( $filename, %expectations ) = @_;
my $message = readfile($filename);
my $bounce = Mail::DeliveryStatus::BounceParser->new($message);
isa_ok( $bounce, 'Mail::DeliveryStatus::BounceParser' );
my ($report) = $bounce->reports;
TODO: {
local $TODO = $expectations{todo_is_bounce};
is( $bounce->is_bounce,
$expectations{is_bounce},
'is_bounce is correct'
);
}
if ( exists $expectations{reports} ) {
my $cnt = $expectations{reports};
cmp_ok( $bounce->reports, '==', $cnt, "Found $cnt reports" );
}
if ( my $addrs = $expectations{addresses} ) {
my $cnt = $expectations{addresses};
cmp_ok( $bounce->addresses, '==', @$addrs,
'found correct number of addresses' );
is_deeply( [ map {lc} $bounce->addresses ],
$addrs, 'addresses are correct' );
}
TODO: {
local $TODO = $expectations{todo_std_reason};
if ( my $std_reason = $expectations{std_reason} ) {
is( $report->get('std_reason'),
$std_reason, "std reason is $std_reason" );
}
}
if ( my $code = $expectations{smtp_code} ) {
is( $report->get('smtp_code'), $code, 'smtp_code is correct' );
}
if ( my $status = $expectations{status} ) {
is( $report->get('Status'), $status, 'status code is correct' );
}
if ( my $expected_reason = $expectations{reason} ) {
my $reason = $report->get('reason');
$expected_reason =~ s/\s//g;
$reason =~ s/\s//g;
is( $reason, $expected_reason, 'reason is right' );
}
if ( my $omid = $expectations{orig_message_id} ) {
is( $bounce->orig_message_id, $omid,
'the right bounced message id is given' );
}
return $bounce;
}
1;
=head1 AUTHOR
Philipp Gortan <gortan@cpan.org>
=cut
|