Permalink
Browse files

renamed uri to method to match the java classes

  • Loading branch information...
1 parent af28f6e commit a9bd1107d0a3275702dc322d75f636766896569f @heyZeus committed Mar 4, 2009
Showing with 29 additions and 29 deletions.
  1. +18 −18 http_client.clj
  2. +11 −11 test/main.clj
View
@@ -4,16 +4,16 @@
(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
+(defmacro send-method
+ "Sends a request to the given method and client. The reponse from the server is
+ stored in the method and any cookies are stored in the client. The response and
+ any resources associated with the request are cleared from the method after this
function is called."
- [client uri & body]
+ [client method & body]
`(try
- (.executeMethod ~client ~uri)
+ (.executeMethod ~client ~method)
~@body
- (finally (.releaseConnection ~uri))))
+ (finally (.releaseConnection ~method))))
(defn client
"Creates a HttpClient for the given server."
@@ -22,16 +22,16 @@
(.. c (getHostConfiguration) (setHost (URI. server true)))
c))
-(defn uri
+(defn method
"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]
+ ([path method-type url-params]
(let [key-method (cond
(> (count url-params) 0) :post
- (nil? method) :get
- :else (keyword method))
+ (nil? method-type) :get
+ :else (keyword method-type))
m (cond
(= :post key-method) (PostMethod. path)
(= :delete key-method) (DeleteMethod. path)
@@ -43,8 +43,8 @@
(doseq [[k v] url-params]
(.addParameter m (name k) (str v)))
m))
- ([path method] (uri path method nil))
- ([path] (uri path nil nil)))
+ ([path method-type] (method path method-type nil))
+ ([path] (method path nil nil)))
(defn cookies
"Convience function to get the cookies from the client."
@@ -57,10 +57,10 @@
(doseq [c (cookies client)] (println c)))
(defn response-str
- "Returns the response from the uri as a string."
+ "Returns the response from the method as a string."
[client]
- [uri]
- (.getResponseBodyAsString uri))
+ [method]
+ (.getResponseBodyAsString method))
(defn assert-cookie-names
"Returns true if all of the given cookie-names exist in the client."
@@ -75,8 +75,8 @@
; Prints the HTML of the clojure.org website
(let [client (client "http://www.clojure.org")
- login (uri "/")]
- (send-uri client login
+ login (method "/")]
+ (send-method client login
(println (response-str login))))
)
View
@@ -9,18 +9,18 @@
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)))
+(deftest method
+ (let [default-get-method (hc/method "/")
+ path-post-method (hc/method "/api" :post)
+ params-method (hc/method "/" :get {:language "clojure" :happy "yes"})]
+ (is (= "GET" (.getName default-get-method)))
+ (is (= "/" (.getPath default-get-method)))
- (is (= "POST" (.getName path-post-uri)))
- (is (= "/api" (.getPath path-post-uri)))
+ (is (= "POST" (.getName path-post-method)))
+ (is (= "/api" (.getPath path-post-method)))
- (is (= "POST" (.getName params-uri)))
- (is (= "clojure" (.. params-uri (getParameter "language") (getValue))))
- (is (= "yes" (.. params-uri (getParameter "happy") (getValue))))))
+ (is (= "POST" (.getName params-method)))
+ (is (= "clojure" (.. params-method (getParameter "language") (getValue))))
+ (is (= "yes" (.. params-method (getParameter "happy") (getValue))))))
(run-tests)

0 comments on commit a9bd110

Please sign in to comment.