Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bypass fn #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/vcr_clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
true)

(defn ^:private build-wrapped-fn
[record-fn {:keys [var arg-transformer arg-key-fn recordable? return-transformer]
[record-fn {:keys [var arg-transformer arg-key-fn recordable? return-transformer middleware]
:or {arg-transformer vector
arg-key-fn vector
recordable? (constantly true)
middleware (fn [f]
(fn [& args] (apply f args)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be simplified to identity

return-transformer identity}}]
(let [orig-fn (deref var)
the-var-name (var-name var)
Expand All @@ -33,7 +35,7 @@
(if-not (and *recording?* (apply recordable? args*))
(apply orig-fn args*)
(let [res (binding [*recording?* false]
(return-transformer (apply orig-fn args*)))
(return-transformer (apply (middleware orig-fn) args*)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to preview a response body in a web browser, I'm thinking you'll need the middleware to be applied after the return-transformer so that the body has already been made re-readable, does that sound right?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having the middleware see the transformed return value should also make it more consistent with the way it's called during playback.

call {:var-name the-var-name
:arg-key (apply arg-key-fn args*)
:return res}]
Expand Down Expand Up @@ -94,19 +96,22 @@
(let [the-playbacker (playbacker cassette :key)
redeffings
(into {}
(for [{:keys [var arg-transformer arg-key-fn recordable?]
(for [{:keys [var arg-transformer arg-key-fn recordable? middleware]
:or {arg-transformer vector
arg-key-fn vector
middleware (fn [f]
(fn [& args] (apply f args)))
recordable? (constantly true)}}
specs
:let [orig (deref var)
the-var-name (var-name var)
wrapped (fn [& args]
(let [args* (apply arg-transformer args)]
(if (apply recordable? args*)
(let [k (apply arg-key-fn args*)]
(:return (the-playbacker the-var-name k)))
(apply orig args*))))]]
(let [k (apply arg-key-fn args*)
res (:return (the-playbacker the-var-name k))]
(apply (middleware (fn [& args] res)) args*))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the middleware could be applied to the real playback function rather than an artificial function via something like

(apply (middleware (fn [& args] (:return (the-playbacker the-var-name (apply arg-key-fn args))))))

(apply (middleware orig) args*))))]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I would expect the middleware to be called in the non-recordable case; what do you think?

[var (add-meta-from wrapped orig)]))]
(with-redefs-fn redeffings func)))

Expand Down
29 changes: 29 additions & 0 deletions test/vcr_clj/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,32 @@
(.join))
(is (= 2 (count (calls increment))))
(is (= 100 @p))))))


(deftest middleware-test
(defn args-test [args]
(is (= '(2 3) args)))

(defn res-test [res]
(is (= 5 res)))

(defn sample-middleware [vcr-clj-fn]
(fn [& args]
(args-test args)
(let [res (apply vcr-clj-fn args)]
(res-test res)
res)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you either move these defns to the top level or transform them into a let or letfn? I don't think nested defs are idiomatic in clojure.


(with-spy [plus sample-middleware args-test res-test]
(with-cassette :middlewared [{:var #'plus :middleware sample-middleware}]
(is (= 5 (plus 2 3)))
(is (= 1 (count (calls plus))))
(is (= 1 (count (calls sample-middleware))))
(is (= 1 (count (calls args-test))))
(is (= 1 (count (calls res-test)))))
(with-cassette :middlewared [{:var #'plus :middleware sample-middleware}]
(is (= 5 (plus 2 3)))
(is (= 1 (count (calls plus))))
(is (= 2 (count (calls sample-middleware))))
(is (= 2 (count (calls args-test))))
(is (= 2 (count (calls res-test)))))))