|
1 | | -;;; impostman-tests.el --- Tests on impostman.el -*- lexical-binding: t -*- |
| 1 | +;;; impostman-test.el --- Test suite for impostman -*- lexical-binding: t -*- |
2 | 2 |
|
3 | 3 | ;; Copyright (C) 2020-2021 Sébastien Helleu <flashcode@flashtux.org> |
4 | 4 |
|
|
28 | 28 |
|
29 | 29 | ;;; Commentary: |
30 | 30 |
|
31 | | -;; Tests on impostman.el |
| 31 | +;; Test suite for impostman. |
32 | 32 |
|
33 | 33 | ;;; Code: |
34 | 34 |
|
35 | 35 | (require 'ert) |
36 | 36 | (require 'impostman) |
37 | 37 |
|
38 | | -(defun get-file-contents (filename) |
| 38 | +(defun impostman-test--get-file-contents (filename) |
39 | 39 | "Get contents of filename." |
40 | 40 | (with-temp-buffer |
41 | 41 | (insert-file-contents filename) |
42 | 42 | (buffer-string))) |
43 | 43 |
|
44 | | -(ert-deftest impostman-format-comment () |
| 44 | +(ert-deftest impostman-test-format-comment () |
45 | 45 | "Test the format of comment." |
46 | 46 | ;; without prefix: default is "# " |
47 | 47 | (should (equal (impostman-format-comment nil) |
|
75 | 75 | ";; Line 2\n" |
76 | 76 | ";; Line 3\n")))) |
77 | 77 |
|
78 | | -(ert-deftest impostman-add-query-string-items-to-url () |
| 78 | +(ert-deftest impostman-test-add-query-string-items-to-url () |
79 | 79 | "Test add of query-string items to an URL." |
80 | 80 | (should (equal (impostman-add-query-string-items-to-url |
81 | 81 | "https://example.com" nil) |
|
87 | 87 | "https://example.com?a=1" '(("apikey" . "1a2b3c4d"))) |
88 | 88 | "https://example.com?a=1&apikey=1a2b3c4d"))) |
89 | 89 |
|
90 | | -(ert-deftest impostman-get-auth-basic-plain () |
| 90 | +(ert-deftest impostman-test-get-auth-basic-plain () |
91 | 91 | "Test the base64 decoding of Authorization Basic header." |
92 | 92 | (should (equal (impostman-get-auth-basic-plain "") |
93 | 93 | nil)) |
|
99 | 99 | "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") |
100 | 100 | "username:password"))) |
101 | 101 |
|
102 | | -(ert-deftest impostman-output-verb-header () |
| 102 | +(ert-deftest impostman-test-output-verb-header () |
103 | 103 | "Test the output of verb header." |
104 | 104 | (should (equal (impostman-output-verb-header "test" "Description\nLine 2") |
105 | 105 | (concat |
106 | 106 | "* test :verb:\n" |
107 | 107 | "# Description\n" |
108 | 108 | "# Line 2\n")))) |
109 | 109 |
|
110 | | -(ert-deftest impostman-output-verb-item () |
| 110 | +(ert-deftest impostman-test-output-verb-item () |
111 | 111 | "Test the output of verb item." |
112 | 112 | (should (equal (impostman-output-verb-item 0 "test" "") |
113 | 113 | (concat |
|
147 | 147 | "# Description\n" |
148 | 148 | "# end.\n")))) |
149 | 149 |
|
150 | | -(ert-deftest impostman-output-verb-request () |
| 150 | +(ert-deftest impostman-test-output-verb-request () |
151 | 151 | "Test the output of verb request." |
152 | 152 | (should (equal (impostman-output-verb-request |
153 | 153 | "" |
|
219 | 219 | "header: value\n" |
220 | 220 | "\n{\"login\": \"admin\"}\n"))))) |
221 | 221 |
|
222 | | -(ert-deftest impostman-output-verb-footer () |
| 222 | +(ert-deftest impostman-test-output-verb-footer () |
223 | 223 | "Test the output of verb footer." |
224 | 224 | (should (equal (impostman-output-verb-footer "test") |
225 | 225 | (concat |
|
230 | 230 | "# eval: (verb-mode)\n" |
231 | 231 | "# End:\n")))) |
232 | 232 |
|
233 | | -(ert-deftest impostman-output-restclient-header () |
| 233 | +(ert-deftest impostman-test-output-restclient-header () |
234 | 234 | "Test the output of restclient header." |
235 | 235 | (should (equal (impostman-output-restclient-header |
236 | 236 | "test" "Description\nLine 2") |
|
242 | 242 | "# Line 2\n" |
243 | 243 | "#\n")))) |
244 | 244 |
|
245 | | -(ert-deftest impostman-output-restclient-item () |
| 245 | +(ert-deftest impostman-test-output-restclient-item () |
246 | 246 | "Test the output of restclient item." |
247 | 247 | (should (equal (impostman-output-restclient-item 0 "test" "") |
248 | 248 | (concat |
|
258 | 258 | "## test\n"))) |
259 | 259 | (should (equal (impostman-output-restclient-item 3 "test" "") |
260 | 260 | "### test\n")) |
261 | | - (should (equal (impostman-output-restclient-item 0 "test" "Description\nend.") |
| 261 | + (should (equal (impostman-output-restclient-item |
| 262 | + 0 "test" "Description\nend.") |
262 | 263 | (concat |
263 | 264 | "\n" |
264 | 265 | "# test\n" |
265 | 266 | "# Description\n" |
266 | 267 | "# end.\n"))) |
267 | | - (should (equal (impostman-output-restclient-item 1 "test" "Description\nend.") |
| 268 | + (should (equal (impostman-output-restclient-item |
| 269 | + 1 "test" "Description\nend.") |
268 | 270 | (concat |
269 | 271 | "\n" |
270 | 272 | "# test\n" |
271 | 273 | "# Description\n" |
272 | 274 | "# end.\n"))) |
273 | | - (should (equal (impostman-output-restclient-item 2 "test" "Description\nend.") |
| 275 | + (should (equal (impostman-output-restclient-item |
| 276 | + 2 "test" "Description\nend.") |
274 | 277 | (concat |
275 | 278 | "\n" |
276 | 279 | "## test\n" |
277 | 280 | "# Description\n" |
278 | 281 | "# end.\n"))) |
279 | | - (should (equal (impostman-output-restclient-item 3 "test" "Description\nend.") |
| 282 | + (should (equal (impostman-output-restclient-item |
| 283 | + 3 "test" "Description\nend.") |
280 | 284 | (concat |
281 | 285 | "### test\n" |
282 | 286 | "# Description\n" |
283 | 287 | "# end.\n")))) |
284 | 288 |
|
285 | | -(ert-deftest impostman-output-restclient-request () |
| 289 | +(ert-deftest impostman-test-output-restclient-request () |
286 | 290 | "Test the output of restclient request." |
287 | 291 | (should (equal (impostman-output-restclient-request |
288 | 292 | "" |
|
355 | 359 | "header: value\n" |
356 | 360 | "{\"login\": \"admin\"}\n"))))) |
357 | 361 |
|
358 | | -(ert-deftest impostman-output-restclient-footer () |
| 362 | +(ert-deftest impostman-test-output-restclient-footer () |
359 | 363 | "Test the output of restclient footer." |
360 | 364 | (should (equal (impostman-output-restclient-footer "test") |
361 | 365 | (concat |
362 | 366 | "\n" |
363 | 367 | "# End of test\n")))) |
364 | 368 |
|
365 | | -(ert-deftest impostman--build-auth-headers () |
| 369 | +(ert-deftest impostman-test-build-auth-headers () |
366 | 370 | "Test the build of auth headers." |
367 | 371 | (let ((auth (make-hash-table :test 'equal)) |
368 | 372 | (username #s(hash-table test equal data ("key" "username" |
|
422 | 426 | (should (equal (impostman--build-auth-headers auth) |
423 | 427 | nil)))) |
424 | 428 |
|
425 | | -(ert-deftest impostman--build-headers () |
| 429 | +(ert-deftest impostman-test-build-headers () |
426 | 430 | "Test the build of headers." |
427 | 431 | (let ((header1 #s(hash-table test equal data ("key" "header1" |
428 | 432 | "value" "value1"))) |
|
444 | 448 | ("header1" . "value1") |
445 | 449 | ("X-header2" . "the value 2")))))) |
446 | 450 |
|
447 | | -(ert-deftest impostman--build-auth-query-string () |
| 451 | +(ert-deftest impostman-test-build-auth-query-string () |
448 | 452 | "Test build of query-string parameter for authentication." |
449 | 453 | (let ((auth (make-hash-table :test 'equal)) |
450 | 454 | (apikey-key #s(hash-table test equal data ("key" "key" |
|
463 | 467 | (should (equal (impostman--build-auth-query-string auth) |
464 | 468 | '(("apikey" . "1a2b3c4d")))))) |
465 | 469 |
|
466 | | -(ert-deftest impostman--parse-item () |
| 470 | +(ert-deftest impostman-test-parse-item () |
467 | 471 | "Test parsing of an item." |
468 | 472 | (let ((item1 (make-hash-table :test 'equal)) |
469 | 473 | (request1 (make-hash-table :test 'equal)) |
|
578 | 582 | "\n" |
579 | 583 | "{\"key\": \"data\"}\n")))))) |
580 | 584 |
|
581 | | -(ert-deftest impostman--parse-json () |
| 585 | +(ert-deftest impostman-test-parse-json () |
582 | 586 | "Test parsing of a JSON collection." |
583 | 587 | (let ((collection (make-hash-table :test 'equal)) |
584 | 588 | (info (make-hash-table :test 'equal))) |
|
633 | 637 | "# End:\n")))) |
634 | 638 | (kill-this-buffer)))) |
635 | 639 |
|
636 | | -(defun impostman-output-test-header (name description) |
| 640 | +(defun impostman-test-output-test-header (name description) |
637 | 641 | "Format the test header." |
638 | 642 | (concat |
639 | 643 | "* " name " :test:\n" |
640 | 644 | (impostman-format-comment description))) |
641 | 645 |
|
642 | | -(defun impostman-output-test-item (level name description) |
| 646 | +(defun impostman-test-output-test-item (level name description) |
643 | 647 | "Format a test item." |
644 | 648 | (concat |
645 | 649 | (if (<= level 2) "\n" "") |
646 | 650 | (make-string (max level 1) ?*) " " name "\n" |
647 | 651 | (impostman-format-comment description))) |
648 | 652 |
|
649 | | -(defun impostman-output-test-request (description method url headers body) |
| 653 | +(defun impostman-test-output-test-request (description method url headers body) |
650 | 654 | "Format a test request." |
651 | 655 | (let ((list-headers)) |
652 | 656 | (dolist (header headers) |
|
658 | 662 | (concat (string-join (nreverse list-headers) "\n") "\n")) |
659 | 663 | (if (string-empty-p body) "" (concat "\n" body "\n"))))) |
660 | 664 |
|
661 | | -(defun impostman-output-test-footer (name) |
| 665 | +(defun impostman-test-output-test-footer (name) |
662 | 666 | "Format the test footer." |
663 | 667 | (concat "\n" "* End of " name "\n")) |
664 | 668 |
|
665 | | -(ert-deftest impostman-import-file () |
| 669 | +(ert-deftest impostman-test-import-file () |
666 | 670 | "Test import of a file with a Postman collection." |
667 | | - (setq impostman-output-test-alist |
| 671 | + (setq impostman-test-output-test-alist |
668 | 672 | '((init . ignore) |
669 | | - (header . impostman-output-test-header) |
670 | | - (item . impostman-output-test-item) |
671 | | - (request . impostman-output-test-request) |
672 | | - (footer . impostman-output-test-footer) |
| 673 | + (header . impostman-test-output-test-header) |
| 674 | + (item . impostman-test-output-test-item) |
| 675 | + (request . impostman-test-output-test-request) |
| 676 | + (footer . impostman-test-output-test-footer) |
673 | 677 | (end . ignore))) |
674 | | - (let* ((verb-output (get-file-contents "tests/verb.org")) |
675 | | - (restclient-output (get-file-contents "tests/restclient.org")) |
676 | | - (test-output (get-file-contents "tests/test.org")) |
| 678 | + (let* ((verb-output |
| 679 | + (impostman-test--get-file-contents "tests/verb.org")) |
| 680 | + (restclient-output |
| 681 | + (impostman-test--get-file-contents "tests/restclient.org")) |
| 682 | + (test-output |
| 683 | + (impostman-test--get-file-contents "tests/test.org")) |
677 | 684 | (impostman-outputs-alist |
678 | 685 | '(("verb" . impostman-output-verb-alist) |
679 | 686 | ("restclient" . impostman-output-restclient-alist) |
680 | | - ("test" . impostman-output-test-alist)))) |
| 687 | + ("test" . impostman-test-output-test-alist)))) |
681 | 688 | (save-excursion |
682 | 689 | (impostman-import-file "tests/collection.json" "verb") |
683 | 690 | (should (string-prefix-p "test.org" (buffer-name))) |
|
698 | 705 | (kill-this-buffer))) |
699 | 706 | (makunbound 'impostman-output-test-alist)) |
700 | 707 |
|
701 | | -(ert-deftest impostman-import-string () |
| 708 | +(ert-deftest impostman-test-import-string () |
702 | 709 | "Test import of a string with a Postman collection." |
703 | | - (setq impostman-output-test-alist |
| 710 | + (setq impostman-test-output-test-alist |
704 | 711 | '((init . ignore) |
705 | | - (header . impostman-output-test-header) |
706 | | - (item . impostman-output-test-item) |
707 | | - (request . impostman-output-test-request) |
708 | | - (footer . impostman-output-test-footer) |
| 712 | + (header . impostman-test-output-test-header) |
| 713 | + (item . impostman-test-output-test-item) |
| 714 | + (request . impostman-test-output-test-request) |
| 715 | + (footer . impostman-test-output-test-footer) |
709 | 716 | (end . ignore))) |
710 | | - (let* ((collection (get-file-contents "tests/collection.json")) |
711 | | - (verb-output (get-file-contents "tests/verb.org")) |
712 | | - (restclient-output (get-file-contents "tests/restclient.org")) |
713 | | - (test-output (get-file-contents "tests/test.org")) |
| 717 | + (let* ((collection |
| 718 | + (impostman-test--get-file-contents "tests/collection.json")) |
| 719 | + (verb-output |
| 720 | + (impostman-test--get-file-contents "tests/verb.org")) |
| 721 | + (restclient-output |
| 722 | + (impostman-test--get-file-contents "tests/restclient.org")) |
| 723 | + (test-output |
| 724 | + (impostman-test--get-file-contents "tests/test.org")) |
714 | 725 | (impostman-outputs-alist |
715 | 726 | '(("verb" . impostman-output-verb-alist) |
716 | 727 | ("restclient" . impostman-output-restclient-alist) |
717 | | - ("test" . impostman-output-test-alist)))) |
| 728 | + ("test" . impostman-test-output-test-alist)))) |
718 | 729 | (save-excursion |
719 | 730 | (impostman-import-string collection "verb") |
720 | 731 | (should (string-prefix-p "test.org" (buffer-name))) |
|
735 | 746 | (kill-this-buffer))) |
736 | 747 | (makunbound 'impostman-output-test-alist)) |
737 | 748 |
|
738 | | -(provide 'impostman-tests) |
| 749 | +(provide 'impostman-test) |
739 | 750 |
|
740 | | -;;; impostman-tests.el ends here |
| 751 | +;;; impostman-test.el ends here |
0 commit comments