-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.clj
73 lines (65 loc) · 2.1 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(ns clj-wiremock.core
(:require [clj-wiremock.server :as server]
[clj-wiremock.stub :refer [->stub]]))
(def wiremocks (atom {}))
(defn root-server
"Convenience function to retrieve the first wiremock server registered - useful when only on wiremock-server is in use."
[]
(first (vals @wiremocks)))
(defn wiremock-fixture
[{:keys [port] :as config} f]
(let [s (server/init-wiremock config)]
(swap! wiremocks assoc port s)
(try
(server/start! s)
(f)
(finally
(swap! wiremocks dissoc port)
(server/stop! s)))))
(defn wiremocks-fixture
[configs f]
(let [servers (->> configs
(reduce (fn [acc {:keys [port] :as c}]
(assoc acc port (server/init-wiremock c)))
{}))]
(swap! wiremocks merge servers)
(try
(doseq [[_ s] servers]
(server/start! s))
(f)
(finally
(swap! wiremocks #(apply dissoc (cons % (keys servers))))
(doseq [[_ s] servers]
(server/stop! s))))))
(defn wiremock-fixture-fn
[config f]
(fn []
(wiremock-fixture config f)))
(defmacro with-wiremock
[{:keys [port] :as config} & body]
`(let [s# (server/init-wiremock ~config)]
(swap! wiremocks assoc ~port s#)
(try
(server/start! s#)
~@body
(finally
(swap! wiremocks dissoc ~port)
(server/stop! s#)))))
(defmacro with-stubs
[stubs & body]
`(let [stubs# (->> ~stubs
(map
(fn [stub#]
(-> stub#
(assoc :server (cond
(:port stub#) (get @wiremocks (:port stub#))
(:server stub#) (:server stub#)
:else (root-server)))
(dissoc :port)))))]
(try
(doseq [stub# stubs#]
(server/register-stub! (:server stub#) (dissoc stub# :server)))
~@body
(finally
(doseq [stub# stubs#]
(server/clear! (:server stub#)))))))