check_http.t 3.9 KB

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