check_http.t 2.9 KB

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