Skip to content
Browse files

Adding basic request recording and persistence.

  • Loading branch information...
1 parent 5c55f40 commit b4544a54e568694ddeef2ce8085e8d42b554d073 @ifesdjeen committed May 24, 2012
Showing with 29 additions and 3 deletions.
  1. +24 −1 src/vcr_clj/core.clj
  2. +5 −2 test/vcr_clj/core_test.clj
View
25 src/vcr_clj/core.clj
@@ -1,4 +1,5 @@
-(ns vcr-clj.core)
+(ns vcr-clj.core
+ (:require [clj-yaml.core :as yaml]))
(defn wrap-fn
"Wrap or replace some function with your own function"
@@ -8,3 +9,25 @@
(fn [original-fn]
(fn [& caller-arguments]
(wrapper caller-arguments original-fn)))))
+
+(defonce recorded-requests (atom {}))
+
+(defn record-request
+ ""
+ [options response]
+ (swap! recorded-requests assoc options response))
+
+(defn request-recorded?
+ [options]
+ (some #(= options %) (keys @recorded-requests)))
+
+(defn persist-recorded!
+ []
+ (io!
+ (spit "/Users/alexp/p/vcr-clj/cassete.yml" (yaml/generate-string @recorded-requests))))
+
+(defn recorder
+ [caller-arguments original-fn]
+ (let [original-result (apply original-fn caller-arguments)]
+ (record-request (first caller-arguments) original-result)
+ original-result))
View
7 test/vcr_clj/core_test.clj
@@ -86,8 +86,11 @@
(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))))
+ )
-
-
+(deftest test-recorder
+ (wrap-fn #'client/request #'recorder)
+ (client/get "http://google.com" {:accept :json :throw-exceptions false})
+ (is (request-recorded? {:url "http://google.com" :method :get :accept :json :throw-exceptions false}))
)

0 comments on commit b4544a5

Please sign in to comment.
Something went wrong with that request. Please try again.