guile-jsonld/test-modules/download.scm

download.scm

1
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
2
;;;; 
3
;;;; This library is free software; you can redistribute it and/or
4
;;;; modify it under the terms of the GNU Lesser General Public
5
;;;; License as published by the Free Software Foundation; either
6
;;;; version 3 of the License, or (at your option) any later version.
7
;;;; 
8
;;;; This library 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 GNU
11
;;;; Lesser General Public License for more details.
12
;;;; 
13
;;;; You should have received a copy of the GNU Lesser General Public
14
;;;; License along with this library; if not, write to the Free Software
15
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
;;;; 
17
18
(define-module (test-modules download)
19
  #:use-module (jsonld download)
20
  #:use-module (jsonld iri)
21
  #:use-module (jsonld json)
22
  #:use-module (jsonld memoization)
23
  #:use-module (jsonld options)
24
  #:use-module (nquads tordf)
25
  #:use-module (rnrs bytevectors)
26
  #:use-module (test-modules testsuite)
27
  #:use-module (web client)
28
  #:use-module (web response)
29
  #:use-module (web uri)
30
  #:export (test-http-get get-test-document-loader download-nq))
31
32
(define* (update-response response
33
                          #:key
34
                          (version           (response-version response))
35
                          (code              (response-code response))
36
                          (reason-phrase     (response-reason-phrase response))
37
                          (headers           (response-headers response))
38
                          (port              (response-port response))
39
                          (validate-headers? #t))
40
  (build-response
41
    #:version version
42
    #:code code
43
    #:reason-phrase reason-phrase
44
    #:headers headers
45
    #:port port
46
    #:validate-headers? validate-headers?))
47
48
(define (test-http-get options)
49
  "Return a procedure that downloads a file, and modifies the response according
50
to @var{options}, an alist of parameters, as described in the test suite
51
specification."
52
  (lambda* (uri #:key (headers '()))
53
    (call-with-values
54
      (lambda ()
55
        (http-get uri #:headers headers))
56
      (lambda (hdr body)
57
        (when (json-has-key? options "contentType")
58
          (let ((type (assoc-ref options "contentType")))
59
            (set! type (list (string->symbol type)))
60
            (set! hdr
61
              (update-response hdr #:headers (alist-set (response-headers hdr)
62
                                                      'content-type type)))))
63
        (when (json-has-key? options "httpLink")
64
          ;; first remove http link headers
65
          (let loop ((headers (response-headers hdr)))
66
            (if (assoc-ref headers 'link)
67
                (loop (alist-remove headers 'link))
68
                (set! hdr (update-response hdr #:headers headers))))
69
          ;; then add our own
70
          (let* ((links (assoc-ref options "httpLink"))
71
                 (links (if (json-array? links)
72
                            (array->list links)
73
                            (list links))))
74
            (let loop ((headers (response-headers hdr))
75
                       (links links))
76
              (if (null? links)
77
                  (set! hdr (update-response hdr #:headers headers))
78
                  (loop (cons (cons 'link (car links)) headers)
79
                        (cdr links))))))
80
        (when (json-has-key? options "redirectTo")
81
          (let* ((location (assoc-ref options "redirectTo"))
82
                 (location (resolve-iri jsonld-test-url location)))
83
            (set! hdr
84
              (update-response hdr #:headers (alist-set (response-headers hdr)
85
                                               'location (string->uri location))))))
86
        (when (json-has-key? options "httpStatus")
87
          (set! hdr
88
            (update-response hdr #:code (assoc-ref options "httpStatus"))))
89
        (values hdr body)))))
90
91
(define (get-test-document-loader options)
92
  "Return a procedure that cane be used as the document loader in the
93
jsonld-options structure."
94
  (memoize
95
    (lambda args
96
      (apply download-json
97
             (append args `(#:http-get ,(test-http-get options)))))))
98
99
(define (download-nq url)
100
  (call-with-values
101
    (lambda ()
102
      (http-get url))
103
    (lambda (hdr body)
104
      (if (equal? (response-code hdr) 200)
105
          (nquads->rdf
106
            (if (string? body)
107
                body
108
                (utf8->string body)))
109
          (throw 'download-error (response-code hdr) url)))))
110