Skip to content

Commit 1c71e2a

Browse files
committed
Add support for sets when form-encoding maps
1 parent 34e3329 commit 1c71e2a

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/ring/util/codec.clj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@
9696
Map
9797
(form-encode* [params encoding]
9898
(letfn [(encode [x] (form-encode* x encoding))
99-
(encode-param [[k v]] (str (encode (name k)) "=" (encode v)))]
99+
(encode-param [k v] (str (encode (name k)) "=" (encode v)))]
100100
(->> params
101101
(mapcat
102102
(fn [[k v]]
103-
(if (or (seq? v) (sequential? v) )
104-
(map #(encode-param [k %]) v)
105-
[(encode-param [k v])])))
103+
(cond
104+
(sequential? v) (map #(encode-param k %) v)
105+
(set? v) (sort (map #(encode-param k %) v))
106+
:else (list (encode-param k v)))))
106107
(str/join "&"))))
107108
Object
108109
(form-encode* [x encoding]

test/ring/util/test/codec.clj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@
3939
(is (= (form-encode "foo/bar" "UTF-16") "foo%FE%FF%00%2Fbar")))
4040
(testing "maps"
4141
(are [x y] (= (form-encode x) y)
42-
{"a" "b"} "a=b"
43-
{:a "b"} "a=b"
44-
{"a" 1} "a=1"
45-
{"a" nil} "a="
42+
{"a" "b"} "a=b"
43+
{:a "b"} "a=b"
44+
{"a" 1} "a=1"
45+
{"a" nil} "a="
4646
{"a" "b" "c" "d"} "a=b&c=d"
47-
{"a" "b c"} "a=b+c")
47+
{"a" "b c"} "a=b+c"
48+
{"a" ["b" "c"]} "a=b&a=c"
49+
{"a" ["c" "b"]} "a=c&a=b"
50+
{"a" (seq [1 2])} "a=1&a=2"
51+
{"a" #{"c" "b"}} "a=b&a=c")
4852
(is (= (form-encode {"a" "foo/bar"} "UTF-16") "a=foo%FE%FF%00%2Fbar"))))
4953

5054
(deftest test-form-decode-str

0 commit comments

Comments
 (0)