Small sh "framework" to test some server responses
sh
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

http.sh 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #Copyright (C) 2016,2023 Weber Yann
  2. #
  3. #This program is free software; you can redistribute it and/or modify
  4. #it under the terms of the GNU General Public License as published by
  5. #the Free Software Foundation; either version 3 of the License, or
  6. #any later version.
  7. #
  8. #This program is distributed in the hope that it will be useful,
  9. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. #GNU General Public License for more details.
  12. #
  13. #You should have received a copy of the GNU General Public License
  14. #along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #
  16. # HTTP/HTTPS tests
  17. #
  18. _check_http_status() {
  19. # $1 url
  20. # $2 status
  21. # $* curl options
  22. url=$1
  23. shift 1
  24. expt=$1
  25. if [ -z "$1" ]
  26. then
  27. expt=200
  28. fi
  29. shift 1
  30. status=$(curl -s -o /dev/null -w "%{http_code}" "$@" "$url")
  31. if [ "$status" -ne "$expt" ]
  32. then
  33. fail "Check http status $expt for $url : $status returned"
  34. elif [ "$verbose" -gt 0 ]
  35. then
  36. success "Check http status $status for $url"
  37. fi
  38. }
  39. check_http_status() {
  40. # $1 url
  41. # $2 status
  42. _check_http_status "$1" "$2"
  43. }
  44. check_http_200() {
  45. _check_http_status "$1" 200
  46. }
  47. check_https_cert() {
  48. # Check that SSL Cert is valid
  49. # $1 URL
  50. status=$(curl -s -o /dev/null -w "%{http_code}" "https://$1")
  51. rep=$?
  52. if [ "$rep" -eq 0 ]
  53. then
  54. success "https://$1 cert verified"
  55. return
  56. fi
  57. status=$(curl -k -s -o /dev/null -q "%{http_code}" "https://$1")
  58. rep=$?
  59. if [ "$rep" -eq 0 ]
  60. then
  61. fail "https://$1 cert invalid"
  62. else
  63. err "Unable to curl https://$1"
  64. fi
  65. }
  66. check_html_title() {
  67. url="$1"
  68. expt="$2"
  69. tmpxsl=$(mktemp -t XSL.XXXXXXXXXXX)
  70. echo '<?xml version="1.0"?>
  71. <xsl:stylesheet version="1.0"
  72. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  73. <xsl:output method = "text"/>
  74. <xsl:template match="/">
  75. <xsl:value-of select="/html/head/title"/>
  76. </xsl:template>
  77. </xsl:stylesheet>' > "$tmpxsl"
  78. tmphtml=$(mktemp -t html.XXXXXXXXX)
  79. curl --silent "$url" > "$tmphtml"
  80. if title=$(xsltproc --html --novalid "$tmpxsl" "$tmphtml" 2>/dev/null)
  81. then
  82. :
  83. else
  84. title=$(xsltproc --novalid "$tmpxsl" "$tmphtml" 2>/dev/null)
  85. fi
  86. if [ "$title" = "$expt" ]
  87. then
  88. success "$url HTML title is '$expt'"
  89. else
  90. fail "$url HTML title is '$title' but '$expt' expected"
  91. fi
  92. rm "$tmpxsl" "$tmphtml" 2>/dev/null
  93. }