Permalink
Browse files

Adding an ability to un-mock functions with use of metadata, implemen…

…ting replaying of requests.
  • Loading branch information...
ifesdjeen committed Aug 16, 2012
1 parent 56f05fb commit 05d39c2d5839cef2523cb877eaa77410c97606ba
Showing with 54 additions and 24 deletions.
  1. +27 −8 src/vcr_clj/core.clj
  2. +10 −12 src/vcr_clj/recorder.clj
  3. +17 −4 test/vcr_clj/core_test.clj
View
@@ -1,23 +1,42 @@
(ns vcr-clj.core
- (:require [clj-yaml.core :as yaml]
- [vcr-clj.recorder :as recorder]))
+ (:require [clj-http.client :as client]
+ [clj-yaml.core :as yaml]
+ [vcr-clj.recorder :as r]))
(defn wrap-fn
"Wrap or replace some function with your own function"
[qualifier wrapper]
(alter-var-root
qualifier
(fn [original-fn]
- (fn [& caller-arguments]
- (wrapper caller-arguments original-fn)))))
+ (with-meta
+ (fn [& caller-arguments]
+ (wrapper caller-arguments original-fn))
+ (assoc (meta original-fn)
+ :original original-fn)))))
+
+(defn unwrap-fn
+ [qualifier]
+ (let [original (-> @qualifier meta :original)]
+ (alter-var-root qualifier (constantly original))))
;; (defn persist-recorded!
;; []
;; (io!
;; (spit "cassete.yml" (yaml/generate-string @recorded-requests))))
-(defn recorder
+(defn- recorder
[caller-arguments original-fn]
- (let [original-result (apply original-fn caller-arguments)]
- ;; (recorder/record-request! (first caller-arguments) original-result)
- original-result))
+ (if-let [res (r/get r/*request-store* (first caller-arguments))]
+ res
+ (let [original-result (apply original-fn caller-arguments)]
+ (r/record! r/*request-store* (first caller-arguments) original-result)
+ original-result)))
+
+(defn mock-clj-http!
+ []
+ (wrap-fn #'client/request recorder))
+
+(defn unmock-clj-http!
+ []
+ (unwrap-fn #'client/request))
View
@@ -1,30 +1,28 @@
-(ns vcr-clj.recorder)
+(ns vcr-clj.recorder
+ (:refer-clojure :exclude [list]))
(declare ^:dynamic *request-store*)
(defprotocol VcrStore
- (record-request! [store key value] "")
- (get-request [store key] "")
+ (record! [store key value] "")
+ (get [store key] "")
(get-all [store] "")
- (request-recorded? [store key] "")
+ (recorded? [store key] "")
(clear-recorded! [store]))
(deftype VcrMemoryStore [store-map]
VcrStore
- (record-request!
+ (record!
[_ options response]
(clojure.core/swap! store-map assoc options response))
- (get-request
- [_ key]
+ (get [_ key]
(clojure.core/get @store-map key))
- (request-recorded?
- [_ options]
+ (recorded? [_ options]
(some #(= options %) (keys @store-map)))
- (clear-recorded!
- [_]
+ (clear-recorded! [_]
(reset! store-map {}))
(get-all
@@ -33,4 +31,4 @@
(defn vcr-memory-store
([] (vcr-memory-store (atom {})))
- ([s] (VcrMemoryStore. s)))
+ ([s] (VcrMemoryStore. s)))
View
@@ -1,5 +1,6 @@
(ns vcr-clj.core-test
- (:require [clj-http.client :as client])
+ (:require [clj-http.client :as client]
+ [vcr-clj.recorder :as r])
(:use clojure.test
vcr-clj.core))
@@ -11,11 +12,16 @@
(is (= "wrap-fn-original-return-value" (wrap-fn-original)))
+
(wrap-fn #'wrap-fn-original
(fn [caller-arguments original-fn]
"wrap-fn-replaced-return-value"))
- (is (= "wrap-fn-replaced-return-value" (wrap-fn-original))))
+ (is (= "wrap-fn-replaced-return-value" (wrap-fn-original)))
+
+ (unwrap-fn #'wrap-fn-original)
+
+ (is (= "wrap-fn-original-return-value" (wrap-fn-original))))
(testing "Replace function (arity 1) with a different implementation"
(defn wrap-fn-original-arity-1
@@ -85,6 +91,13 @@
(let [original-result (apply original-fn caller-arguments)]
(str original-result " " (format "chained-values-for-a-%s-b-%d-c-%d-d-%d" a b c d)))))
- (is (= "original-values-for-a-aaa-b-1-c-2-d-3 chained-values-for-a-aaa-b-1-c-2-d-3" (wrap-fn-original-arity-1-and-optional-keys "aaa" :b 1 :c 2 :d 3))))
- )
+ (is (= "original-values-for-a-aaa-b-1-c-2-d-3 chained-values-for-a-aaa-b-1-c-2-d-3" (wrap-fn-original-arity-1-and-optional-keys "aaa" :b 1 :c 2 :d 3)))))
+(deftest ^{:network-dependent true :unpredictable true} asd
+ (mock-clj-http!)
+ (binding [r/*request-store* (r/vcr-memory-store)]
+ (r/record! r/*request-store* {:url "http://google.com" :method :get} "result")
+ (is (= "result" (client/get "http://google.com"))))
+ (unmock-clj-http!)
+ ;; (r/get r/*request-store* {:url "http://google.com" :method :get})
+ )

0 comments on commit 05d39c2

Please sign in to comment.