Skip to content

Commit e9781f6

Browse files
author
jj
committed
Fix reflection warnings
1 parent aea2734 commit e9781f6

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ pom.xml.asc
1414
.lein-repl-history
1515
/.cpcache
1616
/.clj-kondo
17+
*.iml
18+
/.idea/

src/ring/mock/request.clj

Lines changed: 17 additions & 8 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,10 +101,10 @@
99101
(defn- file? [f]
100102
(instance? File f))
101103

102-
(defn- add-multipart-part [builder k v]
104+
(defn- add-multipart-part [^MultipartEntityBuilder builder k v]
103105
(let [param (if (map? v) v {:value v})
104106
value (if (string? (:value param))
105-
(.getBytes (:value param) default-charset)
107+
(.getBytes ^String (:value param) ^Charset default-charset)
106108
(:value param))
107109
mimetype (ContentType/parse
108110
(or (:content-type param)
@@ -113,11 +115,18 @@
113115
"application/octet-stream")))
114116
filename (or (:filename param)
115117
(when (file? value) (.getName ^File value)))]
116-
(.addBinaryBody builder (name k) value mimetype filename)))
118+
119+
(cond
120+
(bytes? value) (.addBinaryBody builder ^String (name k) ^"[B" value ^ContentType mimetype ^String filename)
121+
(file? value) (.addBinaryBody builder ^String (name k) ^File value ^ContentType mimetype ^String filename)
122+
(instance? InputStream value) (.addBinaryBody builder ^String (name k) ^InputStream value ^ContentType mimetype ^String filename)
123+
:else
124+
builder
125+
)))
117126

118127
(defn- multipart-entity ^HttpEntity [params]
119128
(let [builder (MultipartEntityBuilder/create)]
120-
(.setCharset builder default-charset)
129+
(.setCharset builder ^Charset default-charset)
121130
(doseq [[k v] params]
122131
(add-multipart-part builder k v))
123132
(.build builder)))
@@ -157,7 +166,7 @@
157166
([method uri]
158167
(request method uri nil))
159168
([method uri params]
160-
(let [uri (java.net.URI. uri)
169+
(let [uri (URI. uri)
161170
scheme (keyword (or (.getScheme uri) "http"))
162171
host (or (.getHost uri) "localhost")
163172
port (when (not= (.getPort uri) -1) (.getPort uri))

test/ring/mock/request_test.clj

Lines changed: 7 additions & 5 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)))))

0 commit comments

Comments
 (0)