/
static.lisp
86 lines (77 loc) · 3.1 KB
/
static.lisp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(in-package :cl-user)
(defpackage t.lack.middleware.static
(:use :cl
:prove
:lack
:lack.test)
(:import-from :alexandria
:starts-with-subseq))
(in-package :t.lack.middleware.static)
(plan 4)
(subtest ":path is string"
(let ((app
(builder
(:static :path "/public/"
:root (asdf:system-relative-pathname :lack #P"data/"))
(lambda (env)
(declare (ignore env))
`(200 (:content-type "text/plain") ("Happy Valentine!"))))))
(destructuring-bind (status headers body)
(funcall app (generate-env "/public/jellyfish.jpg"))
(is status 200)
(is (getf headers :content-type) "image/jpeg")
(is (namestring body)
(namestring (asdf:system-relative-pathname :lack #P"data/jellyfish.jpg"))))
(destructuring-bind (status headers body)
(funcall app (generate-env "/public/hoge.png"))
(declare (ignore headers))
(is status 404)
(is body '("Not Found")))
(destructuring-bind (status headers body)
(funcall app (generate-env "/"))
(is status 200)
(is (getf headers :content-type) "text/plain")
(is body '("Happy Valentine!")))))
(subtest ":path is NIL"
(let ((app
(builder
(:static :path nil
:root (asdf:system-relative-pathname :lack #P"data/"))
(lambda (env)
(declare (ignore env))
`(200 (:content-type "text/plain") ("ok"))))))
(destructuring-bind (status headers body)
(funcall app (generate-env "/public/jellyfish.jpg"))
(is status 200)
(is (getf headers :content-type) "text/plain")
(is body '("ok")))))
(subtest ":path is function"
(let ((app
(builder
(:static :path (lambda (path-info)
(when (starts-with-subseq "/static/" path-info)
(subseq path-info #.(length "/static"))))
:root (asdf:system-relative-pathname :lack #P"data/"))
(lambda (env)
(declare (ignore env))
`(200 (:content-type "text/plain") ("ok"))))))
(destructuring-bind (status headers body)
(funcall app (generate-env "/static/jellyfish.jpg"))
(is status 200)
(is (getf headers :content-type) "image/jpeg")
(is (namestring body)
(namestring (asdf:system-relative-pathname :lack #P"data/jellyfish.jpg"))))
(is (car (funcall app (generate-env "/static/not-found.png"))) 404)))
(subtest "special character in path-info"
(let ((app
(builder
(:static :path (lambda (path-info)
(when (starts-with-subseq "/static/" path-info)
(subseq path-info #.(length "/static"))))
:root (asdf:system-relative-pathname :lack #P"data/"))
(lambda (env)
(declare (ignore env))
`(200 (:content-type "text/plain") ("ok"))))))
(is (first (funcall app (generate-env "/static/?broken=yup"))) 404)
(is (first (funcall app (generate-env "/static/%3Fbroken=yup"))) 404)))
(finalize)