check_http.t 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 =~ 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. } elsif ($r->url->path eq "/method") {
  43. if ($r->method eq "DELETE") {
  44. $c->send_error(RC_METHOD_NOT_ALLOWED);
  45. } elsif ($r->method eq "foo") {
  46. $c->send_error(RC_NOT_IMPLEMENTED);
  47. } else {
  48. $c->send_status_line(200, $r->method);
  49. }
  50. } elsif ($r->url->path eq "/postdata") {
  51. $c->send_basic_header;
  52. $c->send_crlf;
  53. $c->send_response($r->method.":".$r->content);
  54. } else {
  55. $c->send_error(RC_FORBIDDEN);
  56. }
  57. $c->close;
  58. }
  59. }
  60. exit;
  61. }
  62. END { if ($pid) { print "Killing $pid\n"; kill "INT", $pid } };
  63. if ($ARGV[0] && $ARGV[0] eq "-d") {
  64. sleep 1000;
  65. }
  66. if (-x "./check_http") {
  67. plan tests => 39;
  68. } else {
  69. plan skip_all => "No check_http compiled";
  70. }
  71. my $result;
  72. my $command = "./check_http -H 127.0.0.1 -p $port";
  73. $result = NPTest->testCmd( "$command -u /file/root" );
  74. is( $result->return_code, 0, "/file/root");
  75. like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" );
  76. $result = NPTest->testCmd( "$command -u /file/root -s Root" );
  77. is( $result->return_code, 0, "/file/root search for string");
  78. TODO: {
  79. local $TODO = "Output is different if a string is requested - should this be right?";
  80. like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" );
  81. }
  82. $result = NPTest->testCmd( "$command -u /slow" );
  83. is( $result->return_code, 0, "/file/root");
  84. like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 177 bytes in ([\d\.]+) seconds/', "Output correct" );
  85. $result->output =~ /in ([\d\.]+) seconds/;
  86. cmp_ok( $1, ">", 1, "Time is > 1 second" );
  87. my $cmd;
  88. $cmd = "$command -u /statuscode/200 -e 200";
  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 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. $cmd = "$command -u /statuscode/201 -e 200";
  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 201 Created/', "Output correct: ".$result->output );
  100. $cmd = "$command -u /statuscode/200 -e 200,201,202";
  101. $result = NPTest->testCmd( $cmd );
  102. is( $result->return_code, 0, $cmd);
  103. like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
  104. $cmd = "$command -u /statuscode/201 -e 200,201,202";
  105. $result = NPTest->testCmd( $cmd );
  106. is( $result->return_code, 0, $cmd);
  107. like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
  108. $cmd = "$command -u /statuscode/203 -e 200,201,202";
  109. $result = NPTest->testCmd( $cmd );
  110. is( $result->return_code, 2, $cmd);
  111. 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 );
  112. $cmd = "$command -j HEAD -u /method";
  113. $result = NPTest->testCmd( $cmd );
  114. is( $result->return_code, 0, $cmd);
  115. like( $result->output, '/^HTTP OK HTTP/1.1 200 HEAD - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
  116. $cmd = "$command -j POST -u /method";
  117. $result = NPTest->testCmd( $cmd );
  118. is( $result->return_code, 0, $cmd);
  119. like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
  120. $cmd = "$command -j GET -u /method";
  121. $result = NPTest->testCmd( $cmd );
  122. is( $result->return_code, 0, $cmd);
  123. like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
  124. $cmd = "$command -u /method";
  125. $result = NPTest->testCmd( $cmd );
  126. is( $result->return_code, 0, $cmd);
  127. like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
  128. $cmd = "$command -P foo -u /method";
  129. $result = NPTest->testCmd( $cmd );
  130. is( $result->return_code, 0, $cmd);
  131. like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
  132. $cmd = "$command -j DELETE -u /method";
  133. $result = NPTest->testCmd( $cmd );
  134. is( $result->return_code, 1, $cmd);
  135. like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
  136. $cmd = "$command -j foo -u /method";
  137. $result = NPTest->testCmd( $cmd );
  138. is( $result->return_code, 2, $cmd);
  139. like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
  140. $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
  141. $result = NPTest->testCmd( $cmd );
  142. is( $result->return_code, 0, $cmd);
  143. like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output );
  144. $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
  145. $result = NPTest->testCmd( $cmd );
  146. is( $result->return_code, 0, $cmd);
  147. like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output );
  148. # To confirm that the free doesn't segfault
  149. $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
  150. $result = NPTest->testCmd( $cmd );
  151. is( $result->return_code, 0, $cmd);
  152. like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output );