Add test for set-routes

This commit is contained in:
paku 2024-12-22 18:33:49 +09:00
parent 234b2e50f3
commit 7641ec5f61
10 changed files with 115 additions and 9 deletions

View file

@ -26,6 +26,7 @@ src/
hello.lisp hello.lisp
users/ users/
index.lisp index.lisp
<id>.lisp
nested/ nested/
page.lisp page.lisp
``` ```

1
qlfile
View file

@ -1,4 +1,5 @@
ql ningle ql ningle
ql lack
ql cl-ppcre ql cl-ppcre
ql alexandria ql alexandria
github rove fukamachi/rove github rove fukamachi/rove

View file

@ -6,6 +6,10 @@
(:class qlot/source/ql:source-ql (:class qlot/source/ql:source-ql
:initargs (:%version :latest) :initargs (:%version :latest)
:version "ql-2023-10-21")) :version "ql-2023-10-21"))
("lack" .
(:class qlot/source/ql:source-ql
:initargs (:%version :latest)
:version "ql-2024-10-12"))
("cl-ppcre" . ("cl-ppcre" .
(:class qlot/source/ql:source-ql (:class qlot/source/ql:source-ql
:initargs (:%version :latest) :initargs (:%version :latest)

View file

@ -1,18 +1,18 @@
(defpackage #:ningle-fbr-test/router (defpackage #:ningle-fbr-test/router
(:use #:cl (:use #:cl
#:rove) #:rove)
(:import-from #:ningle)
(:import-from #:lack)
(:import-from #:lack/test
#:testing-app
#:request)
(:import-from #:ningle-fbr/router (:import-from #:ningle-fbr/router
#:pathname->path
#:path->uri #:path->uri
#:path->package)) #:path->package
#:pathname->path
#:set-routes))
(in-package #:ningle-fbr-test/router) (in-package #:ningle-fbr-test/router)
(deftest router-test
(testing "pathname->path"
(ok (string= (pathname->path #P"/home/app/src/routes/foo.lisp"
#P"/home/app/src/routes/")
"/foo"))))
(deftest uri-test (deftest uri-test
(testing "normal path" (testing "normal path"
(ok (string= (path->uri "/foo") "/foo")) (ok (string= (path->uri "/foo") "/foo"))
@ -32,3 +32,52 @@
:app/routes/foo)) :app/routes/foo))
(ok (eq (path->package "/foo" :app "somedir/routes") (ok (eq (path->package "/foo" :app "somedir/routes")
:app/somedir/routes/foo)))) :app/somedir/routes/foo))))
(deftest router-test
(testing "pathname->path"
(ok (string= (pathname->path #P"/home/app/src/routes/foo.lisp"
#P"/home/app/src/routes/")
"/foo")))
(testing "set-routes"
(testing-app (let ((app (make-instance 'ningle:app)))
(set-routes app
:system :ningle-fbr-test
:target-dir-path "routes")
(lack:builder app))
(multiple-value-bind (body status headers)
(request "/")
(declare (ignore headers))
(ok (string= body "ok"))
(ok (eql status 200)))
(multiple-value-bind (body status headers)
(request "/hello")
(declare (ignore headers))
(ok (string= body "ok"))
(ok (eql status 200)))
(multiple-value-bind (body status headers)
(request "/nested/page")
(declare (ignore headers))
(ok (string= body "ok"))
(ok (eql status 200)))
(multiple-value-bind (body status headers)
(request "/users")
(declare (ignore headers))
(ok (string= body "ok"))
(ok (eql status 200)))
(multiple-value-bind (body status headers)
(request "/users/bob")
(declare (ignore headers))
(ok (string= body "bob"))
(ok (eql status 200)))
(multiple-value-bind (body status headers)
(request "/missing")
(declare (ignore headers))
(ok (string= body "Not Found"))
(ok (eql status 404))))))

8
tests/routes/hello.lisp Normal file
View file

@ -0,0 +1,8 @@
(defpackage #:ningle-fbr-test/routes/hello
(:use #:cl)
(:export #:handle-get))
(in-package #:ningle-fbr-test/routes/hello)
(defun handle-get (params)
(declare (ignore params))
"ok")

8
tests/routes/index.lisp Normal file
View file

@ -0,0 +1,8 @@
(defpackage #:ningle-fbr-test/routes/index
(:use #:cl)
(:export #:handle-get))
(in-package #:ningle-fbr-test/routes/index)
(defun handle-get (params)
(declare (ignore params))
"ok")

View file

@ -0,0 +1,8 @@
(defpackage #:ningle-fbr-test/routes/nested/page
(:use #:cl)
(:export #:handle-get))
(in-package #:ningle-fbr-test/routes/nested/page)
(defun handle-get (params)
(declare (ignore params))
"ok")

View file

@ -0,0 +1,11 @@
(defpackage #:ningle-fbr-test/routes/not-found
(:use #:cl)
(:import-from #:lack/response)
(:import-from #:ningle)
(:export #:handle-not-found))
(in-package #:ningle-fbr-test/routes/not-found)
(defun handle-not-found ()
(setf (lack/response:response-status ningle:*response*)
404)
"Not Found")

View file

@ -0,0 +1,8 @@
(defpackage #:ningle-fbr-test/routes/users/<id>
(:use #:cl)
(:export #:handle-get))
(in-package #:ningle-fbr-test/routes/users/<id>)
(defun handle-get (params)
(let ((id (cdr (assoc :id params))))
id))

View file

@ -0,0 +1,8 @@
(defpackage #:ningle-fbr-test/routes/users/index
(:use #:cl)
(:export #:handle-get))
(in-package #:ningle-fbr-test/routes/users/index)
(defun handle-get (params)
(declare (ignore params))
"ok")