http-client.scm 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-http-client)
  19. #:use-module (guix http-client)
  20. #:use-module (guix tests http)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-34)
  23. #:use-module (srfi srfi-64)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (rnrs io ports)
  26. #:use-module (web response)
  27. #:use-module (web uri))
  28. (test-begin "http-client")
  29. (test-equal "http-fetch, one request, binary"
  30. (string->utf8 "Hello, world.")
  31. (with-http-server `((200 "Hello, world."))
  32. (let* ((port (http-fetch (%local-url)))
  33. (bv (get-bytevector-all port)))
  34. (close-port port)
  35. bv)))
  36. (test-equal "http-fetch, one request, text"
  37. "Hello, world."
  38. (with-http-server `((200 "Hello, world."))
  39. (let* ((port (http-fetch (%local-url) #:text? #t))
  40. (data (get-string-all port)))
  41. (close-port port)
  42. data)))
  43. (test-equal "http-fetch, redirect"
  44. "Hello, world."
  45. (with-http-server `((,(build-response
  46. #:code 301
  47. #:headers
  48. `((location
  49. . ,(string->uri-reference "/elsewhere")))
  50. #:reason-phrase "Moved")
  51. "Redirect!")
  52. (200 "Hello, world."))
  53. (let* ((port (http-fetch (%local-url)))
  54. (data (get-string-all port)))
  55. (close-port port)
  56. data)))
  57. (test-equal "http-fetch, error"
  58. 404
  59. (with-http-server `((404 "Ne trovita."))
  60. (guard (c ((http-get-error? c) (http-get-error-code c)))
  61. (http-fetch (%local-url))
  62. #f)))
  63. (test-equal "http-fetch, redirect + error"
  64. 403
  65. (with-http-server `((,(build-response
  66. #:code 302
  67. #:headers
  68. `((location
  69. . ,(string->uri-reference "/elsewhere")))
  70. #:reason-phrase "Moved")
  71. "Redirect!")
  72. (403 "Verboten."))
  73. (guard (c ((http-get-error? c) (http-get-error-code c)))
  74. (http-fetch (%local-url))
  75. #f)))
  76. (test-end "http-client")