diff --git a/.gitignore b/.gitignore index d74bb25..bf77062 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ *~ .#* data/test.db +data/test.log diff --git a/t-lack-middleware-backtrace.asd b/t-lack-middleware-backtrace.asd index 1e97e4a..c48fb56 100644 --- a/t-lack-middleware-backtrace.asd +++ b/t-lack-middleware-backtrace.asd @@ -8,7 +8,8 @@ :license "LLGPL" :depends-on (:lack :lack-test - :prove) + :prove + :alexandria) :components ((:test-file "t/middleware/backtrace")) diff --git a/t/middleware/backtrace.lisp b/t/middleware/backtrace.lisp index 3b81a96..9a4bbb5 100644 --- a/t/middleware/backtrace.lisp +++ b/t/middleware/backtrace.lisp @@ -6,7 +6,7 @@ :prove)) (in-package :t.lack.middleware.backtrace) -(plan 4) +(plan 6) (let ((app (builder (:backtrace @@ -47,11 +47,28 @@ (ok (< 0 (length (get-output-stream-string *error-output*))) "Got backtraces")))) +(subtest ":result-on-error is function" + (let ((app + (builder (:backtrace + :result-on-error (lambda (condition) + (if (typep condition 'test-error) + '(503 (:content-type "text/plain") ("Service Temporary Unavailable")) + '(500 (:content-type "text/plain") ("Internal Server Error"))))) + (lambda (env) + (if (string= (getf env :path-info) "/503") + (error 'test-error) + (error "Error occured"))))) + (*error-output* (make-broadcast-stream))) + (is (funcall app (generate-env "/")) + '(500 (:content-type "text/plain") ("Internal Server Error"))) + (is (funcall app (generate-env "/503")) + '(503 (:content-type "text/plain") ("Service Temporary Unavailable"))))) + (defparameter *test-error-output* (make-string-output-stream)) -(subtest "Custom :output" +(subtest "Custom :output (stream)" (let ((app - (builder (:backtrace :output '*test-error-output* + (builder (:backtrace :output *test-error-output* :result-on-error '(500 (:content-type "text/plain") ("Internal Server Error"))) (lambda (env) (declare (ignore env)) @@ -63,4 +80,21 @@ (ok (< 0 (length (get-output-stream-string *test-error-output*))) "Output to the custom :output")))) +(subtest "Custom :output (pathname)" + (let* ((log-file (asdf:system-relative-pathname :lack #P"data/test.log")) + (app + (builder (:backtrace :output log-file + :result-on-error '(500 (:content-type "text/plain") ("Internal Server Error"))) + (lambda (env) + (declare (ignore env)) + (error "Error occured"))))) + (when (probe-file log-file) + (delete-file log-file)) + (let ((*error-output* (make-string-output-stream))) + (funcall app (generate-env "/")) + (ok (= 0 (length (get-output-stream-string *error-output*))) + "Don't output to *error-output*") + (ok (< 0 (length (alexandria:read-file-into-string log-file))) + "Output to the custom :output")))) + (finalize)