branch: externals/plz commit e8440857e5500ae3f223afa7bd90263abf5c4df3 Author: Adam Porter <a...@alphapapa.net> Commit: Adam Porter <a...@alphapapa.net>
Tests: Tidy with local URL function --- tests/test-plz.el | 101 +++++++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/tests/test-plz.el b/tests/test-plz.el index d6f7630853..ef47b899fa 100644 --- a/tests/test-plz.el +++ b/tests/test-plz.el @@ -74,7 +74,11 @@ If running httpbin locally, set to \"http://localhost\".") do (sleep-for seconds)))) (cl-defmacro plz-deftest (name () &body docstring-keys-and-body) - "Like `ert-deftest', but defines tests for both HTTP/1.1 and HTTP/2." + "Like `ert-deftest', but defines tests for both HTTP/1.1 and HTTP/2. +Also defines local function `url' which returns its argument +appended to `plz-test-uri-prefix' (and any instance of +\"URI-PREFIX\" in URL-PART is replaced with `plz-test-uri-prefix' +in URL-encoded form)." (declare (debug (&define [&name "test@" symbolp] sexp [&optional stringp] [&rest keywordp sexp] def-body)) @@ -86,18 +90,15 @@ If running httpbin locally, set to \"http://localhost\".") `(ert-deftest ,name () (let ((plz-curl-default-args ',(append plz-curl-default-args (list (format "--http%s" http-version))))) - ,@docstring-keys-and-body)))))) + (cl-labels ((url (part) + (setf part (replace-regexp-in-string + "URI-PREFIX" (url-hexify-string plz-test-uri-prefix) + part t t)) + (concat plz-test-uri-prefix part))) + ,@docstring-keys-and-body))))))) ;;;; Functions -(defun plz-test-url (url-part) - "Return URL-PART appended to `plz-test-uri-prefix'. -Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with -`plz-test-uri-prefix' in URL-encoded form." - (setf url-part (replace-regexp-in-string "URI-PREFIX" (url-hexify-string plz-test-uri-prefix) - url-part t t)) - (concat plz-test-uri-prefix url-part)) - (defmacro plz-test-get-response (response) "Test parts of RESPONSE with `should'." `(progn @@ -115,7 +116,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-string nil (let* ((test-string) - (process (plz 'get (plz-test-url "/get") + (process (plz 'get (url "/get") :as 'string :then (lambda (string) (setf test-string string))))) @@ -124,7 +125,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-buffer nil (let* ((result-buffer) - (process (plz 'get (plz-test-url "/get") + (process (plz 'get (url "/get") :as 'buffer :then (lambda (buffer) (setf result-buffer buffer))))) (unwind-protect @@ -139,7 +140,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-response nil (let* ((test-response) - (process (plz 'get (plz-test-url "/get") + (process (plz 'get (url "/get") :as 'response :then (lambda (response) (setf test-response response))))) @@ -148,7 +149,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-json nil (let* ((test-json) - (process (plz 'get (plz-test-url "/get") + (process (plz 'get (url "/get") :as #'json-read :then (lambda (json) (setf test-json json))))) @@ -159,7 +160,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-post-json-string nil (let* ((json-string (json-encode (list (cons "key" "value")))) (response-json) - (process (plz 'post (plz-test-url "/post") + (process (plz 'post (url "/post") :headers '(("Content-Type" . "application/json")) :body json-string :as #'json-read @@ -171,13 +172,13 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (should (string= "value" (alist-get 'key (json-read-from-string .data))))))) (plz-deftest plz-post-jpeg-string nil - (let* ((jpeg-to-upload (plz 'get (plz-test-url "/image/jpeg") + (let* ((jpeg-to-upload (plz 'get (url "/image/jpeg") :as 'binary :then 'sync)) (_ (unless jpeg-to-upload (error "jpeg-to-upload is nil"))) (response-json) (response-jpeg) - (process (plz 'post (plz-test-url "/post") + (process (plz 'post (url "/post") :headers '(("Content-Type" . "image/jpeg")) :body jpeg-to-upload :body-type 'binary :as #'json-read @@ -199,7 +200,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-put-json-string nil (let* ((json-string (json-encode (list (cons "key" "value")))) (response-json) - (process (plz 'put (plz-test-url "/put") + (process (plz 'put (url "/put") :headers '(("Content-Type" . "application/json")) :body json-string :as #'json-read @@ -215,21 +216,21 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with ;;;;; Sync (plz-deftest plz-get-string-sync nil - (let-alist (json-read-from-string (plz 'get (plz-test-url "/get") + (let-alist (json-read-from-string (plz 'get (url "/get") :as 'string :then 'sync)) - (should (equal (plz-test-url "/get") .url)))) + (should (equal (url "/get") .url)))) (plz-deftest plz-get-response-sync nil - (plz-test-get-response (plz 'get (plz-test-url "/get") + (plz-test-get-response (plz 'get (url "/get") :as 'response :then 'sync))) (plz-deftest plz-get-json-sync nil - (let-alist (plz 'get (plz-test-url "/get") + (let-alist (plz 'get (url "/get") :as #'json-read :then 'sync) (should (string-match "curl" .headers.User-Agent)))) (plz-deftest plz-get-buffer-sync nil - (let ((buffer (plz 'get (plz-test-url "/get") + (let ((buffer (plz 'get (url "/get") :as 'buffer :then 'sync))) (unwind-protect (should (buffer-live-p buffer)) @@ -242,7 +243,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-with-headers () (let* ((response-json) - (process (plz 'get (plz-test-url "/get") + (process (plz 'get (url "/get") :headers '(("X-Plz-Test-Header" . "plz-test-header-value")) :as #'json-read :then (lambda (json) @@ -254,7 +255,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-post-with-headers () (let* ((alist (list (cons "key" "value"))) (response-json) - (process (plz 'post (plz-test-url "/post") + (process (plz 'post (url "/post") :headers '(("Content-Type" . "application/json") ("X-Plz-Test-Header" . "plz-test-header-value")) :body (json-encode alist) @@ -267,7 +268,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (should (equal "value" (alist-get 'key (json-read-from-string .data))))))) (plz-deftest plz-get-json-with-headers-sync () - (let-alist (plz 'get (plz-test-url "/get") + (let-alist (plz 'get (url "/get") :headers '(("X-Plz-Test-Header" . "plz-test-header-value")) :as #'json-read :then 'sync) (should (string-match "curl" .headers.User-Agent)) @@ -284,10 +285,10 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with ;; lightweight way to test a server's presence, so we should ;; probably support it. This merely tests that no error is ;; signaled, which should mean that the HEAD request succeeded. - (should (plz 'head (plz-test-url "/get")))) + (should (plz 'head (url "/get")))) (plz-deftest plz-head-as-response () - (let ((response (plz 'head (plz-test-url "/get") + (let ((response (plz 'head (url "/get") :as 'response))) (should (equal "application/json" (alist-get 'content-type @@ -299,44 +300,44 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (should (equal "" (alist-get 'data (json-read-from-string - (plz 'post (plz-test-url "/post")))))) + (plz 'post (url "/post")))))) (should (equal "application/json" (alist-get 'content-type (plz-response-headers - (plz 'post (plz-test-url "/post") :as 'response)))))) + (plz 'post (url "/post") :as 'response)))))) ;;;;; Status codes (plz-deftest plz-201-succeeds () ;; This merely tests that a 201 response does not signal an error. - (should (plz 'get (plz-test-url "/status/201")))) + (should (plz 'get (url "/status/201")))) (plz-deftest plz-400-errors () - (should-error (plz 'get (plz-test-url "/status/400")))) + (should-error (plz 'get (url "/status/400")))) (plz-deftest plz-500-errors () - (should-error (plz 'get (plz-test-url "/status/500")))) + (should-error (plz 'get (url "/status/500")))) ;;;;; Redirects (plz-deftest plz-301-redirects () (plz-test-get-response - (plz 'get (plz-test-url "/redirect-to?url=URI-PREFIX%2Fget&status_code=301") + (plz 'get (url "/redirect-to?url=URI-PREFIX%2Fget&status_code=301") :as 'response :then 'sync))) (plz-deftest plz-302-redirects () (plz-test-get-response - (plz 'get (plz-test-url "/redirect-to?url=URI-PREFIX%2Fget&status_code=302") + (plz 'get (url "/redirect-to?url=URI-PREFIX%2Fget&status_code=302") :as 'response :then 'sync))) (plz-deftest plz-307-redirects () (plz-test-get-response - (plz 'get (plz-test-url "/redirect-to?url=URI-PREFIX%2Fget&status_code=307") + (plz 'get (url "/redirect-to?url=URI-PREFIX%2Fget&status_code=307") :as 'response :then 'sync))) (plz-deftest plz-308-redirects () (plz-test-get-response - (plz 'get (plz-test-url "/redirect-to?url=URI-PREFIX%2Fget&status_code=308") + (plz 'get (url "/redirect-to?url=URI-PREFIX%2Fget&status_code=308") :as 'response :then 'sync))) ;;;;; Errors @@ -385,7 +386,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-404-error-sync nil (pcase-let ((`(,_signal . (,_message ,data)) - (should-error (plz 'get (plz-test-url "/get/status/404") + (should-error (plz 'get (url "/get/status/404") :as 'string :then 'sync) :type 'plz-error))) (should (plz-error-p data)) @@ -394,7 +395,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-404-error-async nil (let* ((err) - (process (plz 'get (plz-test-url "/get/status/404") + (process (plz 'get (url "/get/status/404") :as 'string :then #'ignore :else (lambda (e) (setf err e))))) @@ -406,7 +407,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-timeout-error-sync nil (pcase-let* ((start-time (current-time)) (`(,_signal . (,_message ,(cl-struct plz-error (curl-error `(,code . ,message))))) - (should-error (plz 'get (plz-test-url "/delay/5") + (should-error (plz 'get (url "/delay/5") :as 'string :then 'sync :timeout 1) :type 'plz-error)) (end-time (current-time))) @@ -418,7 +419,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (let* ((start-time (current-time)) (end-time) (plz-error) - (process (plz 'get (plz-test-url "/delay/5") + (process (plz 'get (url "/delay/5") :as 'response :timeout 1 :then #'ignore :else (lambda (e) (setf end-time (current-time) @@ -432,7 +433,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-finally nil (let* ((finally-null t) - (process (plz 'get (plz-test-url "/get") + (process (plz 'get (url "/get") :as 'string :then #'ignore :finally (lambda () @@ -444,7 +445,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (plz-deftest plz-get-jpeg () (let* ((test-jpeg) - (process (plz 'get (plz-test-url "/image/jpeg") + (process (plz 'get (url "/image/jpeg") :as 'binary :then (lambda (string) (setf test-jpeg string))))) @@ -452,14 +453,14 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (should (equal 'jpeg (image-type-from-data test-jpeg))))) (plz-deftest plz-get-jpeg-sync () - (let ((jpeg (plz 'get (plz-test-url "/image/jpeg") + (let ((jpeg (plz 'get (url "/image/jpeg") :as 'binary :then 'sync))) (should (equal 'jpeg (image-type-from-data jpeg))))) ;;;;; Downloading to files (plz-deftest plz-get-temp-file () - (let ((filename (plz 'get (plz-test-url "/image/jpeg") + (let ((filename (plz 'get (url "/image/jpeg") :as 'file :then 'sync))) (unwind-protect (let ((jpeg-data (with-temp-buffer @@ -476,7 +477,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (delete-file filename) (unwind-protect (progn - (plz 'get (plz-test-url "/image/jpeg") + (plz 'get (url "/image/jpeg") :as `(file ,filename) :then 'sync) (let ((jpeg-data (with-temp-buffer (insert-file-contents filename) @@ -494,7 +495,7 @@ Also, any instance of \"URI-PREFIX\" in URL-PART is replaced with (with-temp-file filename (insert "deadbeef")) (setf process - (plz 'put (plz-test-url "/put") + (plz 'put (url "/put") :body `(file ,filename) :as #'json-read :then (lambda (json) @@ -518,7 +519,7 @@ and only called once." :finally (lambda () (setf finally-called-at (current-time)) (cl-incf finally-called-times)))) - (urls (list (plz-test-url "/delay/2"))) + (urls (list (url "/delay/2"))) completed-urls queue-started-at) (dolist (url urls) (plz-queue queue @@ -540,8 +541,8 @@ and only called once." (plz-deftest plz-queue-without-finally () "Ensure that a queue without a FINALLY function doesn't signal an error." (let* ((queue (make-plz-queue :limit 2)) - (urls (list (plz-test-url "/get?foo=0") - (plz-test-url "/get?foo=1"))) + (urls (list (url "/get?foo=0") + (url "/get?foo=1"))) completed-urls) (dolist (url urls) (plz-queue queue