aboutsummaryrefslogtreecommitdiff
path: root/tests/testutil-gloss-fetch.el
blob: a144d91e360abbdfa4d1e2e0fc9e67fbbce5c9cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
;;; testutil-gloss-fetch.el --- Test helpers for gloss-fetch -*- lexical-binding: t -*-

;; SPDX-License-Identifier: GPL-3.0-or-later

;;; Commentary:

;; Helpers for tests that exercise `gloss-fetch'.  The boundary mock is
;; `url-retrieve-synchronously', so these helpers build response buffers
;; in the shape Emacs's url library returns: status line, blank line,
;; body.  The body comes from a JSON fixture loaded via
;; `gloss-test--load-wiktionary-fixture' (provided by testutil-gloss.el
;; on a parallel branch).

;;; Code:

(defun gloss-fetch-test--make-response-buffer (status-line body)
  "Return a fresh buffer containing STATUS-LINE, a blank line, and BODY.
STATUS-LINE is an HTTP status line such as \"HTTP/1.1 200 OK\".  BODY
is the response body as a string.  The buffer is unibyte so that
multibyte handling is exercised end-to-end."
  (let ((buf (generate-new-buffer " *gloss-fetch-test-response*")))
    (with-current-buffer buf
      (set-buffer-multibyte nil)
      (insert status-line "\n")
      (insert "Content-Type: application/json\n")
      (insert "\n")
      (insert (encode-coding-string (or body "") 'utf-8)))
    buf))

(defmacro gloss-fetch-test--with-mocked-url (response-fn &rest body)
  "Run BODY with `url-retrieve-synchronously' replaced by RESPONSE-FN.
RESPONSE-FN takes the URL string and returns either a buffer (the
response) or nil (to simulate timeout / unreachable)."
  (declare (indent 1) (debug t))
  `(cl-letf (((symbol-function 'url-retrieve-synchronously)
              (lambda (url &rest _args) (funcall ,response-fn url))))
     ,@body))

(defun gloss-fetch-test--ok-response (body)
  "Return a 200 OK response buffer with BODY."
  (gloss-fetch-test--make-response-buffer "HTTP/1.1 200 OK" body))

(defun gloss-fetch-test--status-response (status-line &optional body)
  "Return a response buffer with STATUS-LINE and optional BODY (default empty)."
  (gloss-fetch-test--make-response-buffer status-line (or body "")))

(provide 'testutil-gloss-fetch)
;;; testutil-gloss-fetch.el ends here