-
Notifications
You must be signed in to change notification settings - Fork 4
/
02_smtp.t
87 lines (73 loc) · 2.41 KB
/
02_smtp.t
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
use strict;
use Test::More;
use Mojo::SMTP::Client;
use Mojo::Exception;
use Socket 'CRLF';
use lib 't/lib';
use Utils;
if ($^O eq 'MSWin32') {
plan skip_all => 'fork() support required';
}
my $tls = (exists &Mojo::IOLoop::Client::TLS ? &Mojo::IOLoop::Client::TLS : &Mojo::IOLoop::TLS::TLS) || 0;
# 1
my ($pid, $sock, $host, $port) = Utils::make_smtp_server($tls);
my $smtp = Mojo::SMTP::Client->new(address => $host, port => $port, tls => $tls);
syswrite($sock, join(CRLF, '220 host.net', '220 hello ok', '220 from ok', '220 to ok', '220 quit ok').CRLF);
my $resp = $smtp->send(hello => 'mymail.host', from => '', to => 'jorik@gmail.com', quit => 1);
isa_ok($resp, 'Mojo::SMTP::Client::Response');
ok(!$resp->error, 'no error') or diag $resp->error;
is($resp->code, 220, 'right response code');
is($resp->message, 'quit ok', 'right message');
is($resp->to_string, '220 quit ok'.CRLF, 'stringify message');
my @expected_cmd = (
'CONNECT',
'EHLO mymail.host',
'MAIL FROM:<>',
'RCPT TO:<jorik@gmail.com>',
'QUIT'
);
for (0..4) {
is(scalar(<$sock>), $expected_cmd[$_].CRLF, 'right cmd was sent');
}
close $sock;
kill 15, $pid;
# 2
($pid, $sock, $host, $port) = Utils::make_smtp_server();
$smtp = Mojo::SMTP::Client->new(address => $host, port => $port, inactivity_timeout => 0.5, autodie => 1);
eval {
$smtp->send(quit => 1);
};
ok(my $e = $@, 'timed out');
isa_ok($e, 'Mojo::SMTP::Client::Exception::Stream');
close $sock;
kill 15, $pid;
# 3
($pid, $sock, $host, $port) = Utils::make_smtp_server();
$smtp = Mojo::SMTP::Client->new(address => $host, port => $port, autodie => 1);
syswrite($sock, '500 host.net is busy'.CRLF);
eval {
$smtp->send();
};
ok(my $e = $@, 'bad response');
isa_ok($e, 'Mojo::SMTP::Client::Exception::Response');
close $sock;
kill 15, $pid;
# 4
($pid, $sock, $host, $port) = Utils::make_smtp_server();
$smtp = Mojo::SMTP::Client->new(address => $host, port => $port, autodie => 1);
syswrite($sock, join(CRLF, '220 host.net', '220 hello ok', '220 from ok', '220 to ok', '220 quit ok').CRLF);
$smtp->on(response => sub {
my $cmd = $_[1];
if ($cmd == Mojo::SMTP::Client::CMD_EHLO) {
Mojo::Exception->throw("Throwed from response callback");
}
});
eval {
$smtp->send(hello => 'mymail.host', from => '', to => 'jorik@gmail.com', quit => 1);
};
my $e = $@;
is(ref $e, 'Mojo::Exception', 'got right exception');
is($e->message, 'Throwed from response callback', 'with right message');
close $sock;
kill 15, $pid;
done_testing;