PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/test/clj_http/client_test.clj

https://github.com/maxweber/clj-http
Clojure | 292 lines | 235 code | 54 blank | 3 comment | 52 complexity | de6872484543552301f0d79ae0d8a49c MD5 | raw file
  1. (ns clj-http.client-test
  2. (:use clojure.test)
  3. (:require [clj-http.client :as client]
  4. [clj-http.util :as util]
  5. [clojure.contrib.io :as io])
  6. (:import (java.util Arrays)))
  7. (def base-req
  8. {:scheme "http"
  9. :server-name "localhost"
  10. :server-port 8080})
  11. (deftest rountrip
  12. (let [resp (client/request (merge base-req {:uri "/get" :method :get}))]
  13. (is (= 200 (:status resp)))
  14. (is (= "close" (get-in resp [:headers "connection"])))
  15. (is (= "get" (:body resp)))))
  16. (defn is-passed [middleware req]
  17. (let [client (middleware identity)]
  18. (is (= req (client req)))))
  19. (defn is-applied [middleware req-in req-out]
  20. (let [client (middleware identity)]
  21. (is (= req-out (client req-in)))))
  22. (deftest parse-url-test
  23. (is (= {:scheme "http"
  24. :server-name "bar.com"
  25. :server-port nil
  26. :uri "/bat"
  27. :query-string nil}
  28. (client/parse-url "http://bar.com/bat#cat")))
  29. (is (= {:scheme "http"
  30. :server-name "foo.com"
  31. :server-port nil
  32. :uri "/blargh"
  33. :query-string "args=true"}
  34. (client/parse-url "http://foo.com/blargh?args=true")))
  35. (is (= {:scheme "http"
  36. :server-name "mud.com"
  37. :server-port 8080
  38. :uri "/gnarl"
  39. :query-string "boom=true"}
  40. (client/parse-url "http://mud.com:8080/gnarl?boom=true"))))
  41. (deftest ensure-proper-url-test
  42. (is (= "http://host.com/path"
  43. (client/ensure-proper-url "/path" "http" "host.com")))
  44. (is (= "https://foo.com/path"
  45. (client/ensure-proper-url "foo.com/path" "https" "bar.com"))))
  46. ;; http://f.com:443/orig -- /target -> http://f.com:443/doh
  47. ;; http://g.com/old -- /new -> http://g.com/new
  48. ;; http://h.com:8080/old -- http://hh.com/new -> http://hh.com/new
  49. (deftest follow-redirect-test
  50. (let [client identity
  51. req (client/parse-url "http://mud.com:8080/gnarl?boom=true")
  52. resp {:headers {"location" "/rad?arg=foo"}}
  53. red-req (client/follow-redirect client req resp)]
  54. (is (= "http"
  55. (:scheme red-req)))
  56. (is (= "mud.com"
  57. (:server-name red-req)))
  58. (is (= 8080
  59. (:server-port red-req)))
  60. (is (= "/rad"
  61. (:uri red-req)))
  62. (is (= "arg=foo"
  63. (:query-string red-req)))))
  64. (deftest redirect-on-get
  65. (let [client (fn [req]
  66. (cond
  67. (= "foo.com" (:server-name req))
  68. {:status 301
  69. :headers {"location" "http://deal.com"}}
  70. (= "deal.com" (:server-name req))
  71. {:status 302
  72. :headers {"location" "http://bar.com/bat?more=yes&x=3"}}
  73. (= "bar.com" (:server-name req))
  74. {:status 200
  75. :req req}))
  76. r-client (client/wrap-redirects client)
  77. resp (r-client {:scheme "http"
  78. :server-name "foo.com"
  79. :request-method :get})]
  80. (is (= {:status 200
  81. :req {:request-method :get
  82. :scheme "http"
  83. :server-name "bar.com"
  84. :server-port nil
  85. :uri "/bat"
  86. :query-string "more=yes&x=3"}}
  87. resp))
  88. (is (= 200 (:status resp)))
  89. (is (= :get (:request-method (:req resp))))
  90. (is (= "http" (:scheme (:req resp))))
  91. (is (= "/bat" (:uri (:req resp))))
  92. (is (= "more=yes&x=3" (:query-string (:req resp))))))
  93. (deftest redirect-to-get-on-head
  94. (let [client (fn [req]
  95. (if (= "foo.com" (:server-name req))
  96. {:status 303
  97. :headers {"location" "http://bar.com/bat"}}
  98. {:status 200
  99. :req req}))
  100. r-client (client/wrap-redirects client)
  101. resp (r-client {:scheme "http"
  102. :server-name "foo.com"
  103. :request-method :head})]
  104. (is (= 200 (:status resp)))
  105. (is (= :get (:request-method (:req resp))))
  106. (is (= "http" (:scheme (:req resp))))
  107. (is (= "/bat" (:uri (:req resp))))))
  108. (deftest pass-on-non-redirect
  109. (let [client (fn [req] {:status 200 :body (:body req)})
  110. r-client (client/wrap-redirects client)
  111. resp (r-client {:body "ok"})]
  112. (is (= 200 (:status resp)))
  113. (is (= "ok" (:body resp)))))
  114. (deftest throw-on-exceptional
  115. (let [client (fn [req] {:status 500 :body "No worky."})
  116. e-client (client/wrap-exceptions client)]
  117. (is (thrown-with-msg? Exception #"500"
  118. (e-client {})))))
  119. (deftest pass-on-non-exceptional
  120. (let [client (fn [req] {:status 200})
  121. e-client (client/wrap-exceptions client)
  122. resp (e-client {})]
  123. (is (= 200 (:status resp)))))
  124. (deftest pass-on-exceptional-when-surpressed
  125. (let [client (fn [req] {:status 500})
  126. e-client (client/wrap-exceptions client)
  127. resp (e-client {:throw-exceptions false})]
  128. (is (= 500 (:status resp)))))
  129. (deftest apply-on-compressed
  130. (let [client (fn [req] {:body (-> "foofoofoo"
  131. .getBytes
  132. util/gzip
  133. java.io.ByteArrayInputStream.)
  134. :headers {"Content-Encoding" "gzip"}})
  135. c-client (client/wrap-decompression client)
  136. resp (c-client {})]
  137. (is (= "foofoofoo" (-> resp :body io/slurp*)))))
  138. (deftest apply-on-deflated
  139. (let [client (fn [req] {:body (-> "barbarbar" .getBytes
  140. util/deflate
  141. java.io.ByteArrayInputStream.)
  142. :headers {"Content-Encoding" "deflate"}})
  143. c-client (client/wrap-decompression client)
  144. resp (c-client {})]
  145. (is (= "barbarbar" (io/slurp* (:body resp))))))
  146. (deftest pass-on-non-compressed
  147. (let [c-client (client/wrap-decompression (fn [req] {:body "foo"}))
  148. resp (c-client {:uri "/foo"})]
  149. (is (= "foo" (:body resp)))))
  150. (deftest apply-on-accept
  151. (is-applied client/wrap-accept
  152. {:accept :json}
  153. {:headers {"Accept" "application/json"}}))
  154. (deftest pass-on-no-accept
  155. (is-passed client/wrap-accept
  156. {:uri "/foo"}))
  157. (deftest apply-on-accept-encoding
  158. (is-applied client/wrap-accept-encoding
  159. {:accept-encoding [:identity :gzip]}
  160. {:headers {"Accept-Encoding" "identity, gzip"}}))
  161. (deftest pass-on-no-accept-encoding
  162. (is-passed client/wrap-accept-encoding
  163. {:uri "/foo"}))
  164. (deftest apply-on-output-coercion
  165. (let [client (fn [req] {:body (io/input-stream (.getBytes "foo"))})
  166. o-client (client/wrap-output-coercion client)
  167. resp (o-client {:uri "/foo"})]
  168. (is (= "foo" (:body resp)))))
  169. (deftest pass-on-no-output-coercion
  170. (let [client (fn [req] {:body nil})
  171. o-client (client/wrap-output-coercion client)
  172. resp (o-client {:uri "/foo"})]
  173. (is (nil? (:body resp))))
  174. (let [client (fn [req] {:body :thebytes})
  175. o-client (client/wrap-output-coercion client)
  176. resp (o-client {:uri "/foo" :as :byte-array})]
  177. (is (= :thebytes (:body resp)))))
  178. (deftest apply-on-input-coercion
  179. (let [i-client (client/wrap-input-coercion identity)
  180. resp (i-client {:body "foo"})]
  181. (is (= "UTF-8" (:character-encoding resp)))
  182. (is (Arrays/equals (util/utf8-bytes "foo") (:body resp)))))
  183. (deftest pass-on-no-input-coercion
  184. (is-passed client/wrap-input-coercion
  185. {:body (util/utf8-bytes "foo")}))
  186. (deftest apply-on-content-type
  187. (is-applied client/wrap-content-type
  188. {:content-type :json}
  189. {:content-type "application/json"}))
  190. (deftest pass-on-no-content-type
  191. (is-passed client/wrap-content-type
  192. {:uri "/foo"}))
  193. (deftest apply-on-query-params
  194. (is-applied client/wrap-query-params
  195. {:query-params {"foo" "bar" "dir" "<<"}}
  196. {:query-string "foo=bar&dir=%3C%3C"}))
  197. (deftest pass-on-no-query-params
  198. (is-passed client/wrap-query-params
  199. {:uri "/foo"}))
  200. (deftest apply-on-basic-auth
  201. (is-applied client/wrap-basic-auth
  202. {:basic-auth ["Aladdin" "open sesame"]}
  203. {:headers {"Authorization" "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="}}))
  204. (deftest pass-on-no-basic-auth
  205. (is-passed client/wrap-basic-auth
  206. {:uri "/foo"}))
  207. (deftest apply-on-method
  208. (let [m-client (client/wrap-method identity)
  209. echo (m-client {:key :val :method :post})]
  210. (is (= :val (:key echo)))
  211. (is (= :post (:request-method echo)))
  212. (is (not (:method echo)))))
  213. (deftest pass-on-no-method
  214. (let [m-client (client/wrap-method identity)
  215. echo (m-client {:key :val})]
  216. (is (= :val (:key echo)))
  217. (is (not (:request-method echo)))))
  218. (deftest apply-on-url
  219. (let [u-client (client/wrap-url identity)
  220. resp (u-client {:url "http://google.com:8080/foo?bar=bat"})]
  221. (is (= "http" (:scheme resp)))
  222. (is (= "google.com" (:server-name resp)))
  223. (is (= 8080 (:server-port resp)))
  224. (is (= "/foo" (:uri resp)))
  225. (is (= "bar=bat" (:query-string resp)))))
  226. (deftest pass-on-no-url
  227. (let [u-client (client/wrap-url identity)
  228. resp (u-client {:uri "/foo"})]
  229. (is (= "/foo" (:uri resp)))))
  230. (deftest chunked-request-test
  231. (let [client (fn [req]
  232. {:body (-> "1\r\na\r\n3\r\nfoor\r\n0\r\n\r\n"
  233. .getBytes
  234. io/input-stream)
  235. :headers {"transfer-encoding" "chunked"}})
  236. o-client (client/wrap-output-coercion client)
  237. resp (o-client {:chunked? true})]
  238. (is (= ["a" "foo"] (:body resp)))))