Permalink
Browse files

Refactored multipart-params test

  • Loading branch information...
1 parent 955baf6 commit fec4dded1bd57c0aa7260288098fefa1bd313bb8 @weavejester weavejester committed Mar 23, 2011
Showing with 28 additions and 31 deletions.
  1. +28 −31 ring-core/test/ring/middleware/multipart_params_test.clj
View
59 ring-core/test/ring/middleware/multipart_params_test.clj
@@ -1,36 +1,33 @@
(ns ring.middleware.multipart-params-test
(:use clojure.test
- ring.middleware.multipart-params)
- (:require [ring.util.test :as tu])
- (:import java.io.File))
+ ring.middleware.multipart-params
+ ring.util.test)
+ (:import java.io.InputStream))
-(def ^{:private true}
- upload-content-type
- "multipart/form-data; boundary=----WebKitFormBoundaryAyGUY6aMxOI6UF5s")
-
-(def ^{:private true}
- upload-content-length
- 188)
-
-(def ^{:private true}
- upload-body
- (tu/string-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--"))
-
-(def ^{:private true}
- wrapped-echo
- (wrap-multipart-params identity))
+(defn string-store [item]
+ (-> (select-keys item [:filename :content-type])
+ (assoc :content (slurp (:stream item)))))
(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" (slurp (:tempfile upload)))))))
+ (let [form-body (str "--XXXX\r\n"
+ "Content-Disposition: form-data;"
+ "name=\"upload\"; filename=\"test.txt\"\r\n"
+ "Content-Type: text/plain\r\n\r\n"
+ "foo\r\n"
+ "--XXXX\r\n"
+ "Content-Disposition: form-data;"
+ "name=\"baz\"\r\n\r\n"
+ "qux\r\n"
+ "--XXXX--")
+ handler (wrap-multipart-params identity {:store string-store})
+ request {:content-type "multipart/form-data; boundary=XXXX"
+ :content-length (count form-body)
+ :params {"foo" "bar"}
+ :body (string-input-stream form-body)}
+ response (handler request)]
+ (is (= (get-in response [:params "foo"]) "bar"))
+ (is (= (get-in response [:params "baz"]) "qux"))
+ (let [upload (get-in response [:params "upload"])]
+ (is (= (:filename upload) "test.txt"))
+ (is (= (:content-type upload) "text/plain"))
+ (is (= (:content upload) "foo")))))

0 comments on commit fec4dde

Please sign in to comment.