Skip to content

Commit

Permalink
Added a check for POST method before encoding params in the body
Browse files Browse the repository at this point in the history
  • Loading branch information
senior committed Sep 23, 2011
1 parent 897dbf6 commit a743bf9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/clj_http/client.clj
Expand Up @@ -165,8 +165,8 @@
(client req))))

(defn wrap-form-params [client]
(fn [{:keys (form-params) :as req}]
(if form-params
(fn [{:keys (form-params request-method) :as req}]
(if (and form-params (= :post request-method))
(client (-> req
(dissoc :form-params)
(assoc :content-type (content-type-value :x-www-form-urlencoded)
Expand Down
11 changes: 10 additions & 1 deletion test/clj_http/client_test.clj
Expand Up @@ -227,11 +227,20 @@
(deftest apply-on-form-params
(testing "With form params"
(let [param-client (client/wrap-form-params identity)
resp (param-client {:form-params {:param1 "value1"
resp (param-client {:request-method :post
:form-params {:param1 "value1"
:param2 "value2"}})]
(is (= "param1=value1&param2=value2" (:body resp)))
(is (= "application/x-www-form-urlencoded" (:content-type resp)))
(is (not (contains? resp :form-params)))))
(testing "Ensure it does not affect GET requests"
(let [param-client (client/wrap-form-params identity)
resp (param-client {:request-method :get
:body "untouched"
:form-params {:param1 "value1"
:param2 "value2"}})]
(is (= "untouched" (:body resp)))
(is (not (contains? resp :content-type)))))

(testing "with no form params"
(let [param-client (client/wrap-form-params identity)
Expand Down

0 comments on commit a743bf9

Please sign in to comment.