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 (test-modules testsuite) |
| 25 | #:use-module (web client) |
| 26 | #:use-module (web response) |
| 27 | #:use-module (web uri) |
| 28 | #:export (test-http-get get-test-document-loader)) |
| 29 | |
| 30 | (define* (update-response response |
| 31 | #:key |
| 32 | (version (response-version response)) |
| 33 | (code (response-code response)) |
| 34 | (reason-phrase (response-reason-phrase response)) |
| 35 | (headers (response-headers response)) |
| 36 | (port (response-port response)) |
| 37 | (validate-headers? #t)) |
| 38 | (build-response |
| 39 | #:version version |
| 40 | #:code code |
| 41 | #:reason-phrase reason-phrase |
| 42 | #:headers headers |
| 43 | #:port port |
| 44 | #:validate-headers? validate-headers?)) |
| 45 | |
| 46 | (define (test-http-get options) |
| 47 | "Return a procedure that downloads a file, and modifies the response according |
| 48 | to @var{options}, an alist of parameters, as described in the test suite |
| 49 | specification." |
| 50 | (lambda* (uri #:key (headers '())) |
| 51 | (call-with-values |
| 52 | (lambda () |
| 53 | (http-get uri #:headers headers)) |
| 54 | (lambda (hdr body) |
| 55 | (when (json-has-key? options "contentType") |
| 56 | (let ((type (assoc-ref options "contentType"))) |
| 57 | (set! type (list (string->symbol type))) |
| 58 | (set! hdr |
| 59 | (update-response hdr #:headers (alist-set (response-headers hdr) |
| 60 | 'content-type type))))) |
| 61 | (when (json-has-key? options "httpLink") |
| 62 | ;; first remove http link headers |
| 63 | (let loop ((headers (response-headers hdr))) |
| 64 | (if (assoc-ref headers 'link) |
| 65 | (loop (alist-remove headers 'link)) |
| 66 | (set! hdr (update-response hdr #:headers headers)))) |
| 67 | ;; then add our own |
| 68 | (let* ((links (assoc-ref options "httpLink")) |
| 69 | (links (if (json-array? links) |
| 70 | (array->list links) |
| 71 | (list links)))) |
| 72 | (let loop ((headers (response-headers hdr)) |
| 73 | (links links)) |
| 74 | (if (null? links) |
| 75 | (set! hdr (update-response hdr #:headers headers)) |
| 76 | (loop (cons (cons 'link (car links)) headers) |
| 77 | (cdr links)))))) |
| 78 | (when (json-has-key? options "redirectTo") |
| 79 | (let* ((location (assoc-ref options "redirectTo")) |
| 80 | (location (resolve-iri jsonld-test-url location))) |
| 81 | (set! hdr |
| 82 | (update-response hdr #:headers (alist-set (response-headers hdr) |
| 83 | 'location (string->uri location)))))) |
| 84 | (when (json-has-key? options "httpStatus") |
| 85 | (set! hdr |
| 86 | (update-response hdr #:code (assoc-ref options "httpStatus")))) |
| 87 | (values hdr body))))) |
| 88 | |
| 89 | (define (get-test-document-loader options) |
| 90 | "Return a procedure that cane be used as the document loader in the |
| 91 | jsonld-options structure." |
| 92 | (memoize |
| 93 | (lambda args |
| 94 | (apply download-json |
| 95 | (append args `(#:http-get ,(test-http-get options))))))) |
| 96 |