http-client.scm 3.0 KB

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