diff options
Diffstat (limited to 'plugins/tests/check_http.t')
-rwxr-xr-x | plugins/tests/check_http.t | 322 |
1 files changed, 194 insertions, 128 deletions
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t index c5f90803..20078c20 100755 --- a/plugins/tests/check_http.t +++ b/plugins/tests/check_http.t | |||
@@ -12,26 +12,59 @@ use HTTP::Daemon; | |||
12 | use HTTP::Status; | 12 | use HTTP::Status; |
13 | use HTTP::Response; | 13 | use HTTP::Response; |
14 | 14 | ||
15 | my $servers = { http => 0 }; # HTTP::Daemon should always be available | ||
16 | eval { require HTTP::Daemon::SSL }; | ||
17 | if ($@) { | ||
18 | diag "Cannot load HTTP::Daemon::SSL: $@"; | ||
19 | } else { | ||
20 | $servers->{https} = 0; | ||
21 | } | ||
22 | |||
15 | # set a fixed version, so the header size doesn't vary | 23 | # set a fixed version, so the header size doesn't vary |
16 | $HTTP::Daemon::VERSION = "1.00"; | 24 | $HTTP::Daemon::VERSION = "1.00"; |
17 | 25 | ||
18 | my $port = 50000 + int(rand(1000)); | 26 | my $port_http = 50000 + int(rand(1000)); |
27 | my $port_https = $port_http + 1; | ||
19 | 28 | ||
20 | my $pid = fork(); | 29 | # Start up both servers |
21 | if ($pid) { | 30 | my $pid_https; |
31 | my $pid_http = fork(); | ||
32 | if ($pid_http) { | ||
22 | # Parent | 33 | # Parent |
23 | #print "parent\n"; | 34 | if (exists $servers->{https}) { |
24 | # give our webserver some time to startup | 35 | # Fork another server |
36 | $pid_https = fork(); | ||
37 | if ($pid_https) { | ||
38 | # Parent | ||
39 | } else { | ||
40 | my $d = HTTP::Daemon::SSL->new( | ||
41 | LocalPort => $port_https, | ||
42 | LocalAddr => "127.0.0.1", | ||
43 | SSL_cert_file => "$Bin/certs/server-cert.pem", | ||
44 | SSL_key_file => "$Bin/certs/server-key.pem", | ||
45 | ) || die; | ||
46 | print "Please contact https at: <URL:", $d->url, ">\n"; | ||
47 | run_server( $d ); | ||
48 | exit; | ||
49 | } | ||
50 | } | ||
51 | # give our webservers some time to startup | ||
25 | sleep(1); | 52 | sleep(1); |
26 | } else { | 53 | } else { |
27 | # Child | 54 | # Child |
28 | #print "child\n"; | 55 | #print "child\n"; |
29 | |||
30 | my $d = HTTP::Daemon->new( | 56 | my $d = HTTP::Daemon->new( |
31 | LocalPort => $port, | 57 | LocalPort => $port_http, |
32 | LocalAddr => "127.0.0.1", | 58 | LocalAddr => "127.0.0.1", |
33 | ) || die; | 59 | ) || die; |
34 | print "Please contact me at: <URL:", $d->url, ">\n"; | 60 | print "Please contact http at: <URL:", $d->url, ">\n"; |
61 | run_server( $d ); | ||
62 | exit; | ||
63 | } | ||
64 | |||
65 | # Run the same server on http and https | ||
66 | sub run_server { | ||
67 | my $d = shift; | ||
35 | while (my $c = $d->accept ) { | 68 | while (my $c = $d->accept ) { |
36 | while (my $r = $c->get_request) { | 69 | while (my $r = $c->get_request) { |
37 | if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) { | 70 | if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) { |
@@ -70,140 +103,173 @@ if ($pid) { | |||
70 | $c->close; | 103 | $c->close; |
71 | } | 104 | } |
72 | } | 105 | } |
73 | exit; | ||
74 | } | 106 | } |
75 | 107 | ||
76 | END { if ($pid) { print "Killing $pid\n"; kill "INT", $pid } }; | 108 | END { |
109 | foreach my $pid ($pid_http, $pid_https) { | ||
110 | if ($pid) { print "Killing $pid\n"; kill "INT", $pid } | ||
111 | } | ||
112 | }; | ||
77 | 113 | ||
78 | if ($ARGV[0] && $ARGV[0] eq "-d") { | 114 | if ($ARGV[0] && $ARGV[0] eq "-d") { |
79 | sleep 1000; | 115 | sleep 1000; |
80 | } | 116 | } |
81 | 117 | ||
118 | my $common_tests = 47; | ||
82 | if (-x "./check_http") { | 119 | if (-x "./check_http") { |
83 | plan tests => 47; | 120 | plan tests => $common_tests * 2; |
84 | } else { | 121 | } else { |
85 | plan skip_all => "No check_http compiled"; | 122 | plan skip_all => "No check_http compiled"; |
86 | } | 123 | } |
87 | 124 | ||
88 | my $result; | 125 | my $result; |
89 | my $command = "./check_http -H 127.0.0.1 -p $port"; | 126 | my $command = "./check_http -H 127.0.0.1"; |
90 | |||
91 | $result = NPTest->testCmd( "$command -u /file/root" ); | ||
92 | is( $result->return_code, 0, "/file/root"); | ||
93 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" ); | ||
94 | 127 | ||
95 | $result = NPTest->testCmd( "$command -u /file/root -s Root" ); | 128 | run_common_tests( { command => "$command -p $port_http" } ); |
96 | is( $result->return_code, 0, "/file/root search for string"); | 129 | SKIP: { |
97 | TODO: { | 130 | skip "HTTP::Daemon::SSL not installed", $common_tests if ! exists $servers->{https}; |
98 | local $TODO = "Output is different if a string is requested - should this be right?"; | 131 | run_common_tests( { command => "$command -p $port_https", ssl => 1 } ); |
99 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" ); | ||
100 | } | 132 | } |
101 | 133 | ||
102 | $result = NPTest->testCmd( "$command -u /slow" ); | 134 | sub run_common_tests { |
103 | is( $result->return_code, 0, "/file/root"); | 135 | my ($opts) = @_; |
104 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 177 bytes in ([\d\.]+) seconds/', "Output correct" ); | 136 | my $command = $opts->{command}; |
105 | $result->output =~ /in ([\d\.]+) seconds/; | 137 | my $b; |
106 | cmp_ok( $1, ">", 1, "Time is > 1 second" ); | 138 | my $add = 0; |
107 | 139 | if ($opts->{ssl}) { | |
108 | my $cmd; | 140 | $command .= " --ssl"; |
109 | $cmd = "$command -u /statuscode/200 -e 200"; | 141 | } |
110 | $result = NPTest->testCmd( $cmd ); | ||
111 | is( $result->return_code, 0, $cmd); | ||
112 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
113 | |||
114 | $cmd = "$command -u /statuscode/201 -e 201"; | ||
115 | $result = NPTest->testCmd( $cmd ); | ||
116 | is( $result->return_code, 0, $cmd); | ||
117 | like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds /', "Output correct: ".$result->output ); | ||
118 | |||
119 | $cmd = "$command -u /statuscode/201 -e 200"; | ||
120 | $result = NPTest->testCmd( $cmd ); | ||
121 | is( $result->return_code, 2, $cmd); | ||
122 | like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output ); | ||
123 | |||
124 | $cmd = "$command -u /statuscode/200 -e 200,201,202"; | ||
125 | $result = NPTest->testCmd( $cmd ); | ||
126 | is( $result->return_code, 0, $cmd); | ||
127 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
128 | |||
129 | $cmd = "$command -u /statuscode/201 -e 200,201,202"; | ||
130 | $result = NPTest->testCmd( $cmd ); | ||
131 | is( $result->return_code, 0, $cmd); | ||
132 | like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
133 | |||
134 | $cmd = "$command -u /statuscode/203 -e 200,201,202"; | ||
135 | $result = NPTest->testCmd( $cmd ); | ||
136 | is( $result->return_code, 2, $cmd); | ||
137 | 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 ); | ||
138 | |||
139 | $cmd = "$command -j HEAD -u /method"; | ||
140 | $result = NPTest->testCmd( $cmd ); | ||
141 | is( $result->return_code, 0, $cmd); | ||
142 | like( $result->output, '/^HTTP OK HTTP/1.1 200 HEAD - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
143 | |||
144 | $cmd = "$command -j POST -u /method"; | ||
145 | $result = NPTest->testCmd( $cmd ); | ||
146 | is( $result->return_code, 0, $cmd); | ||
147 | like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
148 | |||
149 | $cmd = "$command -j GET -u /method"; | ||
150 | $result = NPTest->testCmd( $cmd ); | ||
151 | is( $result->return_code, 0, $cmd); | ||
152 | like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
153 | |||
154 | $cmd = "$command -u /method"; | ||
155 | $result = NPTest->testCmd( $cmd ); | ||
156 | is( $result->return_code, 0, $cmd); | ||
157 | like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
158 | |||
159 | $cmd = "$command -P foo -u /method"; | ||
160 | $result = NPTest->testCmd( $cmd ); | ||
161 | is( $result->return_code, 0, $cmd); | ||
162 | like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
163 | |||
164 | $cmd = "$command -j DELETE -u /method"; | ||
165 | $result = NPTest->testCmd( $cmd ); | ||
166 | is( $result->return_code, 1, $cmd); | ||
167 | like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output ); | ||
168 | |||
169 | $cmd = "$command -j foo -u /method"; | ||
170 | $result = NPTest->testCmd( $cmd ); | ||
171 | is( $result->return_code, 2, $cmd); | ||
172 | like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output ); | ||
173 | |||
174 | $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude"; | ||
175 | $result = NPTest->testCmd( $cmd ); | ||
176 | is( $result->return_code, 0, $cmd); | ||
177 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); | ||
178 | |||
179 | $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude"; | ||
180 | $result = NPTest->testCmd( $cmd ); | ||
181 | is( $result->return_code, 0, $cmd); | ||
182 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); | ||
183 | |||
184 | # To confirm that the free doesn't segfault | ||
185 | $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude"; | ||
186 | $result = NPTest->testCmd( $cmd ); | ||
187 | is( $result->return_code, 0, $cmd); | ||
188 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); | ||
189 | |||
190 | $cmd = "$command -u /redirect"; | ||
191 | $result = NPTest->testCmd( $cmd ); | ||
192 | is( $result->return_code, 0, $cmd); | ||
193 | like( $result->output, '/^HTTP OK - HTTP/1.1 301 Moved Permanently - [\d\.]+ second/', "Output correct: ".$result->output ); | ||
194 | |||
195 | $cmd = "$command -f follow -u /redirect"; | ||
196 | $result = NPTest->testCmd( $cmd ); | ||
197 | is( $result->return_code, 0, $cmd); | ||
198 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 183 bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
199 | |||
200 | $cmd = "$command -u /redirect -k 'follow: me'"; | ||
201 | $result = NPTest->testCmd( $cmd ); | ||
202 | is( $result->return_code, 0, $cmd); | ||
203 | like( $result->output, '/^HTTP OK - HTTP/1.1 301 Moved Permanently - [\d\.]+ second/', "Output correct: ".$result->output ); | ||
204 | |||
205 | $cmd = "$command -f follow -u /redirect -k 'follow: me'"; | ||
206 | $result = NPTest->testCmd( $cmd ); | ||
207 | is( $result->return_code, 0, $cmd); | ||
208 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 183 bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
209 | 142 | ||
143 | $result = NPTest->testCmd( "$command -u /file/root" ); | ||
144 | is( $result->return_code, 0, "/file/root"); | ||
145 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" ); | ||
146 | |||
147 | $result = NPTest->testCmd( "$command -u /file/root -s Root" ); | ||
148 | is( $result->return_code, 0, "/file/root search for string"); | ||
149 | TODO: { | ||
150 | local $TODO = "Output is different if a string is requested - should this be right?"; | ||
151 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" ); | ||
152 | } | ||
153 | |||
154 | |||
155 | $b = 177 + $add; | ||
156 | $result = NPTest->testCmd( "$command -u /slow" ); | ||
157 | is( $result->return_code, 0, "/file/root"); | ||
158 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct" ); | ||
159 | $result->output =~ /in ([\d\.]+) seconds/; | ||
160 | cmp_ok( $1, ">", 1, "Time is > 1 second" ); | ||
161 | |||
162 | my $cmd; | ||
163 | $b = 89 + $add; | ||
164 | $cmd = "$command -u /statuscode/200 -e 200"; | ||
165 | $result = NPTest->testCmd( $cmd ); | ||
166 | is( $result->return_code, 0, $cmd); | ||
167 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
168 | |||
169 | $b = 94 + $add; | ||
170 | $cmd = "$command -u /statuscode/201 -e 201"; | ||
171 | $result = NPTest->testCmd( $cmd ); | ||
172 | is( $result->return_code, 0, $cmd); | ||
173 | like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - '.$b.' bytes in ([\d\.]+) seconds /', "Output correct: ".$result->output ); | ||
174 | |||
175 | $cmd = "$command -u /statuscode/201 -e 200"; | ||
176 | $result = NPTest->testCmd( $cmd ); | ||
177 | is( $result->return_code, 2, $cmd); | ||
178 | like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output ); | ||
179 | |||
180 | $b = 89 + $add; | ||
181 | $cmd = "$command -u /statuscode/200 -e 200,201,202"; | ||
182 | $result = NPTest->testCmd( $cmd ); | ||
183 | is( $result->return_code, 0, $cmd); | ||
184 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
185 | |||
186 | $b = 94 + $add; | ||
187 | $cmd = "$command -u /statuscode/201 -e 200,201,202"; | ||
188 | $result = NPTest->testCmd( $cmd ); | ||
189 | is( $result->return_code, 0, $cmd); | ||
190 | like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
191 | |||
192 | $cmd = "$command -u /statuscode/203 -e 200,201,202"; | ||
193 | $result = NPTest->testCmd( $cmd ); | ||
194 | is( $result->return_code, 2, $cmd); | ||
195 | 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 ); | ||
196 | |||
197 | $b = 19 + $add; | ||
198 | $cmd = "$command -j HEAD -u /method"; | ||
199 | $result = NPTest->testCmd( $cmd ); | ||
200 | is( $result->return_code, 0, $cmd); | ||
201 | like( $result->output, '/^HTTP OK HTTP/1.1 200 HEAD - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
202 | |||
203 | $b = 19 + $add; | ||
204 | $cmd = "$command -j POST -u /method"; | ||
205 | $result = NPTest->testCmd( $cmd ); | ||
206 | is( $result->return_code, 0, $cmd); | ||
207 | like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
208 | |||
209 | $b = 18 + $add; | ||
210 | $cmd = "$command -j GET -u /method"; | ||
211 | $result = NPTest->testCmd( $cmd ); | ||
212 | is( $result->return_code, 0, $cmd); | ||
213 | like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
214 | |||
215 | $b = 18 + $add; | ||
216 | $cmd = "$command -u /method"; | ||
217 | $result = NPTest->testCmd( $cmd ); | ||
218 | is( $result->return_code, 0, $cmd); | ||
219 | like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
220 | |||
221 | $b = 19 + $add; | ||
222 | $cmd = "$command -P foo -u /method"; | ||
223 | $result = NPTest->testCmd( $cmd ); | ||
224 | is( $result->return_code, 0, $cmd); | ||
225 | like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
226 | |||
227 | $cmd = "$command -j DELETE -u /method"; | ||
228 | $result = NPTest->testCmd( $cmd ); | ||
229 | is( $result->return_code, 1, $cmd); | ||
230 | like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output ); | ||
231 | |||
232 | $cmd = "$command -j foo -u /method"; | ||
233 | $result = NPTest->testCmd( $cmd ); | ||
234 | is( $result->return_code, 2, $cmd); | ||
235 | like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output ); | ||
236 | |||
237 | $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude"; | ||
238 | $result = NPTest->testCmd( $cmd ); | ||
239 | is( $result->return_code, 0, $cmd); | ||
240 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); | ||
241 | |||
242 | $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude"; | ||
243 | $result = NPTest->testCmd( $cmd ); | ||
244 | is( $result->return_code, 0, $cmd); | ||
245 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); | ||
246 | |||
247 | # To confirm that the free doesn't segfault | ||
248 | $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude"; | ||
249 | $result = NPTest->testCmd( $cmd ); | ||
250 | is( $result->return_code, 0, $cmd); | ||
251 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); | ||
252 | |||
253 | $cmd = "$command -u /redirect"; | ||
254 | $result = NPTest->testCmd( $cmd ); | ||
255 | is( $result->return_code, 0, $cmd); | ||
256 | like( $result->output, '/^HTTP OK - HTTP/1.1 301 Moved Permanently - [\d\.]+ second/', "Output correct: ".$result->output ); | ||
257 | |||
258 | $b = 183 + $add; | ||
259 | $cmd = "$command -f follow -u /redirect"; | ||
260 | $result = NPTest->testCmd( $cmd ); | ||
261 | is( $result->return_code, 0, $cmd); | ||
262 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
263 | |||
264 | $cmd = "$command -u /redirect -k 'follow: me'"; | ||
265 | $result = NPTest->testCmd( $cmd ); | ||
266 | is( $result->return_code, 0, $cmd); | ||
267 | like( $result->output, '/^HTTP OK - HTTP/1.1 301 Moved Permanently - [\d\.]+ second/', "Output correct: ".$result->output ); | ||
268 | |||
269 | $b = 183 + $add; | ||
270 | $cmd = "$command -f follow -u /redirect -k 'follow: me'"; | ||
271 | $result = NPTest->testCmd( $cmd ); | ||
272 | is( $result->return_code, 0, $cmd); | ||
273 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
274 | |||
275 | } | ||