check_http.t 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # HyperText Transfer Protocol (HTTP) Test via check_http
  4. #
  5. #
  6. use strict;
  7. use Test::More;
  8. use NPTest;
  9. plan tests => 27;
  10. my $successOutput = '/OK.*HTTP.*second/';
  11. my $res;
  12. my $host_tcp_http = getTestParameter( "NP_HOST_TCP_HTTP",
  13. "A host providing the HTTP Service (a web server)",
  14. "localhost" );
  15. my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
  16. "The hostname of system not responsive to network requests",
  17. "10.0.0.1" );
  18. my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID",
  19. "An invalid (not known to DNS) hostname",
  20. "nosuchhost");
  21. my $internet_access = getTestParameter( "NP_INTERNET_ACCESS",
  22. "Is this system directly connected to the internet?",
  23. "yes");
  24. my $host_tcp_http2;
  25. if ($internet_access eq "no") {
  26. $host_tcp_http2 = getTestParameter( "NP_HOST_TCP_HTTP2",
  27. "A host providing an index page containing the string 'nagios'",
  28. "www.nagios.com" );
  29. }
  30. $res = NPTest->testCmd(
  31. "./check_http $host_tcp_http -wt 300 -ct 600"
  32. );
  33. cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" );
  34. like( $res->output, $successOutput, "Output OK" );
  35. $res = NPTest->testCmd(
  36. "./check_http $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'"
  37. );
  38. like( $res->output, '/bob:there\r\ncarl:frown\r\n/', "Got headers with multiple -k options" );
  39. $res = NPTest->testCmd(
  40. "./check_http $host_nonresponsive -wt 1 -ct 2"
  41. );
  42. cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" );
  43. cmp_ok( $res->output, 'eq', "CRITICAL - Socket timeout after 10 seconds", "Output OK");
  44. $res = NPTest->testCmd(
  45. "./check_http $hostname_invalid -wt 1 -ct 2"
  46. );
  47. cmp_ok( $res->return_code, '==', 2, "Webserver $hostname_invalid not valid" );
  48. # The first part of the message comes from the OS catalogue, so cannot check this.
  49. # On Debian, it is Name or service not known, on Darwin, it is No address associated with nodename
  50. # Is also possible to get a socket timeout if DNS is not responding fast enough
  51. like( $res->output, "/Unable to open TCP socket|Socket timeout after/", "Output OK");
  52. SKIP: {
  53. skip "No internet access and no host serving nagios in index file",
  54. 7 if $internet_access eq "no" && ! $host_tcp_http2;
  55. $host_tcp_http2 = "www.nagios.com" if (! $host_tcp_http2);
  56. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nagios'" );
  57. cmp_ok( $res->return_code, "==", 0, "Got a reference to 'nagios'");
  58. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nAGiOs'" );
  59. cmp_ok( $res->return_code, "==", 2, "Not got 'nAGiOs'");
  60. like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'");
  61. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -R 'nAGiOs'" );
  62. cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'nAGiOs'");
  63. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nagios' --invert-regex" );
  64. cmp_ok( $res->return_code, "==", 2, "Invert results work when found");
  65. like ( $res->output, "/pattern found/", "Error message says 'pattern found'");
  66. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nAGiOs' --invert-regex" );
  67. cmp_ok( $res->return_code, "==", 0, "And also when not found");
  68. }
  69. SKIP: {
  70. skip "No internet access", 11 if $internet_access eq "no";
  71. $res = NPTest->testCmd(
  72. "./check_http --ssl www.verisign.com"
  73. );
  74. cmp_ok( $res->return_code, '==', 0, "Can read https for www.verisign.com" );
  75. $res = NPTest->testCmd( "./check_http -C 1 --ssl www.verisign.com" );
  76. cmp_ok( $res->return_code, '==', 0, "Checking certificate for www.verisign.com");
  77. like ( $res->output, "/Certificate 'www.verisign.com' will expire on/", "Output OK" );
  78. my $saved_cert_output = $res->output;
  79. $res = NPTest->testCmd( "./check_http -C 8000,1 --ssl www.verisign.com" );
  80. cmp_ok( $res->return_code, '==', 1, "Checking certificate for www.verisign.com");
  81. like ( $res->output, qr/WARNING - Certificate 'www.verisign.com' expires in \d+ day/, "Output Warning" );
  82. $res = NPTest->testCmd( "./check_http www.verisign.com -C 1" );
  83. is( $res->return_code, 0, "Old syntax for cert checking okay" );
  84. is( $res->output, $saved_cert_output, "Same output as new syntax" );
  85. $res = NPTest->testCmd( "./check_http -H www.verisign.com -C 1" );
  86. is( $res->return_code, 0, "Updated syntax for cert checking okay" );
  87. is( $res->output, $saved_cert_output, "Same output as new syntax" );
  88. $res = NPTest->testCmd( "./check_http -C 1 www.verisign.com" );
  89. cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added");
  90. $res = NPTest->testCmd( "./check_http www.verisign.com -C 1" );
  91. cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works");
  92. $res = NPTest->testCmd(
  93. "./check_http --ssl www.e-paycobalt.com"
  94. );
  95. cmp_ok( $res->return_code, "==", 0, "Can read https for www.e-paycobalt.com (uses AES certificate)" );
  96. $res = NPTest->testCmd( "./check_http -H www.mozilla.com -u /firefox -f follow" );
  97. is( $res->return_code, 0, "Redirection based on location is okay");
  98. }