comparison lib/Test/Nginx/SMTP.pm @ 14:d4b74207a627

Tests: refactor common functions. Let it be something more structured, avoid globals.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 12 Sep 2008 20:50:35 +0400
parents _common.pm@d19146b30334
children 0880e0fafde4
comparison
equal deleted inserted replaced
13:e8edb765595d 14:d4b74207a627
1 package Test::Nginx::SMTP;
2
3 # (C) Maxim Dounin
4
5 # Module for nginx smtp tests.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Test::More qw//;
13 use IO::Socket;
14 use Socket qw/ CRLF /;
15
16 use Test::Nginx;
17
18 use base qw/ IO::Socket::INET /;
19
20 sub new {
21 my $class = shift;
22
23 my $self = return $class->SUPER::new(
24 Proto => "tcp",
25 PeerAddr => "localhost",
26 PeerPort => 10025,
27 @_
28 )
29 or die "Can't connect to nginx: $!\n";
30
31 $self->autoflush(1);
32
33 return $self;
34 }
35
36 sub send {
37 my ($self, $cmd) = @_;
38 log_out($cmd);
39 $self->print($cmd . CRLF);
40 }
41
42 sub read {
43 my ($self) = @_;
44 eval {
45 alarm(2);
46 local $SIG{ALRM} = sub { die "alarm\n" };
47 while (<$self>) {
48 log_in($_);
49 next if m/^\d\d\d-/;
50 last;
51 }
52 alarm(0);
53 };
54 alarm(0);
55 if ($@) {
56 return undef;
57 }
58 return $_;
59 }
60
61 sub check {
62 my ($self, $regex, $name) = @_;
63 Test::More::like($self->read(), $regex, $name);
64 }
65
66 sub ok {
67 my $self = shift;
68 $self->check(qr/^2\d\d /, @_);
69 }
70
71 ###############################################################################
72
73 1;
74
75 ###############################################################################