Git Product home page Git Product logo

clj-wiremock-extras's Introduction

clj-wiremock-extras

Extras, helpers and convenience methods for clj-wiremock.

Clojars Project

Add to your project

  project.clj

  ...
  :dependencies [[org.clojure/clojure "1.11.1"]
                 [kelveden/clj-wiremock "1.8.0"]
                 [uk.co.jordanrobinson/clj-wiremock-extras "0.0.7"]]
  ...

Deps, Maven, Gradle, etc methods shown on clojars.

Examples

Near misses

(: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

(: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

(: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

Copyright © 2024 Jordan Robinson

Distributed under the MIT license.

clj-wiremock-extras's People

Contributors

jordanrobinson avatar

Stargazers

Yuhri Graziano Bernardes avatar Tommi Reiman avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.