diff smtp.t @ 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 f772e2a1a13f
children 7e28c64edf55
line wrap: on
line diff
--- a/smtp.t
+++ b/smtp.t
@@ -16,136 +16,138 @@ use Socket qw/ CRLF /;
 
 BEGIN { use FindBin; chdir($FindBin::Bin); }
 
-use _common;
+use lib 'lib';
+use Test::Nginx;
+use Test::Nginx::SMTP;
 
 ###############################################################################
 
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-start_nginx('smtp.conf');
+my $t = Test::Nginx->new()->run('smtp.conf');
 
 ###############################################################################
 
-my $s = smtp_connect();
-smtp_check(qr/^220 /, "greeting");
+my $s = Test::Nginx::SMTP->new();
+$s->check(qr/^220 /, "greeting");
 
-smtp_send('EHLO example.com');
-smtp_check(qr/^250 /, "ehlo");
+$s->send('EHLO example.com');
+$s->check(qr/^250 /, "ehlo");
 
-smtp_send('AUTH PLAIN ' . encode_base64("test\@example.com\0\0bad", ''));
-smtp_check(qr/^5.. /, 'auth plain with bad password');
+$s->send('AUTH PLAIN ' . encode_base64("test\@example.com\0\0bad", ''));
+$s->check(qr/^5.. /, 'auth plain with bad password');
 
-smtp_send('AUTH PLAIN ' . encode_base64("test\@example.com\0\0secret", ''));
-smtp_ok('auth plain');
+$s->send('AUTH PLAIN ' . encode_base64("test\@example.com\0\0secret", ''));
+$s->ok('auth plain');
 
 # We are talking to backend from this point
 
-smtp_send('MAIL FROM:<test@example.com> SIZE=100');
-smtp_ok('mail from after auth');
+$s->send('MAIL FROM:<test@example.com> SIZE=100');
+$s->ok('mail from after auth');
 
-smtp_send('RSET');
-smtp_ok('rset');
+$s->send('RSET');
+$s->ok('rset');
 
-smtp_send('MAIL FROM:<test@xn--e1afmkfd.xn--80akhbyknj4f> SIZE=100');
-smtp_ok("idn mail from (example.test in russian)");
+$s->send('MAIL FROM:<test@xn--e1afmkfd.xn--80akhbyknj4f> SIZE=100');
+$s->ok("idn mail from (example.test in russian)");
 
-smtp_send('QUIT');
-smtp_ok("quit");
+$s->send('QUIT');
+$s->ok("quit");
 
 # Try auth plain with pipelining
 
-$s = smtp_connect();
-smtp_check(qr/^220 /, "greeting");
+$s = Test::Nginx::SMTP->new();
+$s->check(qr/^220 /, "greeting");
 
-smtp_send('EHLO example.com');
-smtp_check(qr/^250 /, "ehlo");
+$s->send('EHLO example.com');
+$s->check(qr/^250 /, "ehlo");
 
