branch: externals/plz
commit 44e6f02064afb36559f968081a3ba1e612a72ea8
Author: Adam Porter <[email protected]>
Commit: Adam Porter <[email protected]>
Binary content
---
plz.el | 12 ++++++++----
tests/test-plz.el | 24 ++++++++++++++++++++++++
2 files changed, 32 insertions(+), 4 deletions(-)
diff --git a/plz.el b/plz.el
index 51cf529446..5cb53f7cbb 100644
--- a/plz.el
+++ b/plz.el
@@ -190,7 +190,7 @@
(cl-defun plz-get (url &key headers as then else
(connect-timeout plz-connect-timeout)
- (decode t))
+ (decode t decode-s))
"Get HTTP URL with curl.
AS selects the kind of result to pass to the callback function
@@ -203,6 +203,7 @@ THEN. It may be:
narrowed to the response body (suitable for, e.g. `json-read').
If DECODE is non-nil, the response body is decoded automatically.
+For binary content, it should be nil.
THEN is a callback function, whose sole argument is selected
above with AS.
@@ -220,12 +221,13 @@ the initial connection attempt."
(plz--curl 'get url
:headers headers
:connect-timeout connect-timeout
- :decode decode
+ :decode (cond ((and decode-s (not decode)) nil)
+ (t decode))
:as as :then then :else else))
(cl-defun plz-get-sync (url &key headers as
(connect-timeout plz-connect-timeout)
- (decode t))
+ (decode t decode-s))
"Get HTTP URL with curl synchronously.
AS selects the kind of result to return. It may be:
@@ -236,6 +238,7 @@ AS selects the kind of result to return. It may be:
narrowed to the response body (suitable for, e.g. `json-read').
If DECODE is non-nil, the response body is decoded automatically.
+For binary content, it should be nil.
If the request fails, an error is signaled, either
`plz-curl-error' or `plz-http-error', as appropriate, with a
@@ -248,7 +251,8 @@ the initial connection attempt."
(plz--curl-sync 'get url
:headers headers
:connect-timeout connect-timeout
- :decode decode
+ :decode (cond ((and decode-s (not decode)) nil)
+ (t decode))
:as as))
;;;;; Private
diff --git a/tests/test-plz.el b/tests/test-plz.el
index a5d468a399..071730e8cf 100644
--- a/tests/test-plz.el
+++ b/tests/test-plz.el
@@ -40,6 +40,14 @@
;;;; Commands
+;;;; Macros
+
+(cl-defmacro plz-test-wait (process &optional (seconds 0.1) (times 100))
+ "Wait for SECONDS seconds TIMES times for PROCESS to finish."
+ `(cl-loop for i upto ,times ;; 10 seconds
+ while (equal 'run (process-status ,process))
+ do (sleep-for ,seconds)))
+
;;;; Functions
(defun plz-test-get-response (response)
@@ -147,6 +155,22 @@
(plz-response-p (plz-error-response (cdr err)))
(eq 404 (plz-response-status (plz-error-response (cdr
err))))))))
+;;;;; Binary
+
+(ert-deftest plz-test-get-jpeg ()
+ (let ((jpeg (plz-get-sync "https://httpbin.org/image/jpeg"
+ :decode nil)))
+ (should (image-jpeg-p jpeg)))
+
+ (let* ((test-jpeg)
+ (process (plz-get "https://httpbin.org/image/jpeg"
+ :decode nil
+ :as 'string
+ :then (lambda (string)
+ (setf test-jpeg string)))))
+ (plz-test-wait process)
+ (should (image-jpeg-p test-jpeg))))
+
;;;; Footer
(provide 'test-plz)