Permalink
Browse files

updated and fixed tests with bumped clj-http version

  • Loading branch information...
1 parent a9184bc commit 57e9a0a26f002f1c6cf92e6634258ca4e29f249f @ngrunwald committed Jun 21, 2012
Showing with 88 additions and 63 deletions.
  1. +59 −46 test/clj_spore/test/core.clj
  2. +29 −17 test/clj_spore/test/generator.clj
@@ -2,54 +2,67 @@
(:use [clj-spore] :reload)
(:use [slingshot.slingshot :only [throw+ try+]])
(:use [clj-spore.middleware] :reload)
- (:use [clojure.test]
- [clojure.contrib.with-ns]
- [clojure.contrib.mock.test-adapter]))
+ (:use [clojure.test]))
(deftest test-load-spec
(let [client (load-spec-from-file "test/ihackernews.json")]
- (expect [clj-http.core/request (calls (fn [arg] {:status 200}))]
- (testing "Mocked requests"
- (is (map? client) "client generated")
- (is (function? (client :askhn_posts)) "function generated")
- (is (=
- ((client :askhn_posts))
- {:status 200})
- "request works")))))
+ (with-redefs
+ {#'clj-http.core/request (fn [arg] {:status 200})}
+ (testing "Mocked requests"
+ (is (map? client) "client generated")
+ (is (function? (client :askhn_posts)) "function generated")))))
(deftest test-client-creation
- (expect [clj-http.core/request (calls (fn [req] {:status 200 :body (:body req) :headers { "content-type" (get-in req [:headers "accept"]) } :request req}))]
- (testing "Simple middlewares"
- (let [client-simple (load-spec-from-file "test/ihackernews.json" :middlewares [wrap-json-format wrap-runtime])
- res ((client-simple :vote) :payload {:bar "barbu"})]
- (is (= (:status res) 200) "request works")
- (is (= (:status res) 200) "request works")
- (is (and (get-in res [:headers "x-spore-runtime"])) "runtime middleware works")
- (is (= (get-in res [:headers "content-type"]) "application/json") "content-type response header is good")
- (is (= (get (:decoded-body res) "bar") "barbu") "decoded-body is good")
- (testing "Simple overload"
- (let [client-overload (load-spec-from-file "test/ihackernews.json" :overload {:base_url "http://grunwald.fr"})
- res ((client-overload :vote))]
- (is (= (:status res) 200) "request works")
- (is (= (:server-name (:request res)) "grunwald.fr") "overloading works")))
- (testing "Complex middleware"
- (let [client-complex (load-spec-from-file "test/ihackernews.json" :middlewares [[wrap-clojure-response
- :enabled-if #(not= (:path %) "/login")
- :args [:type "application/x-clojure"]]])
- res-vote ((client-complex :vote))
- res-login ((client-complex :auth_token))]
- (is (= (:status res-vote) 200) "request works")
- (is (= (get-in (:request res-vote) [:headers "accept"]) "application/x-clojure"))
- (is (not= (get-in (:request res-login) [:headers "accept"]) "application/x-clojure")))))))
- (expect [clj-http.core/request (calls (fn [req]
- {:status 205 :body (:body req)
- :headers {"content-type" (get-in req [:headers "accept"])} :request req}))]
- (testing "Exception handling"
- (let [client-simple (load-spec-from-file "test/ihackernews.json")]
- (try+ ((client-simple :askhn_posts))
- (catch Object o
- (is (= (:type o) :unexpected-status))))
- (try+ ((client-simple :user_profile))
- (catch Object o
- (is (= (:type o) :missing-params))
- (is (= (:missing-params o) '("userid")))))))))
+ (with-redefs
+ [clj-http.core/request
+ (fn [{:keys [body] :as req}]
+ (let [b (if body
+ (let [l (.getContentLength body)
+ arr (byte-array l)]
+ (with-open [stream (.getContent body)]
+ (.read stream arr))
+ arr))]
+ {:status 200
+ :body b
+ :headers {"content-type" (get-in req [:headers "accept"])}
+ :request req}))]
+ (testing "Simple middlewares"
+ (let [client-simple (load-spec-from-file "test/ihackernews.json" :middlewares [wrap-json-format wrap-runtime])
+ res ((client-simple :vote) :payload {:bar "barbu"})]
+ (is (= (:status res) 200) "request works")
+ (is (= (:status res) 200) "request works")
+ (is (and (get-in res [:headers "x-spore-runtime"])) "runtime middleware works")
+ (is (= (get-in res [:headers "content-type"]) "application/json") "content-type response header is good")
+ (is (= (get (:decoded-body res) "bar") "barbu") "decoded-body is good")
+ (testing "Simple overload"
+ (let [client-overload (load-spec-from-file "test/ihackernews.json" :overload {:base_url "http://grunwald.fr"})
+ res ((client-overload :vote))]
+ (is (= (:status res) 200) "request works")
+ (is (= (:server-name (:request res)) "grunwald.fr") "overloading works")))
+ (testing "Complex middleware"
+ (let [client-complex (load-spec-from-file
+ "test/ihackernews.json"
+ :middlewares [[wrap-clojure-response
+ :enabled-if #(not= (:path %) "/login")
+ :args [:type "application/x-clojure"]]])
+ res-vote ((client-complex :vote))
+ res-login ((client-complex :auth_token))]
+ (is (= (:status res-vote) 200) "request works")
+ (is (= (get-in (:request res-vote) [:headers "accept"]) "application/x-clojure"))
+ (is (not= (get-in (:request res-login) [:headers "accept"]) "application/x-clojure")))))))
+ (with-redefs
+ [clj-http.core/request
+ (fn [req]
+ {:status 205
+ :body (:body req)
+ :headers {"content-type" (get-in req [:headers "accept"])}
+ :request req})]
+ (testing "Exception handling"
+ (let [client-simple (load-spec-from-file "test/ihackernews.json")]
+ (try+ ((client-simple :askhn_posts))
+ (catch Object o
+ (is (= (:type o) :unexpected-status))))
+ (try+ ((client-simple :user_profile))
+ (catch Object o
+ (is (= (:type o) :missing-params))
+ (is (= (:missing-params o) '("userid")))))))))
@@ -1,8 +1,6 @@
(ns clj-spore.test.generator
(:use [clj-spore.generator] :reload)
- (:use [clojure.test]
- [clojure.contrib.with-ns]
- [clojure.contrib.mock.test-adapter]))
+ (:use [clojure.test]))
(deftest params
(testing "Missing Params"
@@ -25,17 +23,31 @@
func-get (generate-spore-method api m-spec-get (str method_name "_get") [] {})
m-spec-post {:method "POST" :path "/:foo/path" :optional_params ["bar"] :required_params ["foo"] :expected_status [200]}
func-post (generate-spore-method api m-spec-post (str method_name "_post") [] {})]
- (expect [clj-http.core/request (calls (fn [arg] (assoc (filter-keys arg #{:request-method :scheme :server-name :uri :query-string :body}) :status 200)))]
- (testing "Mocked requests"
- (is (function? func-get) "get function generated")
- (is (=
- (func-get :foo "foo1" :bar "bar1")
- {:request-method :get, :scheme "http", :server-name "example.com",
- :uri "/api/foo1/path", :query-string "bar=bar1" :body nil :status 200})
- "get request params are good")
- (is (function? func-post) "post function generated")
- (is (=
- (func-post :foo "foo1" :bar "bar1" :payload "toto")
- {:request-method :post, :scheme "http", :server-name "example.com",
- :uri "/api/foo1/path", :query-string "bar=bar1" :body "toto" :status 200})
- "post request params are good")))))
+ (with-redefs
+ [clj-http.core/request
+ (fn [{:keys [body] :as arg}]
+ (let [b (if body
+ (let [ l (.getContentLength body)
+ arr (byte-array l)]
+ (with-open [stream (.getContent body)]
+ (.read stream arr))
+ arr))]
+ (assoc
+ (filter-keys
+ arg
+ #{:request-method :scheme :server-name :uri :query-string})
+ :status 200
+ :body b)))]
+ (testing "Mocked requests"
+ (is (function? func-get) "get function generated")
+ (is (=
+ (func-get :foo "foo1" :bar "bar1")
+ {:request-method :get, :scheme "http", :server-name "example.com",
+ :uri "/api/foo1/path", :query-string "bar=bar1" :body nil :status 200})
+ "get request params are good")
+ (is (function? func-post) "post function generated")
+ (is (=
+ (func-post :foo "foo1" :bar "bar1" :payload "toto")
+ {:request-method :post, :scheme "http", :server-name "example.com",
+ :uri "/api/foo1/path", :query-string "bar=bar1" :body "toto" :status 200})
+ "post request params are good")))))

0 comments on commit 57e9a0a

Please sign in to comment.