aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (test-http-client)
  #:use-module (guix http-client)
  #:use-module (guix tests http)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-64)
  #:use-module (rnrs bytevectors)
  #:use-module (rnrs io ports)
  #:use-module (web response)
  #:use-module (web uri))

(test-begin "http-client")

(test-equal "http-fetch, one request, binary"
  (string->utf8 "Hello, world.")
  (with-http-server `((200 "Hello, world."))
    (let* ((port (http-fetch (%local-url)))
           (bv   (get-bytevector-all port)))
      (close-port port)
      bv)))

(test-equal "http-fetch, one request, text"
  "Hello, world."
  (with-http-server `((200 "Hello, world."))
    (let* ((port (http-fetch (%local-url) #:text? #t))
           (data (get-string-all port)))
      (close-port port)
      data)))

(test-equal "http-fetch, redirect"
  "Hello, world."
  (with-http-server `((,(build-response
                         #:code 301
                         #:headers
                         `((location
                            . ,(string->uri-reference "/elsewhere")))
                         #:reason-phrase "Moved")
                       "Redirect!")
                      (200 "Hello, world."))
    (let* ((port (http-fetch (%local-url)))
           (data (get-string-all port)))
      (close-port port)
      data)))

(test-equal "http-fetch, error"
  404
  (with-http-server `((404 "Ne trovita."))
    (guard (c ((http-get-error? c) (http-get-error-code c)))
      (http-fetch (%local-url))
      #f)))

(test-equal "http-fetch, redirect + error"
  403
  (with-http-server `((,(build-response
                         #:code 302
                         #:headers
                         `((location
                            . ,(string->uri-reference "/elsewhere")))
                         #:reason-phrase "Moved")
                       "Redirect!")
                      (403 "Verboten."))
    (guard (c ((http-get-error? c) (http-get-error-code c)))
      (http-fetch (%local-url))
      #f)))

(test-end "http-client")
2018-01-21 00:24:03 +0100'>2018-01-21services: Missing services are automatically instantiated....Ludovic Courtès 2017-12-17services: cleanup: Remove "/run/udev/watch.old" directory....Danny Milosavljevic 2017-11-08services: Add 'lookup-service-types'....Ludovic Courtès 2017-11-08services: 'fold-service-types' includes (gnu services)....Ludovic Courtès 2017-11-08services: 'fold-service-types' honors its seed....Ludovic Courtès 2017-10-22gexp: Add 'directory-union'....Ludovic Courtès 2017-10-22gexp: Add 'file-union'....Ludovic Courtès 2017-10-12services: cleanup: Remove Shadow lock files from /etc....Ludovic Courtès 2017-09-22services: network-manager: Add support for VPN plug-ins....Ludovic Courtès 2017-09-16services: Add 'fold-service-types'....Ludovic Courtès 2017-09-16services: Add a description and location for each service type....Ludovic Courtès 2017-07-11services: Make error message less scary....Ludovic Courtès 2017-05-03ui: Rename '_' to 'G_'....Ludovic Courtès 2017-04-16services: Service types can now specify a default value for instances....Ludovic Courtès 2017-04-16services: 'service-parameters' becomes 'service-value'....Ludovic Courtès 2017-04-13services: Define '%linux-bare-metal-service' using 'simple-service'....Ludovic Courtès 2017-03-10services: Create /var/log upon activation....Christopher Baines 2017-02-08services: Add 'special-files-service-type'....Ludovic Courtès 2017-01-24services: Create /var/log/wtmp upon activation....Ludovic Courtès 2017-01-19services: Create /var/run/utmpx upon activation....Ludovic Courtès 2017-01-16services: Export 'service-extension' procedures....Christopher Baines 2016-12-11services: Activate system prior to services....Christopher Baines 2016-09-20services: Use 'source-module-closure' for (gnu build activation)....Ludovic Courtès 2016-09-19services: Add 'simple-service'....Ludovic Courtès 2016-07-12gnu: Use 'gexp->file' in conjunction with 'with-imported-modules'....Ludovic Courtès 2016-07-12gnu: Switch to 'with-imported-modules'....Ludovic Courtès 2016-06-20services: Add 'gc-root-service-type'....Ludovic Courtès 2016-03-15doc: Clarify and consolidate modify-services documentation....Chris Marusich 2016-03-07services: Use 'packages->manifest' in 'packages->profile-entry'....宋文武 2016-02-08file-systems: Spawn a Bournish REPL upon fsck failure....Ludovic Courtès 2016-02-03services: 'file-union' makes sure each source file exists....Ludovic Courtès 2016-01-29services: Rename 'dmd' services to 'shepherd'....Alex Kost 2016-01-06services: cleanup-service: Catch 'system-error' instead of everything....Ludovic Courtès 2016-01-05services: Move /tmp cleanup to a separate service....Ludovic Courtès 2016-01-05services: boot: Reinstate /tmp and /var/run deletion....Ludovic Courtès