Mercurial > hg > nginx-tests
diff 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 |
line wrap: on
line diff
copy from _common.pm copy to lib/Test/Nginx/SMTP.pm --- a/_common.pm +++ b/lib/Test/Nginx/SMTP.pm @@ -1,125 +1,26 @@ -package _common; +package Test::Nginx::SMTP; # (C) Maxim Dounin -# Generict module for nginx tests. +# Module for nginx smtp tests. ############################################################################### use warnings; use strict; -use base qw/ Exporter /; - -our @EXPORT = qw/ start_nginx write_file smtp_connect smtp_send smtp_read - smtp_check smtp_ok log_in log_out http /; - -############################################################################### - -use Test::More; -use File::Temp qw/ tempdir /; +use Test::More qw//; use IO::Socket; use Socket qw/ CRLF /; -our $testdir; -our $s; - -############################################################################### - -# Create temp directory and run nginx instance. - -sub start_nginx { - my ($conf) = @_; - - $testdir = tempdir('nginx-test-XXXXXXXXXX', TMPDIR => 1, - CLEANUP => not $ENV{LEAVE}) - or die "Can't create temp directory: $!\n"; - - system("cat $conf | sed 's!%%TESTDIR%%!$testdir!g' " - . "> $testdir/nginx.conf"); - - my $pid = fork(); - die "Unable to fork(): $!\n" unless defined $pid; +use Test::Nginx; - if ($pid == 0) { - exec('../nginx/objs/nginx', '-c', "$testdir/nginx.conf", '-g', - "pid $testdir/nginx.pid; " - . "error_log $testdir/nginx-error.log debug;") - or die "Unable to exec(): $!\n"; - } - - # wait for nginx to start - - sleep 1; -} - -sub stop_nginx { - # terminate nginx by SIGTERM - kill 15, `cat $testdir/nginx.pid`; - wait; -} - -END { - stop_nginx(); -} - -sub write_file { - my ($name, $content) = @_; +use base qw/ IO::Socket::INET /; - open F, '>' . $testdir . '/' . $name - or die "Can't create $name: $!"; - print F $content; - close F; -} - -############################################################################### - -sub log_out { - my ($msg) = @_; - $msg =~ s/^/# >> /gm; - $msg .= "\n" unless $msg =~ /\n\Z/; - print $msg; -} - -sub log_in { - my ($msg) = @_; - $msg =~ s/^/# << /gm; - $msg =~ s/([^\x20-\x7e])/sprintf('\\x%02x', ord($1)) . (($1 eq "\n") ? "\n" : '')/gmxe; - $msg .= "\n" unless $msg =~ /\n\Z/; - print $msg; -} - -############################################################################### +sub new { + my $class = shift; -sub http { - my ($request) = @_; - my $reply; - eval { - local $SIG{ALRM} = sub { die "alarm\n" }; - alarm(2); - my $s = IO::Socket::INET->new( - Proto => 'tcp', - PeerHost => 'localhost:8080' - ); - log_out($request); - $s->print($request); - local $/; - $reply = $s->getline(); - log_in($reply); - alarm(0); - }; - alarm(0); - if ($@) { - log_in('(timeout)'); - return undef; - } - return $reply; -} - -############################################################################### - -sub smtp_connect { - $s = IO::Socket::INET->new( + my $self = return $class->SUPER::new( Proto => "tcp", PeerAddr => "localhost", PeerPort => 10025, @@ -127,23 +28,23 @@ sub smtp_connect { ) or die "Can't connect to nginx: $!\n"; - $s->autoflush(1); + $self->autoflush(1); - return $s; + return $self; } -sub smtp_send { - my ($cmd) = @_; +sub send { + my ($self, $cmd) = @_; log_out($cmd); - $s->print($cmd . CRLF); + $self->print($cmd . CRLF); } -sub smtp_read { - my ($regex, $name) = @_; +sub read { + my ($self) = @_; eval { alarm(2); local $SIG{ALRM} = sub { die "alarm\n" }; - while (<$s>) { + while (<$self>) { log_in($_); next if m/^\d\d\d-/; last; @@ -157,13 +58,14 @@ sub smtp_read { return $_; } -sub smtp_check { - my ($regex, $name) = @_; - like(smtp_read(), $regex, $name); +sub check { + my ($self, $regex, $name) = @_; + Test::More::like($self->read(), $regex, $name); } -sub smtp_ok { - smtp_check(qr/^2\d\d /, @_); +sub ok { + my $self = shift; + $self->check(qr/^2\d\d /, @_); } ###############################################################################