summaryrefslogtreecommitdiffstats
path: root/plugins/tests/check_curl.t
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/tests/check_curl.t')
-rwxr-xr-xplugins/tests/check_curl.t508
1 files changed, 508 insertions, 0 deletions
diff --git a/plugins/tests/check_curl.t b/plugins/tests/check_curl.t
new file mode 100755
index 0000000..0caad23
--- /dev/null
+++ b/plugins/tests/check_curl.t
@@ -0,0 +1,508 @@
1#! /usr/bin/perl -w -I ..
2#
3# Test check_http by having an actual HTTP server running
4#
5# To create the https server certificate:
6# openssl req -new -x509 -keyout server-key.pem -out server-cert.pem -days 3650 -nodes
7# to create a new expired certificate:
8# faketime '2008-01-01 12:00:00' openssl req -new -x509 -keyout expired-key.pem -out expired-cert.pem -days 1 -nodes
9# Country Name (2 letter code) [AU]:DE
10# State or Province Name (full name) [Some-State]:Bavaria
11# Locality Name (eg, city) []:Munich
12# Organization Name (eg, company) [Internet Widgits Pty Ltd]:Monitoring Plugins
13# Organizational Unit Name (eg, section) []:
14# Common Name (e.g. server FQDN or YOUR name) []:Monitoring Plugins
15# Email Address []:devel@monitoring-plugins.org
16
17use strict;
18use Test::More;
19use NPTest;
20use FindBin qw($Bin);
21
22$ENV{'LC_TIME'} = "C";
23
24my $common_tests = 72;
25my $ssl_only_tests = 8;
26# Check that all dependent modules are available
27eval "use HTTP::Daemon 6.01;";
28plan skip_all => 'HTTP::Daemon >= 6.01 required' if $@;
29eval {
30 require HTTP::Status;
31 require HTTP::Response;
32};
33
34my $plugin = 'check_http';
35$plugin = 'check_curl' if $0 =~ m/check_curl/mx;
36
37# look for libcurl version to see if some advanced checks are possible (>= 7.49.0)
38my $advanced_checks = 12;
39my $use_advanced_checks = 0;
40my $required_version = '7.49.0';
41my $virtual_host = 'www.somefunnyhost.com';
42my $virtual_port = 42;
43my $curl_version = '';
44open (my $fh, '-|', "./$plugin --version") or die;
45while (<$fh>) {
46 if (m{libcurl/([\d.]+)\s}) {
47 $curl_version = $1;
48 last;
49 }
50}
51close ($fh);
52if ($curl_version) {
53 my ($major, $minor, $release) = split (/\./, $curl_version);
54 my ($req_major, $req_minor, $req_release) = split (/\./, $required_version);
55 my $check = ($major <=> $req_major or $minor <=> $req_minor or $release <=> $req_release);
56 if ($check >= 0) {
57 $use_advanced_checks = 1;
58 print "Found libcurl $major.$minor.$release. Using advanced checks\n";
59 }
60}
61
62if ($@) {
63 plan skip_all => "Missing required module for test: $@";
64} else {
65 if (-x "./$plugin") {
66 plan tests => $common_tests * 2 + $ssl_only_tests + $advanced_checks;
67 } else {
68 plan skip_all => "No $plugin compiled";
69 }
70}
71
72my $servers = { http => 0 }; # HTTP::Daemon should always be available
73eval { require HTTP::Daemon::SSL };
74if ($@) {
75 diag "Cannot load HTTP::Daemon::SSL: $@";
76} else {
77 $servers->{https} = 0;
78}
79
80# set a fixed version, so the header size doesn't vary
81$HTTP::Daemon::VERSION = "1.00";
82
83my $port_http = 50000 + int(rand(1000));
84my $port_https = $port_http + 1;
85my $port_https_expired = $port_http + 2;
86
87# This array keeps sockets around for implementing timeouts
88my @persist;
89
90# Start up all servers
91my @pids;
92my $pid = fork();
93if ($pid) {
94 # Parent
95 push @pids, $pid;
96 if (exists $servers->{https}) {
97 # Fork a normal HTTPS server
98 $pid = fork();
99 if ($pid) {
100 # Parent
101 push @pids, $pid;
102 # Fork an expired cert server
103 $pid = fork();
104 if ($pid) {
105 push @pids, $pid;
106 } else {
107 my $d = HTTP::Daemon::SSL->new(
108 LocalPort => $port_https_expired,
109 LocalAddr => "127.0.0.1",
110 SSL_cert_file => "$Bin/certs/expired-cert.pem",
111 SSL_key_file => "$Bin/certs/expired-key.pem",
112 ) || die;
113 print "Please contact https expired at: <URL:", $d->url, ">\n";
114 run_server( $d );
115 exit;
116 }
117 } else {
118 my $d = HTTP::Daemon::SSL->new(
119 LocalPort => $port_https,
120 LocalAddr => "127.0.0.1",
121 SSL_cert_file => "$Bin/certs/server-cert.pem",
122 SSL_key_file => "$Bin/certs/server-key.pem",
123 ) || die;
124 print "Please contact https at: <URL:", $d->url, ">\n";
125 run_server( $d );
126 exit;
127 }
128 }
129 # give our webservers some time to startup
130 sleep(1);
131} else {
132 # Child
133 #print "child\n";
134 my $d = HTTP::Daemon->new(
135 LocalPort => $port_http,
136 LocalAddr => "127.0.0.1",
137 ) || die;
138 print "Please contact http at: <URL:", $d->url, ">\n";
139 run_server( $d );
140 exit;
141}
142
143# Run the same server on http and https
144sub run_server {
145 my $d = shift;
146 MAINLOOP: while (my $c = $d->accept ) {
147 while (my $r = $c->get_request) {
148 if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
149 $c->send_basic_header($1);
150 $c->send_crlf;
151 } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
152 $c->send_basic_header;
153 $c->send_crlf;
154 $c->send_file_response("$Bin/var/$1");
155 } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
156 $c->send_basic_header;
157 $c->send_crlf;
158 sleep 1;
159 $c->send_response("slow");
160 } elsif ($r->url->path eq "/method") {
161 if ($r->method eq "DELETE") {
162 $c->send_error(HTTP::Status->RC_METHOD_NOT_ALLOWED);
163 } elsif ($r->method eq "foo") {
164 $c->send_error(HTTP::Status->RC_NOT_IMPLEMENTED);
165 } else {
166 $c->send_status_line(200, $r->method);
167 }
168 } elsif ($r->url->path eq "/postdata") {
169 $c->send_basic_header;
170 $c->send_crlf;
171 $c->send_response($r->method.":".$r->content);
172 } elsif ($r->url->path eq "/redirect") {
173 $c->send_redirect( "/redirect2" );
174 } elsif ($r->url->path eq "/redir_external") {
175 $c->send_redirect(($d->isa('HTTP::Daemon::SSL') ? "https" : "http") . "://169.254.169.254/redirect2" );
176 } elsif ($r->url->path eq "/redirect2") {
177 $c->send_basic_header;
178 $c->send_crlf;
179 $c->send_response(HTTP::Response->new( 200, 'OK', undef, 'redirected' ));
180 } elsif ($r->url->path eq "/redir_timeout") {
181 $c->send_redirect( "/timeout" );
182 } elsif ($r->url->path eq "/timeout") {
183 # Keep $c from being destroyed, but prevent severe leaks
184 unshift @persist, $c;
185 delete($persist[1000]);
186 next MAINLOOP;
187 } elsif ($r->url->path eq "/header_check") {
188 $c->send_basic_header;
189 $c->send_header('foo');
190 $c->send_crlf;
191 } elsif ($r->url->path eq "/header_broken_check") {
192 $c->send_basic_header;
193 $c->send_header('foo');
194 print $c "Test1:: broken\n";
195 print $c " Test2: leading whitespace\n";
196 $c->send_crlf;
197 } elsif ($r->url->path eq "/virtual_port") {
198 # return sent Host header
199 $c->send_basic_header;
200 $c->send_crlf;
201 $c->send_response(HTTP::Response->new( 200, 'OK', undef, $r->header ('Host')));
202 } else {
203 $c->send_error(HTTP::Status->RC_FORBIDDEN);
204 }
205 $c->close;
206 }
207 }
208}
209
210END {
211 foreach my $pid (@pids) {
212 if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
213 }
214};
215
216if ($ARGV[0] && $ARGV[0] eq "-d") {
217 while (1) {
218 sleep 100;
219 }
220}
221
222my $result;
223my $command = "./$plugin -H 127.0.0.1";
224
225run_common_tests( { command => "$command -p $port_http" } );
226SKIP: {
227 skip "HTTP::Daemon::SSL not installed", $common_tests + $ssl_only_tests if ! exists $servers->{https};
228 run_common_tests( { command => "$command -p $port_https", ssl => 1 } );
229
230 $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
231 is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
232 is( $result->output, "OK - Certificate 'Monitoring Plugins' will expire on Fri Feb 16 15:31:44 2029 +0000.", "output ok" );
233
234 $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
235 is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
236 like( $result->output, '/WARNING - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(Fri Feb 16 15:31:44 2029 \+0000\)./', "output ok" );
237
238 # Expired cert tests
239 $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" );
240 is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" );
241 like( $result->output, '/CRITICAL - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(Fri Feb 16 15:31:44 2029 \+0000\)./', "output ok" );
242
243 $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
244 is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
245 is( $result->output,
246 'CRITICAL - Certificate \'Monitoring Plugins\' expired on Wed Jan 2 11:00:26 2008 +0000.',
247 "output ok" );
248
249}
250
251my $cmd;
252
253# advanced checks with virtual hostname and virtual port
254SKIP: {
255 skip "libcurl version is smaller than $required_version", 6 unless $use_advanced_checks;
256
257 # http without virtual port
258 $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$port_http\$";
259 $result = NPTest->testCmd( $cmd );
260 is( $result->return_code, 0, $cmd);
261 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
262
263 # http with virtual port (!= 80)
264 $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$virtual_port\$";
265 $result = NPTest->testCmd( $cmd );
266 is( $result->return_code, 0, $cmd);
267 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
268
269 # http with virtual port (80)
270 $cmd = "./$plugin -H $virtual_host:80 -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host\$";
271 $result = NPTest->testCmd( $cmd );
272 is( $result->return_code, 0, $cmd);
273 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
274}
275
276# and the same for SSL
277SKIP: {
278 skip "libcurl version is smaller than $required_version and/or HTTP::Daemon::SSL not installed", 6 if ! exists $servers->{https} or not $use_advanced_checks;
279 # https without virtual port
280 $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$port_https\$";
281 $result = NPTest->testCmd( $cmd );
282 is( $result->return_code, 0, $cmd);
283 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
284
285 # https with virtual port (!= 443)
286 $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$virtual_port\$";
287 $result = NPTest->testCmd( $cmd );
288 is( $result->return_code, 0, $cmd);
289 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
290
291 # https with virtual port (443)
292 $cmd = "./$plugin -H $virtual_host:443 -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host\$";
293 $result = NPTest->testCmd( $cmd );
294 is( $result->return_code, 0, $cmd);
295 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
296}
297
298
299sub run_common_tests {
300 my ($opts) = @_;
301 my $command = $opts->{command};
302 if ($opts->{ssl}) {
303 $command .= " --ssl";
304 }
305
306 $result = NPTest->testCmd( "$command -u /file/root" );
307 is( $result->return_code, 0, "/file/root");
308 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
309
310 $result = NPTest->testCmd( "$command -u /file/root -s Root" );
311 is( $result->return_code, 0, "/file/root search for string");
312 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
313
314 $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" );
315 is( $result->return_code, 2, "Missing string check");
316 like( $result->output, qr%^HTTP CRITICAL: HTTP/1\.1 200 OK - string 'NonRoot' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location");
317
318 $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" );
319 is( $result->return_code, 2, "Missing string check");
320 like( $result->output, qr%HTTP CRITICAL: HTTP/1\.1 200 OK - string 'NonRootWithOver30charsAndM...' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location");
321
322 $result = NPTest->testCmd( "$command -u /header_check -d foo" );
323 is( $result->return_code, 0, "header_check search for string");
324 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 96 bytes in [\d\.]+ second/', "Output correct" );
325
326 $result = NPTest->testCmd( "$command -u /header_check -d bar" );
327 is( $result->return_code, 2, "Missing header string check");
328 like( $result->output, qr%^HTTP CRITICAL: HTTP/1\.1 200 OK - header 'bar' not found on 'https?://127\.0\.0\.1:\d+/header_check'%, "Shows search string and location");
329
330 $result = NPTest->testCmd( "$command -u /header_broken_check" );
331 is( $result->return_code, 0, "header_check search for string");
332 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 138 bytes in [\d\.]+ second/', "Output correct" );
333
334 my $cmd;
335 $cmd = "$command -u /slow";
336 $result = NPTest->testCmd( $cmd );
337 is( $result->return_code, 0, "$cmd");
338 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
339 $result->output =~ /in ([\d\.]+) second/;
340 cmp_ok( $1, ">", 1, "Time is > 1 second" );
341
342 $cmd = "$command -u /statuscode/200";
343 $result = NPTest->testCmd( $cmd );
344 is( $result->return_code, 0, $cmd);
345 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
346
347 $cmd = "$command -u /statuscode/200 -e 200";
348 $result = NPTest->testCmd( $cmd );
349 is( $result->return_code, 0, $cmd);
350 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
351
352 $cmd = "$command -u /statuscode/201";
353 $result = NPTest->testCmd( $cmd );
354 is( $result->return_code, 0, $cmd);
355 like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
356
357 $cmd = "$command -u /statuscode/201 -e 201";
358 $result = NPTest->testCmd( $cmd );
359 is( $result->return_code, 0, $cmd);
360 like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
361
362 $cmd = "$command -u /statuscode/201 -e 200";
363 $result = NPTest->testCmd( $cmd );
364 is( $result->return_code, 2, $cmd);
365 like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
366
367 $cmd = "$command -u /statuscode/200 -e 200,201,202";
368 $result = NPTest->testCmd( $cmd );
369 is( $result->return_code, 0, $cmd);
370 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
371
372 $cmd = "$command -u /statuscode/201 -e 200,201,202";
373 $result = NPTest->testCmd( $cmd );
374 is( $result->return_code, 0, $cmd);
375 like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
376
377 $cmd = "$command -u /statuscode/203 -e 200,201,202";
378 $result = NPTest->testCmd( $cmd );
379 is( $result->return_code, 2, $cmd);
380 like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port (\d+): HTTP/1.1 203 Non-Authoritative Information/', "Output correct: ".$result->output );
381
382 $cmd = "$command -j HEAD -u /method";
383 $result = NPTest->testCmd( $cmd );
384 is( $result->return_code, 0, $cmd);
385 like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
386
387 $cmd = "$command -j POST -u /method";
388 $result = NPTest->testCmd( $cmd );
389 is( $result->return_code, 0, $cmd);
390 like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
391
392 $cmd = "$command -j GET -u /method";
393 $result = NPTest->testCmd( $cmd );
394 is( $result->return_code, 0, $cmd);
395 like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
396
397 $cmd = "$command -u /method";
398 $result = NPTest->testCmd( $cmd );
399 is( $result->return_code, 0, $cmd);
400 like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
401
402 $cmd = "$command -P foo -u /method";
403 $result = NPTest->testCmd( $cmd );
404 is( $result->return_code, 0, $cmd);
405 like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
406
407 $cmd = "$command -j DELETE -u /method";
408 $result = NPTest->testCmd( $cmd );
409 is( $result->return_code, 1, $cmd);
410 like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
411
412 $cmd = "$command -j foo -u /method";
413 $result = NPTest->testCmd( $cmd );
414 is( $result->return_code, 2, $cmd);
415 like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
416
417 $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
418 $result = NPTest->testCmd( $cmd );
419 is( $result->return_code, 0, $cmd);
420 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
421
422 $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
423 $result = NPTest->testCmd( $cmd );
424 is( $result->return_code, 0, $cmd);
425 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
426
427 # To confirm that the free doesn't segfault
428 $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
429 $result = NPTest->testCmd( $cmd );
430 is( $result->return_code, 0, $cmd);
431 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
432
433 $cmd = "$command -u /redirect";
434 $result = NPTest->testCmd( $cmd );
435 is( $result->return_code, 0, $cmd);
436 like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
437
438 $cmd = "$command -f follow -u /redirect";
439 $result = NPTest->testCmd( $cmd );
440 is( $result->return_code, 0, $cmd);
441 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
442
443 $cmd = "$command -u /redirect -k 'follow: me'";
444 $result = NPTest->testCmd( $cmd );
445 is( $result->return_code, 0, $cmd);
446 like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
447
448 $cmd = "$command -f follow -u /redirect -k 'follow: me'";
449 $result = NPTest->testCmd( $cmd );
450 is( $result->return_code, 0, $cmd);
451 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
452
453 $cmd = "$command -f sticky -u /redirect -k 'follow: me'";
454 $result = NPTest->testCmd( $cmd );
455 is( $result->return_code, 0, $cmd);
456 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
457
458 $cmd = "$command -f stickyport -u /redirect -k 'follow: me'";
459 $result = NPTest->testCmd( $cmd );
460 is( $result->return_code, 0, $cmd);
461 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
462
463 # These tests may block
464 print "ALRM\n";
465
466 # stickyport - on full urlS port is set back to 80 otherwise
467 $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
468 eval {
469 local $SIG{ALRM} = sub { die "alarm\n" };
470 alarm(2);
471 $result = NPTest->testCmd( $cmd );
472 alarm(0); };
473 isnt( $@, "alarm\n", $cmd );
474 is( $result->return_code, 0, $cmd );
475
476 # Let's hope there won't be any web server on :80 returning "redirected"!
477 $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
478 eval {
479 local $SIG{ALRM} = sub { die "alarm\n" };
480 alarm(2);
481 $result = NPTest->testCmd( $cmd );
482 alarm(0); };
483 isnt( $@, "alarm\n", $cmd );
484 isnt( $result->return_code, 0, $cmd );
485
486 # Test an external address - timeout
487 SKIP: {
488 skip "This doesn't seem to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
489 $cmd = "$command -f follow -u /redir_external -t 5";
490 eval {
491 $result = NPTest->testCmd( $cmd, 2 );
492 };
493 like( $@, "/timeout in command: $cmd/", $cmd );
494 }
495
496 $cmd = "$command -u /timeout -t 5";
497 eval {
498 $result = NPTest->testCmd( $cmd, 2 );
499 };
500 like( $@, "/timeout in command: $cmd/", $cmd );
501
502 $cmd = "$command -f follow -u /redir_timeout -t 2";
503 eval {
504 $result = NPTest->testCmd( $cmd, 5 );
505 };
506 is( $@, "", $cmd );
507
508}