Permalink
Browse files

Add clack.request & clack.response and fix tests.

  • Loading branch information...
1 parent e5f0653 commit 940000725a5840eacad05ceff38827fd51caf0b2 @fukamachi committed Mar 25, 2015
View
@@ -17,13 +17,20 @@
:cl-fad
:quri
:trivial-mimes
+ :trivial-types
+ :http-body
+ :flexi-streams
+ :circular-streams
:ironclad
:cl-syntax-annot
:alexandria
:split-sequence)
:components ((:module "v1-compat/src/core"
:components
((:file "builder")
+ (:file "request-response")
+ (:file "request" :depends-on ("request-response"))
+ (:file "response" :depends-on ("request-response"))
(:file "component")
(:file "middleware" :depends-on ("component"))
(:module "middleware-components"
@@ -39,7 +46,7 @@
:pathname "session"
:components
((:file "session" :depends-on ("state" "state/cookie" "store"))
- (:file "session/cookie" :depends-on ("session"))
+ (:file "cookie" :depends-on ("session"))
(:file "state")
(:file "state/cookie" :depends-on ("state"))
(:file "store")))))
@@ -12,6 +12,8 @@
((:module "v1-compat/t/core"
:components
((:test-file "builder")
+ (:test-file "request")
+ (:test-file "response")
(:test-file "app/file")
(:test-file "app/urlmap")
(:test-file "middleware/conditional")
@@ -1,5 +1,6 @@
(in-package :cl-user)
(defpackage clack.middleware.conditional
+ (:nicknames :lack.middleware.condition)
(:use :cl
:clack.component
:clack.middleware))
@@ -8,6 +9,15 @@
(cl-syntax:use-syntax :annot)
@export
+(defparameter *lack-middleware-condition*
+ (lambda (app cond &key builder)
+ (wrap
+ (make-instance '<clack-middleware-conditional>
+ :condition cond
+ :builder builder)
+ app)))
+
+@export
(defclass <clack-middleware-conditional> (<middleware>)
((condition :type component-designator
:initarg :condition)
@@ -20,6 +20,7 @@
(:import-from :ironclad
:ascii-string-to-byte-array
:update-hmac
+ :make-hmac
:byte-array-to-hex-string
:hmac-digest))
(in-package :clack.middleware.session.cookie)
@@ -1,18 +1,11 @@
-#|
- This file is a part of Clack package.
- URL: http://github.com/fukamachi/clack
- Copyright (c) 2011 Eitaro Fukamachi <e.arrows@gmail.com>
-
- Clack is freely distributable under the LLGPL License.
-|#
-
(in-package :cl-user)
(defpackage clack.request
(:use :cl)
(:import-from :clack.request-response
:headers)
(:import-from :trivial-types
- :association-list)
+ :association-list
+ :property-list)
(:import-from :http-body
:parse)
(:import-from :quri
@@ -29,8 +22,6 @@
(:import-from :circular-streams
:make-circular-input-stream
:circular-stream-buffer)
- (:import-from :clack.util
- :nappend)
(:export :env
:request-method
:script-name
@@ -185,7 +176,7 @@ instances of <request>."
;; FIXME: Return a circular stream
(flex:make-in-memory-input-stream buffer)
(let ((stream (make-circular-input-stream body)))
- (nappend env `(:raw-body-buffer ,(circular-stream-buffer stream)))
+ (rplacd (last env) (list :raw-body-buffer (circular-stream-buffer stream)))
stream)))))
@export
@@ -252,25 +243,3 @@ on an original raw-body."
(if name
(assoc-value-multi name params)
params)))
-
-(doc:start)
-
-@doc:NAME "
-Clack.Request - Portable HTTP Request object for Clack Request.
-"
-
-@doc:SYNOPSIS "
- (defun app (env)
- (let ((req (make-request env)))
- `(200
- (:content-type \"text/plain\")
- (\"Hello, \" (query-parameter req \"name\")))))
-"
-
-@doc:DESCRIPTION "
-Clack.Request provides a consistent API for request objects.
-"
-
-@doc:AUTHOR "
-* Eitaro Fukamachi (e.arrows@gmail.com)
-"
@@ -1,11 +1,3 @@
-#|
- This file is a part of Clack package.
- URL: http://github.com/fukamachi/clack
- Copyright (c) 2011 Eitaro Fukamachi <e.arrows@gmail.com>
-
- Clack is freely distributable under the LLGPL License.
-|#
-
(in-package :cl-user)
(defpackage clack.response
(:use :cl)
@@ -16,9 +8,8 @@
(:import-from :alexandria
:ensure-list
:doplist
- :when-let)
- (:import-from :clack.util
- :getf*)
+ :when-let
+ :make-keyword)
(:import-from :quri
:url-encode)
(:import-from :local-time
@@ -61,6 +52,12 @@ Create a <response> instance."
:headers headers
:body body))
+(defun normalize-key (name)
+ "Returns a keyword of NAME."
+ (etypecase name
+ (keyword name)
+ ((or string symbol) (make-keyword name))))
+
@export
(defgeneric headers (res &optional name)
(:documentation
@@ -73,7 +70,7 @@ Example:
;;=> \"text/plain\"")
(:method ((res <response>) &optional name)
(if name
- (getf* (headers res) name)
+ (getf (headers res) (normalize-key name))
(slot-value res 'headers))))
@export
@@ -86,7 +83,7 @@ Example:
(setf (headers res :content-type) \"text/html\")")
(:method (value (res <response>) &optional name)
(if name
- (setf (getf* (slot-value res 'headers) name) value)
+ (setf (getf (slot-value res 'headers) (normalize-key name)) value)
(setf (slot-value res 'headers) value))))
@export
@@ -121,7 +118,7 @@ Example:
(:method ((res <response>) &optional name)
(let ((cookies (slot-value res 'set-cookies)))
(if name
- (getf* cookies name)
+ (getf cookies (normalize-key name))
cookies))))
@export
@@ -134,7 +131,7 @@ Example:
(setf (set-cookies res :hoge) \"1\")")
(:method (value (res <response>) &optional name)
(if name
- (setf (getf* (slot-value res 'set-cookies) name)
+ (setf (getf (slot-value res 'set-cookies) (normalize-key name))
(if (consp value)
value
`(:value ,value)))
@@ -192,41 +189,3 @@ Example:
(format nil
"~{~{~A~^=~}~^; ~}"
(nreverse cookie))))
-
-(doc:start)
-
-@doc:NAME "
-Clack.Response - Portable HTTP Response object for Clack response.
-"
-
-@doc:SYNOPSIS "
- (defvar res nil)
-
- (setf res (make-response 200))
- (setf res (make-response 200 '(:content-type \"text/html\")))
- (setf res (make-response 200 '(:content-type \"text/html\") '(\"aiueo\")))
-
- ;; Access each fields
- (status res)
- ;;=> 200
- (headers res)
- ;;=> (:content-type \"text/html\")
- (headers res :content-type)
- ;;=> \"text/html\"
- (body res)
- ;;=> (\"aiueo\")
-
- ;; Set to each fields
- (setf (status res) 304)
- (setf (headers res :content-type) \"text/plain\")
- (setf (body res) '(\"moved\"))
- (setf (body res) \"moved\") ;; string also allowed
-"
-
-@doc:DESCRIPTION "
-Clack.Response allows you a way to create Clack response.
-"
-
-@doc:AUTHOR "
-* Eitaro Fukamachi (e.arrows@gmail.com)
-"
@@ -1,7 +1,8 @@
(in-package :cl-user)
(defpackage t.clack.middleware.conditional
(:use :cl
- :clack
+ :clack.component
+ :clack.middleware
:clack.builder
:prove
:clack.middleware.conditional
@@ -11,7 +11,7 @@
(:shadowing-import-from :prove :finalize))
(in-package :t.clack.middleware.session)
-(plan 8)
+(plan 2)
#+thread-support
(subtest-app "middleware session"
@@ -67,6 +67,6 @@
(is body "Hello, you've been here for 2th times!"))))
#-thread-support
-(skip 4 "because your lisp doesn't support threads")
+(skip 2 "because your lisp doesn't support threads")
(finalize)
@@ -9,7 +9,7 @@
:drakma))
(in-package :t.clack.middleware.static)
-(plan 8)
+(plan 1)
(defvar *clack-pathname*
(asdf:component-pathname (asdf:find-system :clack)))
@@ -39,6 +39,6 @@
(is body "Happy Valentine!")))
#-thread-support
-(skip 8 "because your lisp doesn't support threads")
+(skip 1 "because your lisp doesn't support threads")
(finalize)
@@ -26,7 +26,7 @@
"cookie" "hoge=1;fuga=semi;colon")
:test 'equal))))
-(plan 19)
+(plan 16)
(ok (env req) "env")
@@ -95,31 +95,30 @@
(asdf:component-pathname (asdf:find-system :clack)))
#+thread-support
-(test-app
- (lambda (env)
- (make-request env)
- `(200 nil (,(gethash "filename" (cadr (body-parameter (make-request env) :|file|))))))
- (lambda ()
- (multiple-value-bind (body status)
- (http-request "http://localhost:4242/"
- :method :post
- :parameters
- `(("file" ,(merge-pathnames #p"tmp/jellyfish.jpg" *clack-pathname*)
- :content-type "image/jpeg"
- :filename "jellyfish.jpg")))
- (is status 200)
- (is body "jellyfish.jpg"))
-
- (multiple-value-bind (body status)
- (http-request "http://localhost:4242/"
- :method :post
- :parameters
- `(("file" ,(merge-pathnames #p"tmp/jellyfish.jpg" *clack-pathname*)
- :content-type "image/jpeg"
- :filename "jellyfish.jpg"))
- :content-length t)
- (is status 200)
- (is body "jellyfish.jpg"))))
+(subtest-app "make-request"
+ (lambda (env)
+ (make-request env)
+ `(200 nil (,(gethash "filename" (cadr (body-parameter (make-request env) :|file|))))))
+ (multiple-value-bind (body status)
+ (http-request "http://localhost:4242/"
+ :method :post
+ :parameters
+ `(("file" ,(merge-pathnames #p"tmp/jellyfish.jpg" *clack-pathname*)
+ :content-type "image/jpeg"
+ :filename "jellyfish.jpg")))
+ (is status 200)
+ (is body "jellyfish.jpg"))
+
+ (multiple-value-bind (body status)
+ (http-request "http://localhost:4242/"
+ :method :post
+ :parameters
+ `(("file" ,(merge-pathnames #p"tmp/jellyfish.jpg" *clack-pathname*)
+ :content-type "image/jpeg"
+ :filename "jellyfish.jpg"))
+ :content-length t)
+ (is status 200)
+ (is body "jellyfish.jpg")))
#-thread-support
(skip 4 "because your lisp doesn't support threads")
File renamed without changes.

0 comments on commit 9400007

Please sign in to comment.