diff options
Diffstat (limited to 'plugins/tests')
-rwxr-xr-x | plugins/tests/check_curl.t | 498 | ||||
-rwxr-xr-x | plugins/tests/check_http.t | 9 |
2 files changed, 504 insertions, 3 deletions
diff --git a/plugins/tests/check_curl.t b/plugins/tests/check_curl.t new file mode 100755 index 0000000..1afbe4b --- /dev/null +++ b/plugins/tests/check_curl.t | |||
@@ -0,0 +1,498 @@ | |||
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 | |||
17 | use strict; | ||
18 | use Test::More; | ||
19 | use NPTest; | ||
20 | use FindBin qw($Bin); | ||
21 | |||
22 | $ENV{'LC_TIME'} = "C"; | ||
23 | |||
24 | my $common_tests = 70; | ||
25 | my $ssl_only_tests = 8; | ||
26 | # Check that all dependent modules are available | ||
27 | eval "use HTTP::Daemon 6.01;"; | ||
28 | plan skip_all => 'HTTP::Daemon >= 6.01 required' if $@; | ||
29 | eval { | ||
30 | require HTTP::Status; | ||
31 | require HTTP::Response; | ||
32 | }; | ||
33 | |||
34 | my $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) | ||
38 | my $advanced_checks = 12; | ||
39 | my $use_advanced_checks = 0; | ||
40 | my $required_version = '7.49.0'; | ||
41 | my $virtual_host = 'www.somefunnyhost.com'; | ||
42 | my $virtual_port = 42; | ||
43 | my $curl_version = ''; | ||
44 | open (my $fh, '-|', "./$plugin --version") or die; | ||
45 | while (<$fh>) { | ||
46 | if (m{libcurl/([\d.]+)\s}) { | ||
47 | $curl_version = $1; | ||
48 | last; | ||
49 | } | ||
50 | } | ||
51 | close ($fh); | ||
52 | if ($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 | |||
62 | if ($@) { | ||
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 | |||
72 | my $servers = { http => 0 }; # HTTP::Daemon should always be available | ||
73 | eval { require HTTP::Daemon::SSL }; | ||
74 | if ($@) { | ||
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 | |||
83 | my $port_http = 50000 + int(rand(1000)); | ||
84 | my $port_https = $port_http + 1; | ||
85 | my $port_https_expired = $port_http + 2; | ||
86 | |||
87 | # This array keeps sockets around for implementing timeouts | ||
88 | my @persist; | ||
89 | |||
90 | # Start up all servers | ||
91 | my @pids; | ||
92 | my $pid = fork(); | ||
93 | if ($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 | ||
144 | sub 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 "/virtual_port") { | ||
192 | # return sent Host header | ||
193 | $c->send_basic_header; | ||
194 | $c->send_crlf; | ||
195 | $c->send_response(HTTP::Response->new( 200, 'OK', undef, $r->header ('Host'))); | ||
196 | } else { | ||
197 | $c->send_error(HTTP::Status->RC_FORBIDDEN); | ||
198 | } | ||
199 | $c->close; | ||
200 | } | ||
201 | } | ||
202 | } | ||
203 | |||
204 | END { | ||
205 | foreach my $pid (@pids) { | ||
206 | if ($pid) { print "Killing $pid\n"; kill "INT", $pid } | ||
207 | } | ||
208 | }; | ||
209 | |||
210 | if ($ARGV[0] && $ARGV[0] eq "-d") { | ||
211 | while (1) { | ||
212 | sleep 100; | ||
213 | } | ||
214 | } | ||
215 | |||
216 | my $result; | ||
217 | my $command = "./$plugin -H 127.0.0.1"; | ||
218 | |||
219 | run_common_tests( { command => "$command -p $port_http" } ); | ||
220 | SKIP: { | ||
221 | skip "HTTP::Daemon::SSL not installed", $common_tests + $ssl_only_tests if ! exists $servers->{https}; | ||
222 | run_common_tests( { command => "$command -p $port_https", ssl => 1 } ); | ||
223 | |||
224 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14" ); | ||
225 | is( $result->return_code, 0, "$command -p $port_https -S -C 14" ); | ||
226 | is( $result->output, "OK - Certificate 'Monitoring Plugins' will expire on Fri Feb 16 15:31:44 2029 +0000.", "output ok" ); | ||
227 | |||
228 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" ); | ||
229 | is( $result->return_code, 1, "$command -p $port_https -S -C 14000" ); | ||
230 | like( $result->output, '/WARNING - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(Fri Feb 16 15:31:44 2029 \+0000\)./', "output ok" ); | ||
231 | |||
232 | # Expired cert tests | ||
233 | $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" ); | ||
234 | is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" ); | ||
235 | like( $result->output, '/CRITICAL - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(Fri Feb 16 15:31:44 2029 \+0000\)./', "output ok" ); | ||
236 | |||
237 | $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" ); | ||
238 | is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" ); | ||
239 | is( $result->output, | ||
240 | 'CRITICAL - Certificate \'Monitoring Plugins\' expired on Wed Jan 2 11:00:26 2008 +0000.', | ||
241 | "output ok" ); | ||
242 | |||
243 | } | ||
244 | |||
245 | my $cmd; | ||
246 | |||
247 | # advanced checks with virtual hostname and virtual port | ||
248 | SKIP: { | ||
249 | skip "libcurl version is smaller than $required_version", 6 unless $use_advanced_checks; | ||
250 | |||
251 | # http without virtual port | ||
252 | $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$port_http\$"; | ||
253 | $result = NPTest->testCmd( $cmd ); | ||
254 | is( $result->return_code, 0, $cmd); | ||
255 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
256 | |||
257 | # http with virtual port (!= 80) | ||
258 | $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$virtual_port\$"; | ||
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:80 -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host\$"; | ||
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 | |||
270 | # and the same for SSL | ||
271 | SKIP: { | ||
272 | 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; | ||
273 | # https without virtual port | ||
274 | $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$port_https\$"; | ||
275 | $result = NPTest->testCmd( $cmd ); | ||
276 | is( $result->return_code, 0, $cmd); | ||
277 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
278 | |||
279 | # https with virtual port (!= 443) | ||
280 | $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$virtual_port\$"; | ||
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:443 -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host\$"; | ||
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 | |||
292 | |||
293 | sub run_common_tests { | ||
294 | my ($opts) = @_; | ||
295 | my $command = $opts->{command}; | ||
296 | if ($opts->{ssl}) { | ||
297 | $command .= " --ssl"; | ||
298 | } | ||
299 | |||
300 | $result = NPTest->testCmd( "$command -u /file/root" ); | ||
301 | is( $result->return_code, 0, "/file/root"); | ||
302 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" ); | ||
303 | |||
304 | $result = NPTest->testCmd( "$command -u /file/root -s Root" ); | ||
305 | is( $result->return_code, 0, "/file/root search for string"); | ||
306 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" ); | ||
307 | |||
308 | $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" ); | ||
309 | is( $result->return_code, 2, "Missing string check"); | ||
310 | 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"); | ||
311 | |||
312 | $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" ); | ||
313 | is( $result->return_code, 2, "Missing string check"); | ||
314 | 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"); | ||
315 | |||
316 | $result = NPTest->testCmd( "$command -u /header_check -d foo" ); | ||
317 | is( $result->return_code, 0, "header_check search for string"); | ||
318 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 96 bytes in [\d\.]+ second/', "Output correct" ); | ||
319 | |||
320 | $result = NPTest->testCmd( "$command -u /header_check -d bar" ); | ||
321 | is( $result->return_code, 2, "Missing header string check"); | ||
322 | 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"); | ||
323 | |||
324 | my $cmd; | ||
325 | $cmd = "$command -u /slow"; | ||
326 | $result = NPTest->testCmd( $cmd ); | ||
327 | is( $result->return_code, 0, "$cmd"); | ||
328 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
329 | $result->output =~ /in ([\d\.]+) second/; | ||
330 | cmp_ok( $1, ">", 1, "Time is > 1 second" ); | ||
331 | |||
332 | $cmd = "$command -u /statuscode/200"; | ||
333 | $result = NPTest->testCmd( $cmd ); | ||
334 | is( $result->return_code, 0, $cmd); | ||
335 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
336 | |||
337 | $cmd = "$command -u /statuscode/200 -e 200"; | ||
338 | $result = NPTest->testCmd( $cmd ); | ||
339 | is( $result->return_code, 0, $cmd); | ||
340 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
341 | |||
342 | $cmd = "$command -u /statuscode/201"; | ||
343 | $result = NPTest->testCmd( $cmd ); | ||
344 | is( $result->return_code, 0, $cmd); | ||
345 | like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output ); | ||
346 | |||
347 | $cmd = "$command -u /statuscode/201 -e 201"; | ||
348 | $result = NPTest->testCmd( $cmd ); | ||
349 | is( $result->return_code, 0, $cmd); | ||
350 | like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output ); | ||
351 | |||
352 | $cmd = "$command -u /statuscode/201 -e 200"; | ||
353 | $result = NPTest->testCmd( $cmd ); | ||
354 | is( $result->return_code, 2, $cmd); | ||
355 | like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output ); | ||
356 | |||
357 | $cmd = "$command -u /statuscode/200 -e 200,201,202"; | ||
358 | $result = NPTest->testCmd( $cmd ); | ||
359 | is( $result->return_code, 0, $cmd); | ||
360 | 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 ); | ||
361 | |||
362 | $cmd = "$command -u /statuscode/201 -e 200,201,202"; | ||
363 | $result = NPTest->testCmd( $cmd ); | ||
364 | is( $result->return_code, 0, $cmd); | ||
365 | 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 ); | ||
366 | |||
367 | $cmd = "$command -u /statuscode/203 -e 200,201,202"; | ||
368 | $result = NPTest->testCmd( $cmd ); | ||
369 | is( $result->return_code, 2, $cmd); | ||
370 | 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 ); | ||
371 | |||
372 | $cmd = "$command -j HEAD -u /method"; | ||
373 | $result = NPTest->testCmd( $cmd ); | ||
374 | is( $result->return_code, 0, $cmd); | ||
375 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
376 | |||
377 | $cmd = "$command -j POST -u /method"; | ||
378 | $result = NPTest->testCmd( $cmd ); | ||
379 | is( $result->return_code, 0, $cmd); | ||
380 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
381 | |||
382 | $cmd = "$command -j GET -u /method"; | ||
383 | $result = NPTest->testCmd( $cmd ); | ||
384 | is( $result->return_code, 0, $cmd); | ||
385 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
386 | |||
387 | $cmd = "$command -u /method"; | ||
388 | $result = NPTest->testCmd( $cmd ); | ||
389 | is( $result->return_code, 0, $cmd); | ||
390 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
391 | |||
392 | $cmd = "$command -P foo -u /method"; | ||
393 | $result = NPTest->testCmd( $cmd ); | ||
394 | is( $result->return_code, 0, $cmd); | ||
395 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
396 | |||
397 | $cmd = "$command -j DELETE -u /method"; | ||
398 | $result = NPTest->testCmd( $cmd ); | ||
399 | is( $result->return_code, 1, $cmd); | ||
400 | like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output ); | ||
401 | |||
402 | $cmd = "$command -j foo -u /method"; | ||
403 | $result = NPTest->testCmd( $cmd ); | ||
404 | is( $result->return_code, 2, $cmd); | ||
405 | like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output ); | ||
406 | |||
407 | $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude"; | ||
408 | $result = NPTest->testCmd( $cmd ); | ||
409 | is( $result->return_code, 0, $cmd); | ||
410 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
411 | |||
412 | $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude"; | ||
413 | $result = NPTest->testCmd( $cmd ); | ||
414 | is( $result->return_code, 0, $cmd); | ||
415 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
416 | |||
417 | # To confirm that the free doesn't segfault | ||
418 | $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude"; | ||
419 | $result = NPTest->testCmd( $cmd ); | ||
420 | is( $result->return_code, 0, $cmd); | ||
421 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
422 | |||
423 | $cmd = "$command -u /redirect"; | ||
424 | $result = NPTest->testCmd( $cmd ); | ||
425 | is( $result->return_code, 0, $cmd); | ||
426 | like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
427 | |||
428 | $cmd = "$command -f follow -u /redirect"; | ||
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 -k 'follow: me'"; | ||
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 -k 'follow: me'"; | ||
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 -f sticky -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 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
447 | |||
448 | $cmd = "$command -f stickyport -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 | # These tests may block | ||
454 | print "ALRM\n"; | ||
455 | |||
456 | # stickyport - on full urlS port is set back to 80 otherwise | ||
457 | $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected"; | ||
458 | eval { | ||
459 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
460 | alarm(2); | ||
461 | $result = NPTest->testCmd( $cmd ); | ||
462 | alarm(0); }; | ||
463 | isnt( $@, "alarm\n", $cmd ); | ||
464 | is( $result->return_code, 0, $cmd ); | ||
465 | |||
466 | # Let's hope there won't be any web server on :80 returning "redirected"! | ||
467 | $cmd = "$command -f sticky -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 | isnt( $result->return_code, 0, $cmd ); | ||
475 | |||
476 | # Test an external address - timeout | ||
477 | SKIP: { | ||
478 | skip "This doesn't seem to work all the time", 1 unless ($ENV{HTTP_EXTERNAL}); | ||
479 | $cmd = "$command -f follow -u /redir_external -t 5"; | ||
480 | eval { | ||
481 | $result = NPTest->testCmd( $cmd, 2 ); | ||
482 | }; | ||
483 | like( $@, "/timeout in command: $cmd/", $cmd ); | ||
484 | } | ||
485 | |||
486 | $cmd = "$command -u /timeout -t 5"; | ||
487 | eval { | ||
488 | $result = NPTest->testCmd( $cmd, 2 ); | ||
489 | }; | ||
490 | like( $@, "/timeout in command: $cmd/", $cmd ); | ||
491 | |||
492 | $cmd = "$command -f follow -u /redir_timeout -t 2"; | ||
493 | eval { | ||
494 | $result = NPTest->testCmd( $cmd, 5 ); | ||
495 | }; | ||
496 | is( $@, "", $cmd ); | ||
497 | |||
498 | } | ||
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t index bfecff2..2f051fa 100755 --- a/plugins/tests/check_http.t +++ b/plugins/tests/check_http.t | |||
@@ -32,13 +32,16 @@ eval { | |||
32 | require HTTP::Response; | 32 | require HTTP::Response; |
33 | }; | 33 | }; |
34 | 34 | ||
35 | my $plugin = 'check_http'; | ||
36 | $plugin = 'check_curl' if $0 =~ m/check_curl/mx; | ||
37 | |||
35 | if ($@) { | 38 | if ($@) { |
36 | plan skip_all => "Missing required module for test: $@"; | 39 | plan skip_all => "Missing required module for test: $@"; |
37 | } else { | 40 | } else { |
38 | if (-x "./check_http") { | 41 | if (-x "./$plugin") { |
39 | plan tests => $common_tests * 2 + $ssl_only_tests + $virtual_port_tests; | 42 | plan tests => $common_tests * 2 + $ssl_only_tests + $virtual_port_tests; |
40 | } else { | 43 | } else { |
41 | plan skip_all => "No check_http compiled"; | 44 | plan skip_all => "No $plugin compiled"; |
42 | } | 45 | } |
43 | } | 46 | } |
44 | 47 | ||
@@ -187,7 +190,7 @@ if ($ARGV[0] && $ARGV[0] eq "-d") { | |||
187 | } | 190 | } |
188 | 191 | ||
189 | my $result; | 192 | my $result; |
190 | my $command = "./check_http -H 127.0.0.1"; | 193 | my $command = "./$plugin -H 127.0.0.1"; |
191 | 194 | ||
192 | run_common_tests( { command => "$command -p $port_http" } ); | 195 | run_common_tests( { command => "$command -p $port_http" } ); |
193 | SKIP: { | 196 | SKIP: { |