summaryrefslogtreecommitdiffstats
path: root/plugins/tests
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/tests')
-rwxr-xr-xplugins/tests/check_http.t101
-rw-r--r--plugins/tests/var/root1
2 files changed, 102 insertions, 0 deletions
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t
new file mode 100755
index 0000000..d5a7c82
--- /dev/null
+++ b/plugins/tests/check_http.t
@@ -0,0 +1,101 @@
1#! /usr/bin/perl -w -I ..
2#
3# Test check_http by having an actual HTTP server running
4#
5
6use strict;
7use Test::More;
8use NPTest;
9use FindBin qw($Bin);
10
11use HTTP::Daemon;
12use HTTP::Status;
13use HTTP::Response;
14
15my $port = 50000 + int(rand(1000));
16
17my $pid = fork();
18if ($pid) {
19 # Parent
20 #print "parent\n";
21} else {
22 # Child
23 #print "child\n";
24
25 my $d = HTTP::Daemon->new(
26 LocalPort => $port
27 ) || die;
28 print "Please contact me at: <URL:", $d->url, ">\n";
29 while (my $c = $d->accept ) {
30 while (my $r = $c->get_request) {
31 if ($r->method eq "GET" and $r->url->path eq "/xyzzy") {
32 $c->send_file_response("/etc/passwd");
33 } elsif ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
34 $c->send_basic_header($1);
35 $c->send_crlf;
36 } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
37 $c->send_basic_header;
38 $c->send_crlf;
39 $c->send_file_response("$Bin/var/$1");
40 } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
41 $c->send_basic_header;
42 $c->send_crlf;
43 sleep 1;
44 $c->send_response("slow");
45 } else {
46 $c->send_error(RC_FORBIDDEN);
47 }
48 $c->close;
49 }
50 }
51 exit;
52}
53
54END { if ($pid) { print "Killing $pid\n"; kill "INT", $pid } };
55
56if ($ARGV[0] && $ARGV[0] eq "-d") {
57 sleep 1000;
58}
59
60if (-x "./check_http") {
61 plan tests => 13;
62} else {
63 plan skip_all => "No check_http compiled";
64}
65
66my $result;
67my $command = "./check_http -H 127.0.0.1 -p $port";
68
69$result = NPTest->testCmd( "$command -u /file/root" );
70is( $result->return_code, 0, "/file/root");
71like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" );
72
73TODO: {
74local $TODO = "Output is different if a string is requested - should this be right?";
75$result = NPTest->testCmd( "$command -u /file/root -s Root" );
76is( $result->return_code, 0, "/file/root search for string");
77like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" );
78}
79
80$result = NPTest->testCmd( "$command -u /slow" );
81is( $result->return_code, 0, "/file/root");
82like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 177 bytes in ([\d\.]+) seconds/', "Output correct" );
83$result->output =~ /in ([\d\.]+) seconds/;
84cmp_ok( $1, ">", 1, "Time is > 1 second" );
85
86my $cmd;
87$cmd = "$command -u /statuscode/200 -e 200";
88$result = NPTest->testCmd( $cmd );
89is( $result->return_code, 0, $cmd);
90like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
91
92$cmd = "$command -u /statuscode/201 -e 201";
93$result = NPTest->testCmd( $cmd );
94is( $result->return_code, 0, $cmd);
95like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds /', "Output correct: ".$result->output );
96
97$cmd = "$command -u /statuscode/201 -e 200";
98$result = NPTest->testCmd( $cmd );
99is( $result->return_code, 2, $cmd);
100like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port /', "Output correct: ".$result->output );
101
diff --git a/plugins/tests/var/root b/plugins/tests/var/root
new file mode 100644
index 0000000..9339e13
--- /dev/null
+++ b/plugins/tests/var/root
@@ -0,0 +1 @@
Root