Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit of the code and some tests
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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)))) | |||
|
|||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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) |