Skip to content

Commit 4595069

Browse files
committed
Made urlencoded-form? function public in ring.util.request
1 parent 8869d3b commit 4595069

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Deprecated :content-type, :content-length and :character-encoding keys in SPEC
44
* Removed deprecated keys from source code
55
* Added content-length, content-type and character-encoding to ring.util.request
6+
* Added urlencoded-form? to ring.util.request
67
* Fixed 304 not-modified responses to set content-length header
78
* Added options to wrap-cookies to specify encoder and decoder functions
89
* Fixed wrap-head middleware when response is nil

ring-core/src/ring/middleware/params.clj

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@
1818
{:query-params params, :params params})
1919
{:query-params {}, :params {}})))
2020

21-
(defn- urlencoded-form?
22-
"Does a request have a urlencoded form?"
23-
[request]
24-
(if-let [^String type (req/content-type request)]
25-
(.startsWith type "application/x-www-form-urlencoded")))
26-
2721
(defn assoc-form-params
2822
"Parse and assoc parameters from the request body with the request."
2923
{:added "1.2"}
3024
[request encoding]
3125
(merge-with merge request
32-
(if-let [body (and (urlencoded-form? request) (:body request))]
26+
(if-let [body (and (req/urlencoded-form? request) (:body request))]
3327
(let [params (parse-params (slurp body :encoding encoding) encoding)]
3428
{:form-params params, :params params})
3529
{:form-params {}, :params {}})))

ring-core/src/ring/util/request.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
(if-let [type (get-in request [:headers "content-type"])]
3838
(second (re-find charset-pattern type))))
3939

40+
(defn urlencoded-form?
41+
"True if a request contains a urlencoded form in the body."
42+
{:added "1.3"}
43+
[request]
44+
(if-let [^String type (content-type request)]
45+
(.startsWith type "application/x-www-form-urlencoded")))
46+
4047
(defmulti body-string
4148
"Return the request body as a string."
4249
{:arglists '([request]), :added "1.2"}

ring-core/test/ring/util/test/request.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
(is (= (character-encoding {:headers {"content-type" "text/plain;charset=UTF-8"}})
4646
"UTF-8"))))
4747

48+
(deftest test-urlencoded-form?
49+
(testing "urlencoded form"
50+
(is (urlencoded-form? {:headers {"content-type" "application/x-www-form-urlencoded"}}))
51+
(is (urlencoded-form?
52+
{:headers {"content-type" "application/x-www-form-urlencoded; charset=UTF-8"}})))
53+
(testing "other content type"
54+
(is (not (urlencoded-form? {:headers {"content-type" "application/json"}}))))
55+
(testing "no content type"
56+
(is (not (urlencoded-form? {:headers {}})))))
57+
4858
(deftest test-body-string
4959
(testing "nil body"
5060
(is (= (body-string {:body nil}) nil)))

0 commit comments

Comments
 (0)