check_http.t 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 => 30;
  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 = getTestParameter( "NP_HOST_TCP_HTTP2",
  25. "A host providing an index page containing the string 'nagios'",
  26. "nagios.org" );
  27. $res = NPTest->testCmd(
  28. "./check_http $host_tcp_http -wt 300 -ct 600"
  29. );
  30. cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" );
  31. like( $res->output, $successOutput, "Output OK" );
  32. $res = NPTest->testCmd(
  33. "./check_http $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'"
  34. );
  35. like( $res->output, '/bob:there\r\ncarl:frown\r\n/', "Got headers with multiple -k options" );
  36. $res = NPTest->testCmd(
  37. "./check_http $host_nonresponsive -wt 1 -ct 2"
  38. );
  39. cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" );
  40. cmp_ok( $res->output, 'eq', "CRITICAL - Socket timeout after 10 seconds", "Output OK");
  41. $res = NPTest->testCmd(
  42. "./check_http $hostname_invalid -wt 1 -ct 2"
  43. );
  44. cmp_ok( $res->return_code, '==', 2, "Webserver $hostname_invalid not valid" );
  45. # The first part of the message comes from the OS catalogue, so cannot check this.
  46. # On Debian, it is Name or service not known, on Darwin, it is No address associated with nodename
  47. # Is also possible to get a socket timeout if DNS is not responding fast enough
  48. like( $res->output, "/Unable to open TCP socket|Socket timeout after/", "Output OK");
  49. SKIP: {
  50. skip "No host serving nagios in index file", 7 unless $host_tcp_http2;
  51. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nagios'" );
  52. cmp_ok( $res->return_code, "==", 0, "Got a reference to 'nagios'");
  53. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nAGiOs'" );
  54. cmp_ok( $res->return_code, "==", 2, "Not got 'nAGiOs'");
  55. like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'");
  56. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -R 'nAGiOs'" );
  57. cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'nAGiOs'");
  58. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nagios' --invert-regex" );
  59. cmp_ok( $res->return_code, "==", 2, "Invert results work when found");
  60. like ( $res->output, "/pattern found/", "Error message says 'pattern found'");
  61. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nAGiOs' --invert-regex" );
  62. cmp_ok( $res->return_code, "==", 0, "And also when not found");
  63. }
  64. SKIP: {
  65. skip "No internet access", 16 if $internet_access eq "no";
  66. $res = NPTest->testCmd(
  67. "./check_http --ssl www.verisign.com"
  68. );
  69. cmp_ok( $res->return_code, '==', 0, "Can read https for www.verisign.com" );
  70. $res = NPTest->testCmd( "./check_http -C 1 --ssl www.verisign.com" );
  71. cmp_ok( $res->return_code, '==', 0, "Checking certificate for www.verisign.com");
  72. like ( $res->output, "/Certificate 'www.verisign.com' will expire on/", "Output OK" );
  73. my $saved_cert_output = $res->output;
  74. $res = NPTest->testCmd( "./check_http -C 8000,1 --ssl www.verisign.com" );
  75. cmp_ok( $res->return_code, '==', 1, "Checking certificate for www.verisign.com");
  76. like ( $res->output, qr/WARNING - Certificate 'www.verisign.com' expires in \d+ day/, "Output Warning" );
  77. $res = NPTest->testCmd( "./check_http www.verisign.com -C 1" );
  78. is( $res->return_code, 0, "Old syntax for cert checking okay" );
  79. is( $res->output, $saved_cert_output, "Same output as new syntax" );
  80. $res = NPTest->testCmd( "./check_http -H www.verisign.com -C 1" );
  81. is( $res->return_code, 0, "Updated syntax for cert checking okay" );
  82. is( $res->output, $saved_cert_output, "Same output as new syntax" );
  83. $res = NPTest->testCmd( "./check_http -C 1 www.verisign.com" );
  84. cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added");
  85. $res = NPTest->testCmd( "./check_http www.verisign.com -C 1" );
  86. cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works");
  87. $res = NPTest->testCmd( "./check_http --ssl www.verisign.com -E" );
  88. like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' );
  89. like ( $res->output, '/time_ssl=[\d\.]+/', 'Extended Performance Data SSL Output OK' );
  90. $res = NPTest->testCmd(
  91. "./check_http --ssl www.e-paycobalt.com"
  92. );
  93. cmp_ok( $res->return_code, "==", 0, "Can read https for www.e-paycobalt.com (uses AES certificate)" );
  94. $res = NPTest->testCmd( "./check_http -H www.mozilla.com -u /firefox -f follow" );
  95. is( $res->return_code, 0, "Redirection based on location is okay");
  96. $res = NPTest->testCmd( "./check_http -H www.mozilla.com --extended-perfdata" );
  97. like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' );
  98. }