From 7641ec5f6190a5b31540b874c3261c302dc3391b Mon Sep 17 00:00:00 2001 From: paku Date: Sun, 22 Dec 2024 18:33:49 +0900 Subject: [PATCH] Add test for set-routes --- README.md | 1 + qlfile | 1 + qlfile.lock | 4 +++ tests/router.lisp | 67 ++++++++++++++++++++++++++++++----- tests/routes/hello.lisp | 8 +++++ tests/routes/index.lisp | 8 +++++ tests/routes/nested/page.lisp | 8 +++++ tests/routes/not-found.lisp | 11 ++++++ tests/routes/users/.lisp | 8 +++++ tests/routes/users/index.lisp | 8 +++++ 10 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 tests/routes/hello.lisp create mode 100644 tests/routes/index.lisp create mode 100644 tests/routes/nested/page.lisp create mode 100644 tests/routes/not-found.lisp create mode 100644 tests/routes/users/.lisp create mode 100644 tests/routes/users/index.lisp diff --git a/README.md b/README.md index b01bb22..ecb0a03 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ src/ hello.lisp users/ index.lisp + .lisp nested/ page.lisp ``` diff --git a/qlfile b/qlfile index 048c828..548652c 100644 --- a/qlfile +++ b/qlfile @@ -1,4 +1,5 @@ ql ningle +ql lack ql cl-ppcre ql alexandria github rove fukamachi/rove diff --git a/qlfile.lock b/qlfile.lock index 56722c8..003a88d 100644 --- a/qlfile.lock +++ b/qlfile.lock @@ -6,6 +6,10 @@ (:class qlot/source/ql:source-ql :initargs (:%version :latest) :version "ql-2023-10-21")) +("lack" . + (:class qlot/source/ql:source-ql + :initargs (:%version :latest) + :version "ql-2024-10-12")) ("cl-ppcre" . (:class qlot/source/ql:source-ql :initargs (:%version :latest) diff --git a/tests/router.lisp b/tests/router.lisp index 564c06d..03724a0 100644 --- a/tests/router.lisp +++ b/tests/router.lisp @@ -1,18 +1,18 @@ (defpackage #:ningle-fbr-test/router (:use #:cl #:rove) + (:import-from #:ningle) + (:import-from #:lack) + (:import-from #:lack/test + #:testing-app + #:request) (:import-from #:ningle-fbr/router - #:pathname->path #:path->uri - #:path->package)) + #:path->package + #:pathname->path + #:set-routes)) (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 (testing "normal path" (ok (string= (path->uri "/foo") "/foo")) @@ -24,7 +24,7 @@ (testing "dynamic path" (ok (string= (path->uri "/user/") "/user/:id")) - (ok (string= (path->uri "/location//") "/location/:country/:city" )))) + (ok (string= (path->uri "/location//") "/location/:country/:city")))) (deftest package-test (testing "normal case" @@ -32,3 +32,52 @@ :app/routes/foo)) (ok (eq (path->package "/foo" :app "somedir/routes") :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)))))) + diff --git a/tests/routes/hello.lisp b/tests/routes/hello.lisp new file mode 100644 index 0000000..18beefa --- /dev/null +++ b/tests/routes/hello.lisp @@ -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") diff --git a/tests/routes/index.lisp b/tests/routes/index.lisp new file mode 100644 index 0000000..dab5d79 --- /dev/null +++ b/tests/routes/index.lisp @@ -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") diff --git a/tests/routes/nested/page.lisp b/tests/routes/nested/page.lisp new file mode 100644 index 0000000..afc9f62 --- /dev/null +++ b/tests/routes/nested/page.lisp @@ -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") diff --git a/tests/routes/not-found.lisp b/tests/routes/not-found.lisp new file mode 100644 index 0000000..5392fd8 --- /dev/null +++ b/tests/routes/not-found.lisp @@ -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") diff --git a/tests/routes/users/.lisp b/tests/routes/users/.lisp new file mode 100644 index 0000000..0eb385f --- /dev/null +++ b/tests/routes/users/.lisp @@ -0,0 +1,8 @@ +(defpackage #:ningle-fbr-test/routes/users/ + (:use #:cl) + (:export #:handle-get)) +(in-package #:ningle-fbr-test/routes/users/) + +(defun handle-get (params) + (let ((id (cdr (assoc :id params)))) + id)) diff --git a/tests/routes/users/index.lisp b/tests/routes/users/index.lisp new file mode 100644 index 0000000..5473c9b --- /dev/null +++ b/tests/routes/users/index.lisp @@ -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")