Skip to content

Commit

Permalink
initial commit of the code and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heyZeus committed Mar 3, 2009
1 parent 2db1610 commit f4d7a50
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
82 changes: 82 additions & 0 deletions http_client.clj
@@ -0,0 +1,82 @@
(ns http-client
(:import (org.apache.commons.httpclient HttpClient NameValuePair URI)
(org.apache.commons.httpclient.cookie CookiePolicy CookieSpec)
(org.apache.commons.httpclient.methods GetMethod PostMethod DeleteMethod
TraceMethod HeadMethod PutMethod)))

(defmacro send-uri
"Sends a request to the given uri and client. The reponse from the server is
stored in the uri and any cookies are stored in the client. The response and
any resources associated with the request are cleared from the uri after this
function is called."
[client uri & body]
`(try
(.executeMethod ~client ~uri)
~@body
(finally (.releaseConnection ~uri))))

(defn client
"Creates a HttpClient for the given server."
[server]
(let [c (HttpClient.)]
(.. c (getHostConfiguration) (setHost (URI. server true)))
c))

(defn uri
"Creates a commons-client method type object with the given path and method.
A method can be one of: :post, :get, :put, :delete, :trace or :head. If no
method is supplied :get is the default. You can supply a url-params hash like:
{:login \"foo\" :password \"bar\"}."
([path method url-params]
(let [key-method (cond
(> (count url-params) 0) :post
(nil? method) :get
:else (keyword method))
m (cond
(= :post key-method) (PostMethod. path)
(= :delete key-method) (DeleteMethod. path)
(= :put key-method) (PutMethod. path)
(= :trace key-method) (TraceMethod. path)
(= :head key-method) (HeadMethod. path)
(= :get key-method) (GetMethod. path)
:else (GetMethod. path))]
(doseq [[k v] url-params]
(.addParameter m (name k) (str v)))
m))
([path method] (uri path method nil))
([path] (uri path nil nil)))

(defn cookies
"Convience function to get the cookies from the client."
[client]
(.. client (getState) (getCookies)))

(defn print-cookies
"Prints the cookies from the client."
[client]
(doseq [c (cookies client)] (println c)))

(defn response-str
"Returns the response from the uri as a string."
[client]
[uri]
(.getResponseBodyAsString uri))

(defn assert-cookie-names
"Returns true if all of the given cookie-names exist in the client."
[client & cookie-names]
(let [actual-cookies (cookies client)]
(every? (fn [exp-cookie-name]
(some #(= exp-cookie-name (.getName %1)) actual-cookies))
cookie-names)))


(comment

; Prints the HTML of the clojure.org website
(let [client (client "http://www.clojure.org")
login (uri "/")]
(send-uri client login
(println (response-str login))))

)
26 changes: 26 additions & 0 deletions test/main.clj
@@ -0,0 +1,26 @@
(ns http-client.test.main
(:require [http-client :as hc])
(:use clojure.contrib.test-is))

(def client (hc/client "http://www.clojure.org"))

(deftest client
(let [host "www.clojure.org"
client (hc/client (str "http://" host))]
(is (= (.. client (getHostConfiguration) getHost) host))))

(deftest uri
(let [default-get-uri (hc/uri "/")
path-post-uri (hc/uri "/api" :post)
params-uri (hc/uri "/" :get {:language "clojure" :happy "yes"})]
(is (= "GET" (.getName default-get-uri)))
(is (= "/" (.getPath default-get-uri)))

(is (= "POST" (.getName path-post-uri)))
(is (= "/api" (.getPath path-post-uri)))

(is (= "POST" (.getName params-uri)))
(is (= "clojure" (.. params-uri (getParameter "language") (getValue))))
(is (= "yes" (.. params-uri (getParameter "happy") (getValue))))))

(run-tests)

0 comments on commit f4d7a50

Please sign in to comment.