Code

Fix minor test issues
[nagiosplug.git] / plugins / tests / check_http.t
index 88b77d3b8cd7b662a8ebe99045451edd8d18f3e2..74eff1758acdcd3130362fb23a2ed6f7a1baa9e6 100755 (executable)
@@ -18,9 +18,24 @@ use Test::More;
 use NPTest;
 use FindBin qw($Bin);
 
-use HTTP::Daemon;
-use HTTP::Status;
-use HTTP::Response;
+my $common_tests = 66;
+my $ssl_only_tests = 6;
+# Check that all dependent modules are available
+eval {
+       require HTTP::Daemon;
+       require HTTP::Status;
+       require HTTP::Response;
+};
+
+if ($@) {
+       plan skip_all => "Missing required module for test: $@";
+} else {
+       if (-x "./check_http") {
+               plan tests => $common_tests * 2 + $ssl_only_tests;
+       } else {
+               plan skip_all => "No check_http compiled";
+       }
+}
 
 my $servers = { http => 0 };   # HTTP::Daemon should always be available
 eval { require HTTP::Daemon::SSL };
@@ -112,9 +127,9 @@ sub run_server {
                                $c->send_response("slow");
                        } elsif ($r->url->path eq "/method") {
                                if ($r->method eq "DELETE") {
-                                       $c->send_error(RC_METHOD_NOT_ALLOWED);
+                                       $c->send_error(HTTP::Status->RC_METHOD_NOT_ALLOWED);
                                } elsif ($r->method eq "foo") {
-                                       $c->send_error(RC_NOT_IMPLEMENTED);
+                                       $c->send_error(HTTP::Status->RC_NOT_IMPLEMENTED);
                                } else {
                                        $c->send_status_line(200, $r->method);
                                }
@@ -138,7 +153,7 @@ sub run_server {
                                delete($persist[1000]);
                                next MAINLOOP;
                        } else {
-                               $c->send_error(RC_FORBIDDEN);
+                               $c->send_error(HTTP::Status->RC_FORBIDDEN);
                        }
                        $c->close;
                }
@@ -157,14 +172,6 @@ if ($ARGV[0] && $ARGV[0] eq "-d") {
        }
 }
 
-my $common_tests = 62;
-my $ssl_only_tests = 6;
-if (-x "./check_http") {
-       plan tests => $common_tests * 2 + $ssl_only_tests;
-} else {
-       plan skip_all => "No check_http compiled";
-}
-
 my $result;
 my $command = "./check_http -H 127.0.0.1";
 
@@ -205,6 +212,14 @@ sub run_common_tests {
        is( $result->return_code, 0, "/file/root search for string");
        like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
 
+       $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" );
+       is( $result->return_code, 2, "Missing string check");
+       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");
+
+       $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" );
+       is( $result->return_code, 2, "Missing string check");
+       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");
+
 
        my $cmd;
        $cmd = "$command -u /slow";