Skip to content

Commit

Permalink
fixed wrap after functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
diasbruno committed May 20, 2024
1 parent 84a2da1 commit 6383128
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
14 changes: 12 additions & 2 deletions routing/dsl.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
(:import-from #:wst.routing
#:add-route)
(:import-from #:wst.routing
#:remove-route))
#:remove-route)
(:import-from #:str
#:join)
(:export
#:build-webserver
#:wrap
#:any-route
#:route
#:group
#:resource))

(in-package #:wst.routing.dsl)

Expand All @@ -20,6 +29,7 @@
stack
(destructuring-bind (method &rest rest)
api
(declare (ignorable method))
(let ((actions (append (reduce #'append bfs)
rest
(reduce #'append afs))))
Expand Down Expand Up @@ -59,7 +69,7 @@
- :before (list) - run a sequence of functions before the route.
- :route (list) - the actual route or routing group.
- :after (list) - run a sequence of functions after the route."
(with-keys ((bfs "befores") (afs "after"))
(with-keys ((bfs "befores") (afs "afters"))
stack
(progn
(setf bfs (append bfs (list (ensure-list (getf api :before nil))))
Expand Down
91 changes: 88 additions & 3 deletions routing/test.lisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
;;; copyright (c) 2024 Bruno H. Dias <[email protected]>

(defpackage #:wst.routing.test
(:use #:cl)
(:import-from #:cl-hash-util
Expand Down Expand Up @@ -54,4 +52,91 @@
(5am:is (equal "internal server error"
(dispatch-route "/throw-exception" :get nil)))
(remove-route 'throw-exception))
(5am:run! 'return-internal-server-error-if-exception-is-thrown)

(5am:def-test build-a-simple-route-using-the-dsl ()
(setf wst.routing::*route-specs* nil)
(flet ((the-function ()
(5am:is-true t)))
(wst.routing.dsl:build-webserver
'(wst.routing.dsl:route :get index "/" the-function))
(wst.routing:dispatch-route-by-name 'index '(:request-method :get))))

(5am:def-test build-with-just-route-is-the-same-of-just-route-using-the-dsl ()
(setf wst.routing::*route-specs* nil)
(let* ((count 0)
(must-be-called (lambda (req res)
(declare (ignore req res))
(setf count (1+ count)))))
(wst.routing.dsl:build-webserver
`(wst.routing.dsl:wrap
:route (wst.routing.dsl:route :get index "/" ,must-be-called)))
(wst.routing:dispatch-route-by-name 'index '(:request-method :get))
(5am:is (= 1 count))))

(5am:def-test build-route-with-just-before ()
(setf wst.routing::*route-specs* nil)
(let* ((count 0)
(must-be-called (lambda (req res)
(declare (ignore req res))
(setf count (1+ count)))))
(wst.routing.dsl:build-webserver
`(wst.routing.dsl:wrap
:before ,must-be-called
:route (wst.routing.dsl:route :get index "/" ,must-be-called)))
(wst.routing:dispatch-route-by-name 'index '(:request-method :get))
(5am:is (= 2 count))))

(5am:def-test build-route-with-just-after ()
(setf wst.routing::*route-specs* nil)
(let* ((count 0)
(must-be-called (lambda (req res)
(declare (ignore req res))
(setf count (1+ count)))))
(wst.routing.dsl:build-webserver
`(wst.routing.dsl:wrap
:route (wst.routing.dsl:route :get index "/" ,must-be-called)
:after ,must-be-called))
(wst.routing:dispatch-route-by-name 'index '(:request-method :get))
(5am:is (= 2 count))))

(5am:def-test build-a-route-wrapped-using-the-dsl ()
(setf wst.routing::*route-specs* nil)
(let* ((count 0)
(must-be-called (lambda (req res)
(declare (ignore req res))
(setf count (1+ count)))))
(wst.routing.dsl:build-webserver
`(wst.routing.dsl:wrap
:before ,must-be-called
:route (wst.routing.dsl:route :get index "/" ,must-be-called)
:after ,must-be-called))
(wst.routing:dispatch-route-by-name 'index '(:request-method :get))
(5am:is (= 3 count))))

(5am:def-test build-group-of-routes-using-the-dsl ()
(setf wst.routing::*route-specs* nil)
(let* ((count 0)
(must-be-called (lambda (req res)
(declare (ignore req res))
(setf count (1+ count)))))
(wst.routing.dsl:build-webserver
`(wst.routing.dsl:group
(wst.routing.dsl:route :get route-a "/a" ,must-be-called)
(wst.routing.dsl:route :get route-b "/b" ,must-be-called)))
(wst.routing:dispatch-route-by-name 'route-a '(:request-method :get))
(wst.routing:dispatch-route-by-name 'route-b '(:request-method :get))
(5am:is (= 2 count))))

(5am:def-test build-a-resource-routes-using-the-dsl ()
(setf wst.routing::*route-specs* nil)
(let* ((count 0)
(must-be-called (lambda (req res)
(declare (ignore req res))
(setf count (1+ count)))))
(wst.routing.dsl:build-webserver
`(wst.routing.dsl:resource "/base"
(wst.routing.dsl:route :get route-a "/a" ,must-be-called)
(wst.routing.dsl:route :get route-b ,must-be-called)))
(wst.routing:dispatch-route "/base/a" :get '(:request-method :get))
(wst.routing:dispatch-route "/base" :get '(:request-method :get))
(5am:is (= 2 count))))
3 changes: 2 additions & 1 deletion wst.routing.test.asd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#:str
#:cl-hash-util
#:fiveam
#:wst.routing)
#:wst.routing
#:wst.routing.dsl)
:pathname "routing"
:serial t
:components ((:file "test")))

0 comments on commit 6383128

Please sign in to comment.