-smtp_send('INVALID COMMAND WITH ARGUMENTS' . CRLF
+$s->send('INVALID COMMAND WITH ARGUMENTS' . CRLF
 	. 'RSET');
-smtp_read();
-smtp_ok('pipelined rset after invalid command');
+$s->read();
+$s->ok('pipelined rset after invalid command');
 
-smtp_send('AUTH PLAIN '
+$s->send('AUTH PLAIN '
 	. encode_base64("test\@example.com\0\0bad", '') . CRLF
 	. 'MAIL FROM:<test@example.com> SIZE=100');
-smtp_read();
-smtp_ok('mail from after failed pipelined auth');
+$s->read();
+$s->ok('mail from after failed pipelined auth');
 
-smtp_send('AUTH PLAIN '
+$s->send('AUTH PLAIN '
 	. encode_base64("test\@example.com\0\0secret", '') . CRLF
 	. 'MAIL FROM:<test@example.com> SIZE=100');
-smtp_read();
-smtp_ok('mail from after pipelined auth');
+$s->read();
+$s->ok('mail from after pipelined auth');
 
 # Try auth none
 
-$s = smtp_connect();
-smtp_check(qr/^220 /, "greeting");
+$s = Test::Nginx::SMTP->new();
+$s->check(qr/^220 /, "greeting");
 
-smtp_send('EHLO example.com');
-smtp_check(qr/^250 /, "ehlo");
+$s->send('EHLO example.com');
+$s->check(qr/^250 /, "ehlo");
 
-smtp_send('MAIL FROM:<test@example.com> SIZE=100');
-smtp_ok('auth none - mail from');
+$s->send('MAIL FROM:<test@example.com> SIZE=100');
+$s->ok('auth none - mail from');
 
-smtp_send('RCPT TO:<test@example.com>');
-smtp_ok('auth none - rcpt to');
+$s->send('RCPT TO:<test@example.com>');
+$s->ok('auth none - rcpt to');
 
-smtp_send('RSET');
-smtp_ok('auth none - rset, should go to backend');
+$s->send('RSET');
+$s->ok('auth none - rset, should go to backend');
 
 # Auth none with pipelining
 
-$s = smtp_connect();
-smtp_check(qr/^220 /, "greeting");
+$s = Test::Nginx::SMTP->new();
+$s->check(qr/^220 /, "greeting");
 
-smtp_send('EHLO example.com');
-smtp_check(qr/^250 /, "ehlo");
+$s->send('EHLO example.com');
+$s->check(qr/^250 /, "ehlo");
 
-smtp_send('MAIL FROM:<test@example.com> SIZE=100' . CRLF
+$s->send('MAIL FROM:<test@example.com> SIZE=100' . CRLF
 	. 'RCPT TO:<test@example.com>' . CRLF
 	. 'RSET');
 
-smtp_ok('pipelined mail from');
+$s->ok('pipelined mail from');
 
-smtp_ok('pipelined rcpt to');
-smtp_ok('pipelined rset');
+$s->ok('pipelined rcpt to');
+$s->ok('pipelined rset');
 
 # Connection must stay even if error returned to rcpt to command
 
-$s = smtp_connect();
-smtp_read(); # skip greeting
+$s = Test::Nginx::SMTP->new();
+$s->read(); # skip greeting
 
-smtp_send('EHLO example.com');
-smtp_read(); # skip ehlo reply
+$s->send('EHLO example.com');
+$s->read(); # skip ehlo reply
 
-smtp_send('MAIL FROM:<test@example.com> SIZE=100');
-smtp_read(); # skip mail from reply
+$s->send('MAIL FROM:<test@example.com> SIZE=100');
+$s->read(); # skip mail from reply
 
-smtp_send('RCPT TO:<example.com>');
-smtp_check(qr/^5.. /, "bad rcpt to");
+$s->send('RCPT TO:<example.com>');
+$s->check(qr/^5.. /, "bad rcpt to");
 
-smtp_send('RCPT TO:<test@example.com>');
-smtp_ok('good rcpt to');
+$s->send('RCPT TO:<test@example.com>');
+$s->ok('good rcpt to');
 
 # Make sure command splitted into many packets processed correctly
 
-$s = smtp_connect();
-smtp_read();
+$s = Test::Nginx::SMTP->new();
+$s->read();
 
 log_out('HEL');
 $s->print('HEL');
-smtp_send('O example.com');
-smtp_ok('splitted command');
+$s->send('O example.com');
+$s->ok('splitted command');
 
 # With smtp_greeting_delay session expected to be closed after first error
 # message if client sent something before greeting.  Use 10026 port
 # configured with smtp_greeting_delay 0.1s to check this.
 
-$s = smtp_connect(PeerPort => 10026);
-smtp_send('HELO example.com');
-smtp_check(qr/^5.. /, "command before greeting - session must be rejected");
+$s = Test::Nginx::SMTP->new(PeerPort => 10026);
+$s->send('HELO example.com');
+$s->check(qr/^5.. /, "command before greeting - session must be rejected");
 ok($s->eof(), "session have to be closed");
 
 ###############################################################################