Skip to content
This repository has been archived by the owner on Jan 23, 2018. It is now read-only.

Commit

Permalink
Merge from master.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed May 5, 2009
2 parents c11f148 + d218e07 commit c6e960e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/clojure/http/client.clj
@@ -1,6 +1,6 @@
(ns clojure.http.client
(:use [clojure.contrib.java-utils :only [as-str]]
[clojure.contrib.duck-streams :only [read-lines]]
[clojure.contrib.duck-streams :only [read-lines writer]]
[clojure.contrib.str-utils :only [str-join]])
(:import (java.net URL HttpURLConnection URLEncoder)
(java.io StringReader)))
Expand All @@ -15,6 +15,11 @@ representation of text."
[text]
(URLEncoder/encode text "UTF-8"))

(defn- encode-body-map
"Turns a map into a URL-encoded string suitable for a request body."
[body]
(str-join "&" (map #(str-join "=" (map url-encode %)) body)))

(defn url
"If u is an instance of java.net.URL then returns it without
modification, otherwise tries to instantiate a java.net.URL with
Expand Down Expand Up @@ -88,11 +93,10 @@ by a server."
"Content-Type"
"application/x-www-form-urlencoded")
(.connect connection)
(.write (.getOutputStream connection)
(.write (writer (.getOutputStream connection))
(if (isa? body String)
body
;; TODO: keys/values need to be URL-encoded
(str-join "&" (map #(str-join "=" %) body)))))
(encode-body-map body))))
(.connect connection))

(let [headers (parse-headers connection)]
Expand Down
10 changes: 8 additions & 2 deletions test/clojure/http/test/client.clj
Expand Up @@ -11,7 +11,7 @@

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

(deftest case-insensitive-headers
(let [response (request (str "http://localhost:" test-port))]
(is (= "text/plain" ((:get-header response) "content-type")))))
(is (= "text/plain" ((:get-header response) "content-type")))))

;; need echo response to work with body before this will work.
;; (deftest request-body
;; (let [response (request (str "http://localhost:" test-port)
;; :get {} {} {"hey" "düde"})]
;; (is (some #{"o=hai+dere&hey=d%C3%B6od"} (:body-seq response)))))

0 comments on commit c6e960e

Please sign in to comment.