Skip to content

Commit c6e960e

Browse files
committed
Merge from master.
2 parents c11f148 + d218e07 commit c6e960e

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/clojure/http/client.clj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns clojure.http.client
22
(:use [clojure.contrib.java-utils :only [as-str]]
3-
[clojure.contrib.duck-streams :only [read-lines]]
3+
[clojure.contrib.duck-streams :only [read-lines writer]]
44
[clojure.contrib.str-utils :only [str-join]])
55
(:import (java.net URL HttpURLConnection URLEncoder)
66
(java.io StringReader)))
@@ -15,6 +15,11 @@ representation of text."
1515
[text]
1616
(URLEncoder/encode text "UTF-8"))
1717

18+
(defn- encode-body-map
19+
"Turns a map into a URL-encoded string suitable for a request body."
20+
[body]
21+
(str-join "&" (map #(str-join "=" (map url-encode %)) body)))
22+
1823
(defn url
1924
"If u is an instance of java.net.URL then returns it without
2025
modification, otherwise tries to instantiate a java.net.URL with
@@ -88,11 +93,10 @@ by a server."
8893
"Content-Type"
8994
"application/x-www-form-urlencoded")
9095
(.connect connection)
91-
(.write (.getOutputStream connection)
96+
(.write (writer (.getOutputStream connection))
9297
(if (isa? body String)
9398
body
94-
;; TODO: keys/values need to be URL-encoded
95-
(str-join "&" (map #(str-join "=" %) body)))))
99+
(encode-body-map body))))
96100
(.connect connection))
97101

98102
(let [headers (parse-headers connection)]

test/clojure/http/test/client.clj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
(defn response-headers
1313
([headers]
14-
(reduce #(conj %1 (str (first %2) ": " (second %2)))
14+
(reduce #(conj %1 (str-join ":" %2))
1515
'("HTTP/1.1 200 OK"
1616
"Server: clojure-http-test-client"
1717
"Content-Type: text/plain")
@@ -46,4 +46,10 @@
4646

4747
(deftest case-insensitive-headers
4848
(let [response (request (str "http://localhost:" test-port))]
49-
(is (= "text/plain" ((:get-header response) "content-type")))))
49+
(is (= "text/plain" ((:get-header response) "content-type")))))
50+
51+
;; need echo response to work with body before this will work.
52+
;; (deftest request-body
53+
;; (let [response (request (str "http://localhost:" test-port)
54+
;; :get {} {} {"hey" "düde"})]
55+
;; (is (some #{"o=hai+dere&hey=d%C3%B6od"} (:body-seq response)))))

0 commit comments

Comments
 (0)