Code

Updated with more tests re: multiple status codes (Sven Nierlein)
[nagiosplug.git] / plugins / tests / check_http.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # Test check_http by having an actual HTTP server running
4 #
6 use strict;
7 use Test::More;
8 use NPTest;
9 use FindBin qw($Bin);
11 use HTTP::Daemon;
12 use HTTP::Status;
13 use HTTP::Response;
15 my $port = 50000 + int(rand(1000));
17 my $pid = fork();
18 if ($pid) {
19         # Parent
20         #print "parent\n";
21         # give our webserver some time to startup
22         sleep(1);
23 } else {
24         # Child
25         #print "child\n";
27         my $d = HTTP::Daemon->new(
28                 LocalPort => $port
29         ) || die;
30         print "Please contact me at: <URL:", $d->url, ">\n";
31         while (my $c = $d->accept ) {
32                 while (my $r = $c->get_request) {
33                         if ($r->method eq "GET" and $r->url->path eq "/xyzzy") {
34                                 $c->send_file_response("/etc/passwd");
35                         } elsif ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
36                                 $c->send_basic_header($1);
37                                 $c->send_crlf;
38                         } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
39                                 $c->send_basic_header;
40                                 $c->send_crlf;
41                                 $c->send_file_response("$Bin/var/$1");
42                         } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
43                                 $c->send_basic_header;
44                                 $c->send_crlf;
45                                 sleep 1;
46                                 $c->send_response("slow");
47                         } else {
48                                 $c->send_error(RC_FORBIDDEN);
49                         }
50                         $c->close;
51                 }
52         }
53         exit;
54 }
56 END { if ($pid) { print "Killing $pid\n"; kill "INT", $pid } };
58 if ($ARGV[0] && $ARGV[0] eq "-d") {
59         sleep 1000;
60 }
62 if (-x "./check_http") {
63         plan tests => 19;
64 } else {
65         plan skip_all => "No check_http compiled";
66 }
68 my $result;
69 my $command = "./check_http -H 127.0.0.1 -p $port";
71 $result = NPTest->testCmd( "$command -u /file/root" );
72 is( $result->return_code, 0, "/file/root");
73 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" );
75 TODO: {
76 local $TODO = "Output is different if a string is requested - should this be right?";
77 $result = NPTest->testCmd( "$command -u /file/root -s Root" );
78 is( $result->return_code, 0, "/file/root search for string");
79 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" );
80 }
82 $result = NPTest->testCmd( "$command -u /slow" );
83 is( $result->return_code, 0, "/file/root");
84 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 177 bytes in ([\d\.]+) seconds/', "Output correct" );
85 $result->output =~ /in ([\d\.]+) seconds/;
86 cmp_ok( $1, ">", 1, "Time is > 1 second" );
88 my $cmd;
89 $cmd = "$command -u /statuscode/200 -e 200";
90 $result = NPTest->testCmd( $cmd );
91 is( $result->return_code, 0, $cmd);
92 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
94 $cmd = "$command -u /statuscode/201 -e 201";
95 $result = NPTest->testCmd( $cmd );
96 is( $result->return_code, 0, $cmd);
97 like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds /', "Output correct: ".$result->output );
99 $cmd = "$command -u /statuscode/201 -e 200";
100 $result = NPTest->testCmd( $cmd );
101 is( $result->return_code, 2, $cmd);
102 like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
104 $cmd = "$command -u /statuscode/200 -e 200,201,202";
105 $result = NPTest->testCmd( $cmd );
106 is( $result->return_code, 0, $cmd);
107 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
109 $cmd = "$command -u /statuscode/201 -e 200,201,202";
110 $result = NPTest->testCmd( $cmd );
111 is( $result->return_code, 0, $cmd);
112 like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
114 $cmd = "$command -u /statuscode/203 -e 200,201,202";
115 $result = NPTest->testCmd( $cmd );
116 is( $result->return_code, 2, $cmd);
117 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 );