Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.
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
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,107 @@ e.g.
(wmk/with-stubs [{:req [:GET "/ping"] :res [200] :scenario "myscenario" :state {:required "Started" :new "pinged"}}] ...)
```

## Configuration

You can configure WireMock with the same options available to the Java library (as of version 2.27.2).

### Network ports and binding

```clj
:port 8000
:https-port 8001
:dynamic-port? true
:dynamic-https-port? true
:bind-address "192.168.1.111"
```

### Jetty Configuration

```clj
:container-threads 5
:jetty-acceptors 4
:jetty-accept-queue-size 100
:jetty-header-buffer-size 16834
:asynchronous-response-enabled? true
:asynchronous-response-threads 10
```

### HTTPS Configuration

```clj
:keystore-path "/path/to/https-certs-keystore.jks"
:keystore-password "verysecret!"
:keystore-type "BKS"
:key-manager-password "donttell"
:need-client-auth? true
:trust-store-path "/path/to/trust-store.jks"
:trust-store-password "trustme"
```

### Proxy Settings

```clj
:enable-browser-proxying? true
:preserve-host-header? false
:proxy-host-header "my.otherdomain.com"
:proxy-via ["my.corporate.proxy", 8080]
:ca-keystore-path "/path/to/ca-key-store.jks"
:ca-keystore-password "trustme"
:ca-keystore-type "JKS"
```

### File Locations

```clj
:using-files-under-directory "/path/to/files-and-mappings-root"
:using-files-under-classpath "root/path/under/classpath"
```

### Request Journal

```clj
:disable-request-journal? true
:max-requests-journal-entries 100
```

### Notification

```clj
:log-to-console? true ;syntactic sugar for .notifier(new ConsoleNotifier(true))
:notifier (reify Notifier
(info [_ _])
(error [_ _])
(error [_ _ _])
```

### Gzip

```clj
:gzip-disabled? true
```

### Extensions
Supports passing instances of Extension only at this point.

```clj
:extensions [(ResponseTemplateTransformer. true)]
```

### Transfer Encoding

Accepts `:always`, `:body-file`, or `:never`.

```clj
:use-chunked-transfer-encoding :body-file
```

### Cross-origin response headers (CORS)

```clj
:stub-cors-enabled? true
```

You can find more information about each configuration on the [WireMock Configuration](http://wiremock.org/docs/configuration) page.

## License
[Eclipse Public License 1.0](http://opensource.org/licenses/eclipse-1.0.php), the same as Clojure.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject kelveden/clj-wiremock "1.8.0"
(defproject kelveden/clj-wiremock "1.9.0-SNAPSHOT"
:description "Clojure bindings for WireMock"
:url "https://github.com/kelveden/clj-wiremock"
:license {:name "Eclipse Public License - v 1.0"
Expand Down
93 changes: 86 additions & 7 deletions src/clj_wiremock/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[clj-http.client :as http]
[clj-wiremock.stub :refer [->stub]]
[clojure.tools.logging :as log])
(:import com.github.tomakehurst.wiremock.core.WireMockConfiguration))
(:import (com.github.tomakehurst.wiremock.core WireMockConfiguration Options$ChunkedEncodingPolicy)
(com.github.tomakehurst.wiremock.common ConsoleNotifier)
(com.github.tomakehurst.wiremock.extension Extension)))

(defprotocol Wiremocked
(start! [_] "Start the wiremock server.")
Expand Down Expand Up @@ -49,10 +51,87 @@
(json/parse-string true)
:requests)))

(defn- chunked-encoding-policy
"Gets the ChunkedEncodingPolicy according to the given keyword."
[policy]
(case policy
:always (Options$ChunkedEncodingPolicy/ALWAYS)
:body-file (Options$ChunkedEncodingPolicy/BODY_FILE)
:never (Options$ChunkedEncodingPolicy/NEVER)
(throw (IllegalArgumentException. (str policy " is not a recognizable chunked encoding policy. (try :always, :body-file, or :never instead)")))))

(defn- set-wiremock-option
"Sets the WireMock configuration according to the given key/value pair."
[^WireMockConfiguration config key value]
(case key
;; Network ports and binding
:port (.port config value)
:https-port (.httpsPort config (Integer/valueOf value))
:dynamic-port? (if value (.dynamicPort config) config)
:dynamic-https-port? (if value (.dynamicHttpsPort config) config)
:bind-address (.bindAddress config value)

;; Jetty Configuration
:container-threads (.containerThreads config (Integer/valueOf value))
:jetty-acceptors (.jettyAcceptors config (Integer/valueOf value))
:jetty-accept-queue-size (.jettyAcceptQueueSize config (Integer/valueOf value))
:jetty-header-buffer-size (.jettyHeaderBufferSize config (Integer/valueOf value))
:asynchronous-response-enabled? (.asynchronousResponseEnabled config value)
:asynchronous-response-threads (.asynchronousResponseThreads config value)

;; HTTPS Configuration
:keystore-path (.keystorePath config value)
:keystore-password (.keystorePassword config value)
:keystore-type (.keystoreType config value)
:key-manager-password (.keyManagerPassword config value)
:need-client-auth? (.needClientAuth config value)
:trust-store-path (.trustStorePath config value)
:trust-store-password (.trustStorePassword config value)

;; Proxy Settings
:enable-browser-proxying? (.enableBrowserProxying config value)
:preserve-host-header? (.preserveHostHeader config value)
:proxy-host-header (.proxyHostHeader config value)
:proxy-via (.proxyVia config (first value) (second value))
:ca-keystore-path (.caKeystorePath config value)
:ca-keystore-password (.caKeystorePassword config value)
:ca-keystore-type (.caKeystoreType config value)

;; File Locations
:using-files-under-directory (.usingFilesUnderDirectory config value)
:using-files-under-classpath (.usingFilesUnderClasspath config value)

;; Request Journal
:disable-request-journal? (if value (.disableRequestJournal config) config)
:max-request-journal-entries (.maxRequestJournalEntries config value)

;; Notification (Logging)
:log-to-console? (if value (.notifier config (ConsoleNotifier. true)) config)
:notifier (.notifier config value)

;; Gzip
:gzip-disabled? (.gzipDisabled config value)

;; Extensions - supports passing instances of Extension only at this point
:extensions (.extensions config (into-array Extension value))

;; Transfer Encoding
:use-chunked-transfer-encoding (.useChunkedTransferEncoding config (chunked-encoding-policy value))

;; Cross-origin response headers (CORS)
:stub-cors-enabled? (.stubCorsEnabled config value)

;; Unrecognizable Option
(throw (IllegalArgumentException. (str key " is not a recognizable clj-wiremock option.")))))

(defn- wiremock-config
"Creates a WireMock configuration with the given options."
[options]
(reduce-kv set-wiremock-option (WireMockConfiguration.) options))

(defn init-wiremock
"Intialises a new WireMock server ready for starting on the specified port."
[{:keys [port]}]
(let [config (doto (new WireMockConfiguration)
(.port (int port)))
wmk-java (new com.github.tomakehurst.wiremock.WireMockServer config)]
(->WireMockServer wmk-java)))
"Intialises a new WireMock server ready for starting with the given options."
[options]
(let [config (wiremock-config options)
wmk-java (com.github.tomakehurst.wiremock.WireMockServer. config)]
(->WireMockServer wmk-java)))
Loading