Skip to content

Commit

Permalink
added `clack.test.suite' to test Clack handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukamachi committed Jan 22, 2011
1 parent 3c0feae commit d98e7e5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
7 changes: 5 additions & 2 deletions clack.asd
Expand Up @@ -34,7 +34,9 @@
:modlisp
;; for Other purpose
:cl-ppcre
:cl-fad)
:cl-fad
:cl-test-more
:drakma)
:components ((:module "src"
:serial t
:components
Expand All @@ -48,7 +50,8 @@
:components
((:file "hunchentoot")
#+(or allegro cmu lispworks sbcl)
(:file "apache")))))
(:file "apache")))
(:file "test/suite")))
(:module "contrib"
:depends-on ("core")
:serial t
Expand Down
67 changes: 67 additions & 0 deletions src/core/test/suite.lisp
@@ -0,0 +1,67 @@
#|
This file is a part of Clack package.
URL: http://github.com/fukamachi/clack
Copyright (c) 2011 Eitarow Fukamachi <e.arrows@gmail.com>
Clack is freely distributable under the LLGPL License.
|#

#|
Test suite for Clack handlers.
Author: Eitarow Fukamachi (e.arrows@gmail.com)
|#

(in-package :cl-user)

(defpackage clack.test.suite
(:use :cl
:drakma
:cl-test-more)
(:export :run-server-tests))

(in-package :clack.test.suite)

(defvar *handler-package* nil
"Handler package to test.")
(defvar *tests* '()
"Collection of tests for Clack Handler.")

(defun run-server-tests (handler-name)
"Run tests for clack.handler.
Handler name is a keyword and doesn't include the clack.handler prefix.
For example, if you have a handler `clack.handler.foo',
you would call like this: `(run-server-tests :foo)'."
(setf *handler-package*
(find-package
(concatenate 'string "CLACK.HANDLER."
(symbol-name handler-name))))
(plan (length *tests*))
(dolist (test *tests*)
(apply #'test test))
(finalize))

(defun test (fn app &optional desc)
"Test each registed tests."
(let ((acceptor (funcall (intern "RUN" *handler-package*)
app :port 4242 :debug t)))
(when desc (diag desc))
(funcall fn)
(funcall (intern "STOP" *handler-package*) acceptor)))

(defmacro deftest (desc fn app)
"Regist a test. Note that `desc' should be a string."
`(push (list ,fn ,app ,desc) *tests*))

;; Tests

(deftest "normal"
(lambda ()
(multiple-value-bind (body status headers)
(http-request "http://localhost:4242/")
(is body (format nil "ok~%"))))
(lambda (req)
(declare (ignore req))
'(200
(:content-type "text/plain")
("ok"))))

0 comments on commit d98e7e5

Please sign in to comment.