mirror of
https://github.com/vale981/lack
synced 2025-03-05 09:21:39 -05:00
Merge pull request #6 from Rudolph-Miller/mount
Make mount to be able to take lack-component.
This commit is contained in:
commit
759ddb8c93
4 changed files with 96 additions and 2 deletions
|
@ -7,4 +7,5 @@
|
|||
:version "0.1"
|
||||
:author "Eitaro Fukamachi"
|
||||
:license "LLGPL"
|
||||
:depends-on (:lack-component)
|
||||
:components ((:file "src/middleware/mount")))
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
(in-package :cl-user)
|
||||
(defpackage lack.middleware.mount
|
||||
(:use :cl)
|
||||
(:import-from :lack.component
|
||||
:to-app)
|
||||
(:export :*lack-middleware-mount*))
|
||||
(in-package :lack.middleware.mount)
|
||||
|
||||
|
@ -12,13 +14,13 @@
|
|||
(cond
|
||||
((string= path-info path)
|
||||
(setf (getf env :path-info) "/")
|
||||
(funcall mount-app env))
|
||||
(funcall (to-app mount-app) env))
|
||||
((and (< len (length path-info))
|
||||
(string= path-info path :end1 len)
|
||||
(char= (aref path-info len) #\/))
|
||||
(setf (getf env :path-info)
|
||||
(subseq path-info (length path)))
|
||||
(funcall mount-app env))
|
||||
(funcall (to-app mount-app) env))
|
||||
(t
|
||||
(funcall app env)))))))
|
||||
"Middleware for attaching another Lack application on a specific URL")
|
||||
|
|
19
t-lack-middleware-mount.asd
Normal file
19
t-lack-middleware-mount.asd
Normal file
|
@ -0,0 +1,19 @@
|
|||
(in-package :cl-user)
|
||||
(defpackage t-lack-middleware-mount-asd
|
||||
(:use :cl :asdf))
|
||||
(in-package :t-lack-middleware-mount-asd)
|
||||
|
||||
(defsystem t-lack-middleware-mount
|
||||
:author "Rudolph Miller"
|
||||
:license "LLGPL"
|
||||
:depends-on (:lack
|
||||
:lack-test
|
||||
:lack-component
|
||||
:lack-middleware-mount
|
||||
:prove)
|
||||
:components
|
||||
((:test-file "t/middleware/mount"))
|
||||
|
||||
:defsystem-depends-on (:prove-asdf)
|
||||
:perform (test-op :after (op c)
|
||||
(funcall (intern #.(string :run-test-system) :prove) c)))
|
72
t/middleware/mount.lisp
Normal file
72
t/middleware/mount.lisp
Normal file
|
@ -0,0 +1,72 @@
|
|||
(in-package :cl-user)
|
||||
(defpackage t.lack.middleware.mount
|
||||
(:use :cl
|
||||
:lack
|
||||
:lack.test
|
||||
:lack.component
|
||||
:prove))
|
||||
(in-package :t.lack.middleware.mount)
|
||||
|
||||
(plan 2)
|
||||
|
||||
(defclass test-component (lack-component) ())
|
||||
|
||||
(defmethod call ((app test-component) env)
|
||||
(declare (ignore env))
|
||||
'(200 () "mount2"))
|
||||
|
||||
(subtest "dispatch"
|
||||
(macrolet ((mount-test (component)
|
||||
`(let* ((not-mounted '(200 () ("not-mounted")))
|
||||
(app
|
||||
(builder
|
||||
(:mount "/mount" ,component)
|
||||
(lambda (env)
|
||||
(declare (ignore env))
|
||||
not-mounted))))
|
||||
(let ((expected (funcall (to-app ,component) (generate-env "/")))
|
||||
(result1 (funcall app (generate-env "/mount")))
|
||||
(result2 (funcall app (generate-env "/mount/test")))
|
||||
(result3 (funcall app (generate-env "/test"))))
|
||||
(is result1
|
||||
expected
|
||||
"string=.")
|
||||
|
||||
(is result2
|
||||
expected
|
||||
"subseq.")
|
||||
|
||||
(is result3
|
||||
not-mounted
|
||||
"t.")))))
|
||||
(subtest "(lambda (env) ...)"
|
||||
(mount-test
|
||||
(lambda (env)
|
||||
(declare (ignore env))
|
||||
'(200 () ("mount")))))
|
||||
(subtest "lack-component"
|
||||
(mount-test (make-instance 'test-component)))))
|
||||
|
||||
(subtest "path-info"
|
||||
(macrolet ((is-path-info (env expected &optional comment)
|
||||
`(is (getf ,env :path-info)
|
||||
,expected
|
||||
,@(when comment (list comment)))))
|
||||
(let* ((response '(200 () ("ok")))
|
||||
(app
|
||||
(builder
|
||||
(:mount "/mount1"
|
||||
(lambda (env)
|
||||
(is-path-info env "/" "string=.")
|
||||
response))
|
||||
(:mount "/mount2"
|
||||
(lambda (env)
|
||||
(is-path-info env "/test" "subseq.")
|
||||
response))
|
||||
(lambda (env)
|
||||
(is-path-info env "/test" "t.")
|
||||
response))))
|
||||
(dolist (path (list "/mount1" "/mount2/test" "/test"))
|
||||
(funcall app (generate-env path))))))
|
||||
|
||||
(finalize)
|
Loading…
Add table
Reference in a new issue