Mercurial > hg > nginx-tests
comparison _common.pm @ 5:4d75bdb05ecf
Tests: some generic code and ssi tests.
Move generic code to _common.pm and add test for ssi big includes.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sun, 07 Sep 2008 05:00:28 +0400 |
parents | |
children | 8813a78ab8b5 |
comparison
equal
deleted
inserted
replaced
4:6c41dbb2954f | 5:4d75bdb05ecf |
---|---|
1 package _common; | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Generict module for nginx tests. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use base qw/ Exporter /; | |
13 | |
14 our @EXPORT = qw/ start_nginx smtp_connect smtp_send smtp_read smtp_check | |
15 smtp_ok log_in log_out CRLF http /; | |
16 | |
17 ############################################################################### | |
18 | |
19 use Test::More; | |
20 use File::Temp qw/ tempdir /; | |
21 use IO::Socket; | |
22 | |
23 use constant CRLF => "\x0D\x0A"; | |
24 | |
25 our $testdir; | |
26 our $s; | |
27 | |
28 ############################################################################### | |
29 | |
30 # Create temp directory and run nginx instance. | |
31 | |
32 sub start_nginx { | |
33 my ($conf) = @_; | |
34 | |
35 $testdir = tempdir('nginx-test-XXXXXXXXXX', TMPDIR => 1, CLEANUP => 1) | |
36 or die "Can't create temp directory: $!\n"; | |
37 | |
38 system("cat $conf | sed 's!%%TESTDIR%%!$testdir!g' " | |
39 . "> $testdir/nginx.conf"); | |
40 | |
41 my $pid = fork(); | |
42 die "Unable to fork(): $!\n" unless defined $pid; | |
43 | |
44 if ($pid == 0) { | |
45 exec('../nginx/objs/nginx', '-c', "$testdir/nginx.conf", '-g', | |
46 "pid $testdir/nginx.pid; " | |
47 . "error_log $testdir/nginx-error.log debug;") | |
48 or die "Unable to exec(): $!\n"; | |
49 } | |
50 | |
51 # wait for nginx to start | |
52 | |
53 sleep 1; | |
54 } | |
55 | |
56 sub stop_nginx { | |
57 # terminate nginx by SIGTERM | |
58 kill 15, `cat $testdir/nginx.pid`; | |
59 wait; | |
60 } | |
61 | |
62 END { | |
63 stop_nginx(); | |
64 } | |
65 | |
66 ############################################################################### | |
67 | |
68 sub log_out { | |
69 my ($msg) = @_; | |
70 $msg =~ s/^/# >> /gm; | |
71 $msg .= "\n" unless $msg =~ /\n\Z/; | |
72 print $msg; | |
73 } | |
74 | |
75 sub log_in { | |
76 my ($msg) = @_; | |
77 $msg =~ s/^/# << /gm; | |
78 $msg =~ s/([\x00-\x1f\x7f-])/sprintf('\\x%02x', ord($1)) . (($1 eq "\n") ? "\n" : '')/gmxe; | |
79 $msg .= "\n" unless $msg =~ /\n\Z/; | |
80 print $msg; | |
81 } | |
82 | |
83 ############################################################################### | |
84 | |
85 sub http { | |
86 my ($request) = @_; | |
87 my $reply; | |
88 eval { | |
89 local $SIG{ALRM} = sub { die "alarm\n" }; | |
90 alarm(2); | |
91 my $s = IO::Socket::INET->new( | |
92 Proto => 'tcp', | |
93 PeerHost => 'localhost:8080' | |
94 ); | |
95 log_out($request); | |
96 $s->print($request); | |
97 local $/; | |
98 $reply = $s->getline(); | |
99 log_in($reply); | |
100 alarm(0); | |
101 }; | |
102 alarm(0); | |
103 if ($@) { | |
104 log_in('(timeout)'); | |
105 return undef; | |
106 } | |
107 return $reply; | |
108 } | |
109 | |
110 ############################################################################### | |
111 | |
112 sub smtp_connect { | |
113 $s = IO::Socket::INET->new( | |
114 Proto => "tcp", | |
115 PeerAddr => "localhost", | |
116 PeerPort => 10025, | |
117 @_ | |
118 ) | |
119 or die "Can't connect to nginx: $!\n"; | |
120 | |
121 $s->autoflush(1); | |
122 | |
123 return $s; | |
124 } | |
125 | |
126 sub smtp_send { | |
127 my ($cmd) = @_; | |
128 log_out($cmd); | |
129 $s->print($cmd . CRLF); | |
130 } | |
131 | |
132 sub smtp_read { | |
133 my ($regex, $name) = @_; | |
134 eval { | |
135 alarm(2); | |
136 local $SIG{ALRM} = sub { die "alarm\n" }; | |
137 while (<$s>) { | |
138 log_in($_); | |
139 next if m/^\d\d\d-/; | |
140 last; | |
141 } | |
142 alarm(0); | |
143 }; | |
144 alarm(0); | |
145 if ($@) { | |
146 return undef; | |
147 } | |
148 return $_; | |
149 } | |
150 | |
151 sub smtp_check { | |
152 my ($regex, $name) = @_; | |
153 like(smtp_read(), $regex, $name); | |
154 } | |
155 | |
156 sub smtp_ok { | |
157 smtp_check(qr/^2\d\d /, @_); | |
158 } | |
159 | |
160 ############################################################################### | |
161 | |
162 1; | |
163 | |
164 ############################################################################### |