Skip to content

Commit

Permalink
Adding basic request recording and persistence.
Browse files Browse the repository at this point in the history
  • Loading branch information
ifesdjeen committed May 24, 2012
1 parent 5c55f40 commit b4544a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/vcr_clj/core.clj
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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))
7 changes: 5 additions & 2 deletions test/vcr_clj/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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.