Skip to content

Commit

Permalink
Adds filters, updates readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanrobinson committed Jan 25, 2024
1 parent 7868477 commit 71b6c43
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 10 deletions.
72 changes: 70 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@
# clj-wiremock-extras

Extras, helpers and convenience methods for clj-wiremock.
Extras, helpers and convenience methods for [clj-wiremock](https://github.com/kelveden/clj-wiremock).

TODO: all of this
[![Clojars Project](https://img.shields.io/clojars/v/uk.co.jordanrobinson/clj-wiremock-extras.svg)](https://clojars.org/uk.co.jordanrobinson/awaitility-clj)

## Examples

### Near misses
```clojure
(:require
[clj-wiremock.core :as wmk]
[clj-wiremock-extras.core :refer [near-misses]])

;; grabs the near misses from the admin api (https://wiremock.org/docs/verifying/#near-misses)
;; some wiremock setup removed for brevity, see tests for more examples

(wmk/with-stubs [{:req [:GET "/foo"]
:res [200 {:body "OK"}]}]

(http/get (str wiremock-url "/bar"))
(near-misses))
;; => [{:match-result
;; {:distance 0.18292682926829268}
;; :request {:absolute-url ... }
;; :stub-mapping {:request {:method "GET", :url-path "/foo"}
;; ...
;; :response {:body "OK", :status 200}
;; ...}}]
```

### Filtering convenience functions

```clojure
(:require
[clj-wiremock.core :as wmk]
[clj-wiremock-extras.core :refer [requests-by-method requests-by-url]])

(let [get-stub {:req [:GET "/foo"]
:res [200 {:body "OK"}]}
post-stub {:req [:POST "/bar"]
:res [200 {:body "OK"}]}
delete-stub {:req [:DELETE "/foo"]
:res [200 {:body "OK"}]}]

(wmk/with-stubs [get-stub post-stub delete-stub]
(http/get (str wiremock-url "/foo"))
(http/post (str wiremock-url "/bar"))
(http/delete (str wiremock-url "/foo"))

(requests-by-method :get))
;; => [{:absoluteUrl ... :method "GET" ... :url "/foo"}]

(count (requests-by-method :get)) ;; => 1

(count (requests-by-url "/foo"))) ;; => 2
```
### Value change on repeated calls
```clojure
(:require
[clj-wiremock.core :as wmk]
[clj-wiremock-extras.core :refer [first-call second-call]])

(let [success-stub {:req [:GET "/foo"]
:res [200 {:body "OK"}]}
failure-stub {:req [:GET "/foo"]
:res [500 {:body "Not OK"}]}]

(wmk/with-stubs [(first-call failure-stub)
(second-call success-stub)]
(http/get (str wiremock-url "/foo")) ;; => {:body "Not OK" :status 500 ...}
(http/get (str wiremock-url "/foo")))) ;; => {:body "OK" :status 200 ...}
```

## License

Expand Down
13 changes: 10 additions & 3 deletions src/clj_wiremock_extras/core.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
(ns clj-wiremock-extras.core
(:require
[potemkin.namespaces :refer [import-vars]]))
[potemkin.namespaces :refer [import-fn]]
[clj-wiremock-extras.times :as times]
[clj-wiremock-extras.near-misses :as near-misses]
[clj-wiremock-extras.filters :as filters]))

(import-vars
[clj-wiremock-extras.times first-call second-call])
(import-fn times/first-call)
(import-fn times/second-call)
(import-fn near-misses/near-misses)
(import-fn filters/requests-by-method)
(import-fn filters/requests-by-url)
(import-fn filters/filter-request-journal)
26 changes: 26 additions & 0 deletions src/clj_wiremock_extras/filters.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns clj-wiremock-extras.filters
(:require [clj-wiremock.core :as wmk]
[clojure.string :as str]))

(defn filter-request-journal
[predicates server]
(let [server (or server (wmk/root-server))]
(->> (wmk/request-journal server)
(filter (apply every-pred predicates))
(mapv :request))))

(defn requests-by-method
[method & server]
(filter-request-journal
[(fn
[{:keys [request]}]
(= (str/upper-case (name method)) (:method request)))]
server))

(defn requests-by-url
[url & server]
(filter-request-journal
[(fn
[{:keys [request]}]
(= url (:url request)))]
server))
20 changes: 15 additions & 5 deletions test/clj_wiremock_extras/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
(ns clj-wiremock-extras.core-test
(:require
[clj-wiremock-extras.filters :as filters]
[clj-wiremock-extras.near-misses :as near-misses]
[clojure.test :refer [deftest is testing]]
[clj-wiremock-extras.core :refer [first-call second-call]]))
[clj-wiremock-extras.core :refer [first-call second-call near-misses
requests-by-method requests-by-url filter-request-journal]]
[clj-wiremock-extras.times :as times]
[clojure.repl :refer [doc]]))

(defmacro out= [& args]
`(= ~@(map (fn [x] `(with-out-str ~x)) args)))

(deftest has-expected-core-functions
(testing "has imported the various utilities from the other namespaces"
(let [empty-stub {}
first-call-stub (first-call empty-stub)
second-call-stub (second-call empty-stub)]
(is (not= empty-stub first-call-stub second-call-stub)))))
(is (out= (doc times/first-call) (doc first-call)))
(is (out= (doc times/second-call) (doc second-call)))
(is (out= (doc near-misses/near-misses) (doc near-misses)))
(is (out= (doc filters/requests-by-method) (doc requests-by-method)))
(is (out= (doc filters/requests-by-url) (doc requests-by-url)))
(is (out= (doc filters/filter-request-journal) (doc filter-request-journal)))))
32 changes: 32 additions & 0 deletions test/clj_wiremock_extras/filters_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns clj-wiremock-extras.filters-test
(:require
[clj-wiremock-extras.filters :as filters]
[clj-wiremock.core :as wmk]
[freeport.core :refer [get-free-port!]]
[clojure.test :refer [deftest is testing use-fixtures]]
[clj-http.client :as http]))

(def wiremock-port (get-free-port!))
(def wiremock-url (str "http://localhost:" wiremock-port))

(use-fixtures :once
(partial wmk/wiremock-fixture [{:port wiremock-port}]))

(deftest filter-by-method
(testing "filters requests made by the method or url given"
(let [get-stub {:req [:GET "/foo"]
:res [200 {:body "OK"}]}
post-stub {:req [:POST "/bar"]
:res [200 {:body "OK"}]}
delete-stub {:req [:DELETE "/foo"]
:res [200 {:body "OK"}]}]
(wmk/with-stubs [get-stub post-stub delete-stub]
(http/get (str wiremock-url "/foo"))
(http/post (str wiremock-url "/bar"))
(http/delete (str wiremock-url "/foo"))

(is (= (count (filters/requests-by-method :get)) 1))
(is (= (count (filters/requests-by-method :post)) 1))
(is (= (count (filters/requests-by-method :delete)) 1))
(is (= (count (filters/requests-by-url "/foo")) 2))
(is (= (count (filters/requests-by-url "/bar")) 1))))))

0 comments on commit 71b6c43

Please sign in to comment.