diff options
Diffstat (limited to 'plugins')
-rwxr-xr-x | plugins/tests/check_http.t | 101 | ||||
-rw-r--r-- | plugins/tests/var/root | 1 |
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 | |||
6 | use strict; | ||
7 | use Test::More; | ||
8 | use NPTest; | ||
9 | use FindBin qw($Bin); | ||
10 | |||
11 | use HTTP::Daemon; | ||
12 | use HTTP::Status; | ||
13 | use HTTP::Response; | ||
14 | |||
15 | my $port = 50000 + int(rand(1000)); | ||
16 | |||
17 | my $pid = fork(); | ||
18 | if ($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 | |||
54 | END { if ($pid) { print "Killing $pid\n"; kill "INT", $pid } }; | ||
55 | |||
56 | if ($ARGV[0] && $ARGV[0] eq "-d") { | ||
57 | sleep 1000; | ||
58 | } | ||
59 | |||
60 | if (-x "./check_http") { | ||
61 | plan tests => 13; | ||
62 | } else { | ||
63 | plan skip_all => "No check_http compiled"; | ||
64 | } | ||
65 | |||
66 | my $result; | ||
67 | my $command = "./check_http -H 127.0.0.1 -p $port"; | ||
68 | |||
69 | $result = NPTest->testCmd( "$command -u /file/root" ); | ||
70 | is( $result->return_code, 0, "/file/root"); | ||
71 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" ); | ||
72 | |||
73 | TODO: { | ||
74 | local $TODO = "Output is different if a string is requested - should this be right?"; | ||
75 | $result = NPTest->testCmd( "$command -u /file/root -s Root" ); | ||
76 | is( $result->return_code, 0, "/file/root search for string"); | ||
77 | like( $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" ); | ||
81 | is( $result->return_code, 0, "/file/root"); | ||
82 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 177 bytes in ([\d\.]+) seconds/', "Output correct" ); | ||
83 | $result->output =~ /in ([\d\.]+) seconds/; | ||
84 | cmp_ok( $1, ">", 1, "Time is > 1 second" ); | ||
85 | |||
86 | my $cmd; | ||
87 | $cmd = "$command -u /statuscode/200 -e 200"; | ||
88 | $result = NPTest->testCmd( $cmd ); | ||
89 | is( $result->return_code, 0, $cmd); | ||
90 | like( $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 ); | ||
94 | is( $result->return_code, 0, $cmd); | ||
95 | like( $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 ); | ||
99 | is( $result->return_code, 2, $cmd); | ||
100 | like( $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 | |||