Permalink
Browse files

Update r.m.multipart-params: new wrap-multipart-params name, updated …

…project.clj deps, add tests, remove last of reflection.
  • Loading branch information...
1 parent fa8c6f1 commit 7d0999561bf214fad6f7403c9af51e221d7d707c @mmcgrana committed Mar 3, 2010
View
5 ring-core/project.clj
@@ -3,5 +3,8 @@
:url "http://github.com/mmcgrana/ring"
:dependencies [[org.clojure/clojure "1.1.0"]
[org.clojure/clojure-contrib "1.1.0"]
- [commons-codec "1.4"]]
+ [commons-codec "1.4"]
+ [commons-io "1.4"]
+ [commons-fileupload "1.2.1"]
+ [org.mortbay.jetty/servlet-api-2.5 "6.1.14"]]
:dev-dependencies [[lein-clojars "0.5.0-SNAPSHOT"]])
View
5 ring-core/src/ring/middleware/multipart_params.clj
@@ -14,7 +14,7 @@
(if-let [#^String content-type (:content-type request)]
(.startsWith content-type "multipart/form-data")))
-(defvar- file-upload
+(defvar- #^FileUpload file-upload
(FileUpload.
(doto (DiskFileItemFactory.)
(.setSizeThreshold -1)
@@ -23,6 +23,7 @@
(defn- request-context
"Create a RequestContext object from a request map."
+ {:tag RequestContext}
[request encoding]
(proxy [RequestContext] []
(getContentType [] (:content-type request))
@@ -55,7 +56,7 @@
file-upload
(request-context request encoding))))
-(defn wrap-multipart
+(defn wrap-multipart-params
"Middleware to parse multipart parameters from a request. Adds the
following keys to the request map:
:multipart-params - a map of multipart parameters
View
33 ring-core/test/ring/middleware/multipart_params_test.clj
@@ -0,0 +1,33 @@
+(ns ring.middleware.multipart-params-test
+ (:use clojure.test
+ ring.middleware.multipart-params
+ [clojure.contrib.def :only (defvar-)])
+ (:require [clojure.contrib.duck-streams :as du])
+ (:import (java.io File ByteArrayInputStream)))
+
+(defn- str-input-stream [#^String s]
+ (ByteArrayInputStream. (.getBytes s)))
+
+(defvar- upload-content-type
+ "multipart/form-data; boundary=----WebKitFormBoundaryAyGUY6aMxOI6UF5s")
+
+(defvar- upload-content-length 188)
+
+(defvar- upload-body (str-input-stream
+ "------WebKitFormBoundaryAyGUY6aMxOI6UF5s\r\nContent-Disposition: form-data; name=\"upload\"; filename=\"test.txt\"\r\nContent-Type: text/plain\r\n\r\nfoo\r\n\r\n------WebKitFormBoundaryAyGUY6aMxOI6UF5s--"))
+
+(defvar- wrapped-echo (wrap-multipart-params identity))
+
+(deftest test-wrap-multipart-params
+ (let [req {:content-type upload-content-type
+ :content-length upload-content-length
+ :body upload-body
+ :params {"foo" "bar"}}
+ resp (wrapped-echo req)]
+ (is (= "bar" (get-in resp [:params "foo"])))
+ (let [upload (get-in resp [:params "upload"])]
+ (is (= "test.txt" (:filename upload)))
+ (is (= 5 (:size upload)))
+ (is (= "text/plain" (:content-type upload)))
+ (is (instance? File (:tempfile upload)))
+ (is (= "foo\r\n" (du/slurp* (:tempfile upload)))))))

0 comments on commit 7d09995

Please sign in to comment.