Code

check_http now has options to specify the HTTP method (Jan - 2155152)
[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 # set a fixed version, so the header size doesn't vary
16 $HTTP::Daemon::VERSION = "1.00";
18 my $port = 50000 + int(rand(1000));
20 my $pid = fork();
21 if ($pid) {
22         # Parent
23         #print "parent\n";
24         # give our webserver some time to startup
25         sleep(1);
26 } else {
27         # Child
28         #print "child\n";
30         my $d = HTTP::Daemon->new(
31                 LocalPort => $port
32         ) || die;
33         print "Please contact me at: <URL:", $d->url, ">\n";
34         while (my $c = $d->accept ) {
35                 while (my $r = $c->get_request) {
36                         if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
37                                 $c->send_basic_header($1);
38                                 $c->send_crlf;
39                         } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
40                                 $c->send_basic_header;
41                                 $c->send_crlf;
42                                 $c->send_file_response("$Bin/var/$1");
43                         } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
44                                 $c->send_basic_header;
45                                 $c->send_crlf;
46                                 sleep 1;
47                                 $c->send_response("slow");
48                         } elsif ($r->url->path eq "/method") {
49                                 if ($r->method eq "DELETE") {
50                                         $c->send_error(RC_METHOD_NOT_ALLOWED);
51                                 } elsif ($r->method eq "foo") {
52                                         $c->send_error(RC_NOT_IMPLEMENTED);
53                                 } else {
54                                         $c->send_status_line(200, $r->method);
55                                 }
56                         } elsif ($r->url->path eq "/postdata") {
57                                 $c->send_basic_header;
58                                 $c->send_crlf;
59                                 $c->send_response($r->method.":".$r->content);
60                         } else {
61                                 $c->send_error(RC_FORBIDDEN);
62                         }
63                         $c->close;
64                 }
65         }
66         exit;
67 }
69 END { if ($pid) { print "Killing $pid\n"; kill "INT", $pid } };
71 if ($ARGV[0] && $ARGV[0] eq "-d") {
72         sleep 1000;
73 }
75 if (-x "./check_http") {
76         plan tests => 39;
77 } else {
78         plan skip_all => "No check_http compiled";
79 }
81 my $result;
82 my $command = "./check_http -H 127.0.0.1 -p $port";
84 $result = NPTest->testCmd( "$command -u /file/root" );
85 is( $result->return_code, 0, "/file/root");
86 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" );
88 $result = NPTest->testCmd( "$command -u /file/root -s Root" );
89 is( $result->return_code, 0, "/file/root search for string");
90 TODO: {
91 local $TODO = "Output is different if a string is requested - should this be right?";
92 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" );
93 }
95 $result = NPTest->testCmd( "$command -u /slow" );
96 is( $result->return_code, 0, "/file/root");
97 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 177 bytes in ([\d\.]+) seconds/', "Output correct" );
98 $result->output =~ /in ([\d\.]+) seconds/;
99 cmp_ok( $1, ">", 1, "Time is > 1 second" );
101 my $cmd;
102 $cmd = "$command -u /statuscode/200 -e 200";
103 $result = NPTest->testCmd( $cmd );
104 is( $result->return_code, 0, $cmd);
105 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
107 $cmd = "$command -u /statuscode/201 -e 201";
108 $result = NPTest->testCmd( $cmd );
109 is( $result->return_code, 0, $cmd);
110 like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds /', "Output correct: ".$result->output );
112 $cmd = "$command -u /statuscode/201 -e 200";
113 $result = NPTest->testCmd( $cmd );
114 is( $result->return_code, 2, $cmd);
115 like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
117 $cmd = "$command -u /statuscode/200 -e 200,201,202";
118 $result = NPTest->testCmd( $cmd );
119 is( $result->return_code, 0, $cmd);
120 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
122 $cmd = "$command -u /statuscode/201 -e 200,201,202";
123 $result = NPTest->testCmd( $cmd );
124 is( $result->return_code, 0, $cmd);
125 like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
127 $cmd = "$command -u /statuscode/203 -e 200,201,202";
128 $result = NPTest->testCmd( $cmd );
129 is( $result->return_code, 2, $cmd);
130 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 );
132 $cmd = "$command -j HEAD -u /method";
133 $result = NPTest->testCmd( $cmd );
134 is( $result->return_code, 0, $cmd);
135 like( $result->output, '/^HTTP OK HTTP/1.1 200 HEAD - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
137 $cmd = "$command -j POST -u /method";
138 $result = NPTest->testCmd( $cmd );
139 is( $result->return_code, 0, $cmd);
140 like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
142 $cmd = "$command -j GET -u /method";
143 $result = NPTest->testCmd( $cmd );
144 is( $result->return_code, 0, $cmd);
145 like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
147 $cmd = "$command -u /method";
148 $result = NPTest->testCmd( $cmd );
149 is( $result->return_code, 0, $cmd);
150 like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
152 $cmd = "$command -P foo -u /method";
153 $result = NPTest->testCmd( $cmd );
154 is( $result->return_code, 0, $cmd);
155 like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
157 $cmd = "$command -j DELETE -u /method";
158 $result = NPTest->testCmd( $cmd );
159 is( $result->return_code, 1, $cmd);
160 like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
162 $cmd = "$command -j foo -u /method";
163 $result = NPTest->testCmd( $cmd );
164 is( $result->return_code, 2, $cmd);
165 like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
167 $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
168 $result = NPTest->testCmd( $cmd );
169 is( $result->return_code, 0, $cmd);
170 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output );
172 $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
173 $result = NPTest->testCmd( $cmd );
174 is( $result->return_code, 0, $cmd);
175 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output );
177 # To confirm that the free doesn't segfault
178 $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
179 $result = NPTest->testCmd( $cmd );
180 is( $result->return_code, 0, $cmd);
181 like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output );