check_http.t 3.8 KB

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