blob: a517bb69c5f94e0d108b23c50527fdf6811ead0f (
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
49
|
;;; testutil-request.el --- request mocking helpers for pearl tests -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Craig Jennings
;;; Commentary:
;; Shared helpers for stubbing the `request' library at the HTTP boundary.
;; A stub invokes the package's own :success or :error callback synchronously
;; with canned, json-read-shaped data, so the response-parsing and callback
;; logic runs for real without any network. Not a test file itself (no
;; test- prefix), so the suite runner ignores it; test files `require' it.
;;; Code:
(require 'cl-lib)
(defun testutil-linear-request-success (data)
"Return a `request' replacement that invokes its :success callback with DATA."
(lambda (_url &rest args)
(let ((cb (plist-get args :success)))
(when cb (funcall cb :data data)))))
(defun testutil-linear-request-error (msg)
"Return a `request' replacement that invokes its :error callback with MSG.
The :response carries a real `request-response' struct, matching what the
live library passes, so the package's status-code logging doesn't choke."
(lambda (_url &rest args)
(let ((cb (plist-get args :error)))
(when cb
(funcall cb :error-thrown msg
:response (make-request-response :status-code 500)
:data nil)))))
(defmacro testutil-linear-with-response (data &rest body)
"Run BODY with `request' stubbed to succeed with DATA and an API key set."
(declare (indent 1))
`(let ((pearl-api-key "test-key"))
(cl-letf (((symbol-function 'request) (testutil-linear-request-success ,data)))
,@body)))
(defmacro testutil-linear-with-error (msg &rest body)
"Run BODY with `request' stubbed to fail with MSG and an API key set."
(declare (indent 1))
`(let ((pearl-api-key "test-key"))
(cl-letf (((symbol-function 'request) (testutil-linear-request-error ,msg)))
,@body)))
(provide 'testutil-request)
;;; testutil-request.el ends here
|