/
in_memory_http_client.clj
43 lines (38 loc) · 1.64 KB
/
in_memory_http_client.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
(ns common-clj.components.http-client.in-memory-http-client
(:require [com.stuartsierra.component :as component]
[common-clj.components.http-client.protocol :as hc-pro]
[common-clj.schemata.http-client :as s-hc]
[schema.core :as s]))
(s/defrecord InMemoryHttpClient [endpoints :- s-hc/Endpoints]
component/Lifecycle
(start [component]
(assoc component :mocked-responses (atom {})))
(stop [component]
component)
hc-pro/HttpClient
(request [component endpoint]
(hc-pro/request component endpoint {}))
(request [{:keys [mocked-responses]} endpoint options]
(let [path-params-keys (or (-> endpoints endpoint :path-params-schema keys)
'())
path-params-values (or (vals (filter (fn [[k]] (some #(= k %) path-params-keys)) options))
'())
response-path (conj path-params-values endpoint)
response (-> mocked-responses
deref
(get-in response-path))]
(if response
(:body response)
(throw (ex-info "No response mocked"
{:type :http-client.error/no-response}))))))
(s/defn new-http-client [endpoints :- s-hc/Endpoints]
(map->InMemoryHttpClient {:endpoints endpoints}))
(defn mock-response!
([http-client endpoint options]
(mock-response! http-client endpoint {} options))
([{:keys [mocked-responses]} endpoint path-params {:keys [body]}]
(let [path (or (-> path-params vals)
'())
response-path (conj path endpoint)]
(swap! mocked-responses
#(assoc-in % response-path {:body body})))))