summaryrefslogtreecommitdiff
path: root/extra/http/http-tests.lisp
blob: 38dfac80d61bc3e9633521f993ea5cb3bccb51f7 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(defpackage #:eris-http-tests
  (:use #:common-lisp #:serapeum #:fiveam #:eris-http-server #:eris-http-client))

(in-package :eris-http-tests)


(defun make-temporary-dir ()
  (let* ((tmpdir (uiop:temporary-directory))
         (tmp-tmpdir (make-pathname
                      :directory (serapeum:append1
                                  (pathname-directory tmpdir)
                                  (ironclad:byte-array-to-hex-string (ironclad:random-data 10)))
                      :defaults tmpdir)))
    (ensure-directories-exist tmp-tmpdir)
    tmp-tmpdir))

(defmacro with-temporary-dir (sym &body expr)
  `(let ((,sym (make-temporary-dir)))
     (unwind-protect
          (progn ,@expr)
       (uiop:delete-directory-tree ,sym :validate t))))

(defmacro with-clack (handler &body expr)
  (alexandria:with-gensyms (v)
    `(let ((,v (clack:clackup ,handler)))
       (unwind-protect (progn ,@expr)
         (progn (format t "STOPPING CLACK ~%") (clack:stop ,v))))))

;; (base64:string-to-base64-string "test:test")
(alexandria:define-constant auth '("dGVzdDp0ZXN0") :test #'equal)

(def-suite* eris-http-tests)

(test basic-http-rw
  (with-temporary-dir tdir
    (with-clack (eris-handler tdir :auth auth)
      (let ((backend (make-instance 'http-backend :endpoint "http://127.0.0.1:5000/uri-res/"
                                                  :auth '("test" . "test"))))
        (for-all ((buffer (gen-buffer :length (gen-integer :min 1 :max 80000))))
          (is (equalp (alexandria:read-stream-content-into-byte-vector
                       (eris:fetch-data 
                        (eris:store-data buffer backend)
                        backend))
                      buffer))
          (is (equalp (alexandria:read-stream-content-into-byte-vector
                       (eris:fetch-data
                        (eris:store-data (make-array 40000 :element-type 'octet :initial-element 2) backend)
                        backend))
                      (make-array 40000 :element-type 'octet :initial-element 2))))))))

(test bad-inputs
  (with-temporary-dir tdir
    (with-clack (eris-handler tdir :auth auth)
      (signals error
        (dexador:get "http://127.0.0.1:5000/lole"))
      (signals error
        (dexador:get "http://127.0.0.1:5000/../../"))
      (signals error
        (dexador:get "http://127.0.0.1:5000/uri-res/N2R?urn:blake2b:../../passwd"))
      (signals error
        (dexador:get "http://127.0.0.1:5000/uri-res/N2R?urn:blake2b:not"))
      (signals error
        (dexador:get "http://127.0.0.1:5000/uri-res/random-data"))
      (signals error
        (dexador:get "http://127.0.0.1:5000/../../../shadow"))
      (signals error
        (dexador:get "http://127.0.0.1:5000/uri-res/../something")))))