From 6383128d3a7788755bf687f2c1cbc293fa2b9acc Mon Sep 17 00:00:00 2001 From: Bruno Dias Date: Sun, 19 May 2024 22:49:06 -0300 Subject: [PATCH] fixed wrap after functions. --- routing/dsl.lisp | 14 ++++++- routing/test.lisp | 91 ++++++++++++++++++++++++++++++++++++++++++-- wst.routing.test.asd | 3 +- 3 files changed, 102 insertions(+), 6 deletions(-) diff --git a/routing/dsl.lisp b/routing/dsl.lisp index 633fffe..d8606f1 100644 --- a/routing/dsl.lisp +++ b/routing/dsl.lisp @@ -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) @@ -20,6 +29,7 @@ stack (destructuring-bind (method &rest rest) api + (declare (ignorable method)) (let ((actions (append (reduce #'append bfs) rest (reduce #'append afs)))) @@ -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)))) diff --git a/routing/test.lisp b/routing/test.lisp index ec617ed..e40a988 100644 --- a/routing/test.lisp +++ b/routing/test.lisp @@ -1,5 +1,3 @@ -;;; copyright (c) 2024 Bruno H. Dias - (defpackage #:wst.routing.test (:use #:cl) (:import-from #:cl-hash-util @@ -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)))) diff --git a/wst.routing.test.asd b/wst.routing.test.asd index 01558c9..be16172 100644 --- a/wst.routing.test.asd +++ b/wst.routing.test.asd @@ -3,7 +3,8 @@ #:str #:cl-hash-util #:fiveam - #:wst.routing) + #:wst.routing + #:wst.routing.dsl) :pathname "routing" :serial t :components ((:file "test")))