Add more options to the HTTP wait strategy (#33)

Issue #25

Co-authored-by: Tim Zöller <tim.zoeller@ilume.de>
This commit is contained in:
Tim Zöller 2020-09-26 21:32:14 +02:00 committed by GitHub
parent 651b4faf7f
commit 9f36f0db78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,5 @@
(ns clj-test-containers.core
(:require
[clj-test-containers.spec.container :as csc]
[clj-test-containers.spec.core :as cs]
[clojure.spec.alpha :as s])
(:import
@ -23,12 +22,57 @@
BindMode/READ_WRITE
BindMode/READ_ONLY))
(defmulti wait :strategy)
(defmulti wait
"Sets a wait strategy to the container.
Supports :http, :health and :log as strategies.
## HTTP Strategy
The :http strategy will only accept the container as initialized if it can be accessed
via HTTP. It accepts a path, a port, a vector of status codes, a boolean that specifies
if TLS is enabled, a read timeout in seconds and a map with basic credentials, containing
username and password. Only the path is required, all others are optional.
Example:
```clojure
(wait {:strategy :http
:port 80
:path \"/\"
:status-codes [200 201]
:tls true
:read-timeout 5
:basic-credentials {:username \"user\"
:password \"password\"}}
container))
```
## Health Strategy
TBD
## Log Strategy
TBD"
:strategy)
(defmethod wait :http
[{:keys [path]} container]
(.waitingFor container (Wait/forHttp path))
{:wait-for-http path})
[{:keys [path port status-codes tls read-timeout basic-credentials] :as options} container]
(let [for-http (Wait/forHttp path)]
(when port
(.forPort for-http port))
(doseq [status-code status-codes]
(.forStatusCode for-http status-code))
(when tls
(.usingTls for-http))
(when read-timeout
(.withReadTimeout (java.time.Duration/ofSeconds read-timeout)))
(when basic-credentials
(let [{username :username password :password} basic-credentials]
(.withBasicCredentials username password)))
(.waitingFor container for-http)
{:wait-for-http (dissoc options :strategy)}))
(defmethod wait :health
[_ container]