| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 |
|
| 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 |
|
| 30 | (test-begin "http-client")
|
| 31 |
|
| 32 | (test-equal "http-fetch, one request, binary"
|
| 33 | (string->utf8 "Hello, world.")
|
| 34 | (with-http-server `((200 "Hello, world."))
|
| 35 | (let* ((port (http-fetch (%local-url)))
|
| 36 | (bv (get-bytevector-all port)))
|
| 37 | (close-port port)
|
| 38 | bv)))
|
| 39 |
|
| 40 | (test-equal "http-fetch, one request, text"
|
| 41 | "Hello, world."
|
| 42 | (with-http-server `((200 "Hello, world."))
|
| 43 | (let* ((port (http-fetch (%local-url) #:text? #t))
|
| 44 | (data (get-string-all port)))
|
| 45 | (close-port port)
|
| 46 | data)))
|
| 47 |
|
| 48 | (test-equal "http-fetch, redirect"
|
| 49 | "Hello, world."
|
| 50 | (with-http-server `((,(build-response
|
| 51 | #:code 301
|
| 52 | #:headers
|
| 53 | `((location
|
| 54 | . ,(string->uri-reference "/elsewhere")))
|
| 55 | #:reason-phrase "Moved")
|
| 56 | "Redirect!")
|
| 57 | (200 "Hello, world."))
|
| 58 | (let* ((port (http-fetch (%local-url)))
|
| 59 | (data (get-string-all port)))
|
| 60 | (close-port port)
|
| 61 | data)))
|
| 62 |
|
| 63 | (test-equal "http-fetch, error"
|
| 64 | 404
|
| 65 | (with-http-server `((404 "Ne trovita."))
|
| 66 | (guard (c ((http-get-error? c) (http-get-error-code c)))
|
| 67 | (http-fetch (%local-url))
|
| 68 | #f)))
|
| 69 |
|
| 70 | (test-equal "http-fetch, redirect + error"
|
| 71 | 403
|
| 72 | (with-http-server `((,(build-response
|
| 73 | #:code 302
|
| 74 | #:headers
|
| 75 | `((location
|
| 76 | . ,(string->uri-reference "/elsewhere")))
|
| 77 | #:reason-phrase "Moved")
|
| 78 | "Redirect!")
|
| 79 | (403 "Verboten."))
|
| 80 | (guard (c ((http-get-error? c) (http-get-error-code c)))
|
| 81 | (http-fetch (%local-url))
|
| 82 | #f)))
|
| 83 |
|
| 84 | (test-end "http-client")
|