Skip to content

Commit 908ba71

Browse files
authored
Merge pull request #28 from ruroru/master
Fix reflection warnings
2 parents aea2734 + e4ce86d commit 908ba71

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

src/ring/mock/request.clj

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
[clojure.string :as string]
55
[ring.util.codec :as codec]
66
[ring.util.mime-type :as mime])
7-
(:import [java.io ByteArrayInputStream ByteArrayOutputStream File]
7+
(:import [java.io ByteArrayInputStream ByteArrayOutputStream File InputStream]
8+
[java.net URI]
89
[java.nio.charset Charset]
10+
[java.util Map]
911
[org.apache.hc.core5.http ContentType HttpEntity]
1012
[org.apache.hc.client5.http.entity.mime MultipartEntityBuilder]))
1113

@@ -75,9 +77,9 @@
7577
(defmethod body (class (byte-array 0)) [request bytes]
7678
(-> request
7779
(content-length (count bytes))
78-
(assoc :body (java.io.ByteArrayInputStream. bytes))))
80+
(assoc :body (ByteArrayInputStream. bytes))))
7981

80-
(defmethod body java.util.Map [request params]
82+
(defmethod body Map [request params]
8183
(-> request
8284
(content-type "application/x-www-form-urlencoded")
8385
(body (encode-params params))))
@@ -99,11 +101,30 @@
99101
(defn- file? [f]
100102
(instance? File f))
101103

102-
(defn- add-multipart-part [builder k v]
104+
(defn- str->bytes ^bytes [^String s]
105+
(.getBytes s ^Charset default-charset))
106+
107+
(defn- add-binary-body
108+
[^MultipartEntityBuilder builder key value ^ContentType mimetype
109+
^String filename]
110+
(let [k (name key)]
111+
(cond
112+
(string? value)
113+
(.addBinaryBody builder k (str->bytes value) mimetype filename)
114+
(bytes? value)
115+
(.addBinaryBody builder k ^bytes value mimetype filename)
116+
(file? value)
117+
(.addBinaryBody builder k ^File value mimetype filename)
118+
(instance? InputStream value)
119+
(.addBinaryBody builder k ^InputStream value mimetype filename)
120+
:else
121+
(throw (IllegalArgumentException.
122+
(str "Cannot encode a value of type " (type value)
123+
" as a multipart body."))))))
124+
125+
(defn- add-multipart-part [^MultipartEntityBuilder builder k v]
103126
(let [param (if (map? v) v {:value v})
104-
value (if (string? (:value param))
105-
(.getBytes (:value param) default-charset)
106-
(:value param))
127+
value (if (map? v) (:value v) v)
107128
mimetype (ContentType/parse
108129
(or (:content-type param)
109130
(when (file? value)
@@ -113,11 +134,11 @@
113134
"application/octet-stream")))
114135
filename (or (:filename param)
115136
(when (file? value) (.getName ^File value)))]
116-
(.addBinaryBody builder (name k) value mimetype filename)))
137+
(add-binary-body builder (name k) value mimetype filename)))
117138

118139
(defn- multipart-entity ^HttpEntity [params]
119140
(let [builder (MultipartEntityBuilder/create)]
120-
(.setCharset builder default-charset)
141+
(.setCharset builder ^Charset default-charset)
121142
(doseq [[k v] params]
122143
(add-multipart-part builder k v))
123144
(.build builder)))
@@ -157,7 +178,7 @@
157178
([method uri]
158179
(request method uri nil))
159180
([method uri params]
160-
(let [uri (java.net.URI. uri)
181+
(let [uri (URI. uri)
161182
scheme (keyword (or (.getScheme uri) "http"))
162183
host (or (.getHost uri) "localhost")
163184
port (when (not= (.getPort uri) -1) (.getPort uri))

test/ring/mock/request_test.clj

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
(ns ring.mock.request-test
22
(:require [clojure.java.io :as io]
3+
[clojure.spec.alpha :as s]
34
[clojure.test :refer [deftest is testing]]
45
[ring.mock.request :refer [body content-length content-type cookie
56
header json-body multipart-body
6-
query-string request]]))
7+
query-string request]])
8+
(:import [java.io InputStream]))
79

810
(deftest test-request
911
(testing "relative uri"
@@ -130,27 +132,27 @@
130132
(deftest test-body
131133
(testing "string body"
132134
(let [resp (body {} "Hello World")]
133-
(is (instance? java.io.InputStream (:body resp)))
135+
(is (instance? InputStream (:body resp)))
134136
(is (= (slurp (:body resp)) "Hello World"))
135137
(is (= (:content-length resp) 11))))
136138
(testing "map body"
137139
(let [resp (body {} (array-map :foo "bar" :fi ["fi" "fo" "fum"]))]
138-
(is (instance? java.io.InputStream (:body resp)))
140+
(is (instance? InputStream (:body resp)))
139141
(is (= (slurp (:body resp)) "foo=bar&fi=fi&fi=fo&fi=fum"))
140142
(is (= (:content-length resp) 26))
141143
(is (= (:content-type resp)
142144
"application/x-www-form-urlencoded"))))
143145
(testing "bytes body"
144146
(let [resp (body {} (.getBytes "foo"))]
145-
(is (instance? java.io.InputStream (:body resp)))
147+
(is (instance? InputStream (:body resp)))
146148
(is (= (slurp (:body resp)) "foo"))
147149
(is (= (:content-length resp) 3)))))
148150

149151
(deftest test-json-body
150152
(testing "json body"
151153
(let [resp (json-body {} {:baz ["qu" "qi" "qo"]})]
152154
(is (= (:content-type resp) "application/json"))
153-
(is (instance? java.io.InputStream (:body resp)))
155+
(is (instance? InputStream (:body resp)))
154156
(is (= (slurp (:body resp))
155157
"{\"baz\":[\"qu\",\"qi\",\"qo\"]}"))
156158
(is (= (:content-length resp) 24)))))
@@ -229,7 +231,16 @@
229231
"filename=\"test.html\"\r\n"
230232
"Content-Type: text/html\r\n\r\n"
231233
"a\n\r\n"
232-
"--" boundary "--\r\n"))))))
234+
"--" boundary "--\r\n")))))
235+
(testing "not supported type"
236+
(try
237+
(multipart-body {} {:foo :keyword
238+
:bar {:value :keyword
239+
:content-type "text/html"
240+
:filename "test.html"}})
241+
(catch Exception e
242+
(let [expected-message "Cannot encode a value of type class clojure.lang.Keyword as a multipart body."]
243+
(is (= expected-message (.getMessage ^Exception e))))))))
233244

234245
(defmacro when-clojure-spec
235246
[& body]

0 commit comments

Comments
 (0)