standalone-test-server.core
Provides a ring handler that can record received requests.
recording-endpoint
deprecated in 0.7.2
(recording-endpoint & [{:keys [handler], :or {handler default-handler}}])
DEPRECATED: Use with-requests-chan instead.
Creates a ring handler that can record the requests that flows through it.
Options:
handler
The handler forrecording-endpoint
to wrap. Defaults to a handler that returns status 200 with an empty body.
Returns:
[requests-atom recording-handler]
The requests-atom
contains a vector of requests received by the recording-handler.
The requests are standard ring requests except that the :body
will be a string instead of InputStream.
Example invocations:
;; Returns a 404 response to the http client that hits this handler
(recording-endpoint {:handler (constantly {:status 404 :headers {}})})
requests-count?
deprecated in 0.7.2
(requests-count? requests exact-count)
(requests-count? requests exact-count options)
DEPRECATED: Use txfm-requests instead
Convenience for calling
(requests-meet? requests-atom #(= exact-count (count %)) options)
requests-meet?
deprecated in 0.7.2
(requests-meet? requests pred)
(requests-meet? requests pred {:keys [timeout], :or {timeout default-timeout}})
DEPRECATED: Use txfm-requests instead
Blocks until the given requests
atom satisfies a predicate or the timeout has been reached.
Returns true or false respectively.
The predicate function takes the state of the requests
as the only argument. It is tested after every change to the requests.
There is one optional argument:
:timeout
the period of time (in milliseconds) to wait until returning false; defaults to 500.
Also note that the predicate function may be called on multiple threads simultaneously.
;; Reports whether requests-atom has at least 2 items before 3 seconds have elapsed.
(requests-meet? requests-atom #(<= 2 (count %)) {:timeout 3000})
requests-min-count?
deprecated in 0.7.2
(requests-min-count? requests min-count)
(requests-min-count? requests min-count options)
DEPRECATED: Use txfm-requests instead
Convenience for calling
(requests-meet? requests-atom #(<= min-count (count %)) options)
requests-quiescent
deprecated in 0.7.2
(requests-quiescent requests)
(requests-quiescent requests {:keys [for-ms], :or {for-ms default-timeout}})
DEPRECATED: Use txfm-requests instead
Blocks until the given requests requests has stopped growing for for-ms
Returns nil.
There is one optional argument:
:for-ms
How long to wait after receiving the last request before declaring quiescence; defaults to 500.
seq-handler
(seq-handler & handlers)
A helper function which iterates through a sequence of handlers using a new one for each call to the handler.
Uses the default handler when out of handlers to use.
Example
(with-requests-chan (seq-handler
;;first handler
(fn [req] {:status 200 :body "ok"})
;;second handler
(fn [req] {:status 400 :body "bad"})))
standalone-server
(standalone-server handler & [opts])
Wrapper to start a standalone server through ring-jetty.
Takes a ring handler and if desired, options to pass through to run-jetty
.
The options default to :port 4334
and :join? false
.
Example:
(standalone-server handler {:port 4335})
txfm-request
(txfm-request requests)
(txfm-request requests xf)
(txfm-request requests xf opts)
Convenience for extracting the first request in a channel of requests
which satisifies the transform tx
. With no transform, or a transform that doesn’t filter results, just returns the first request.
txfm-requests
(txfm-requests requests xf)
(txfm-requests requests xf {:keys [timeout]})
Accepts a channel of requests
and a transducing function xf
which can filter
, take
, or drop
requests. Especially useful to centralize the transformation of requests bodies, using (map #(update :body % ...))
.
Blocks until the xf
is reduced or the timeout has been reached. As such, to avoid unnecessary pauses, take
as many requests as you expect to receive.
There is one optional argument:
:timeout
the period of time (in milliseconds) to wait until returning what has been fetched so far; defaults to 500.
;; Returns upto 2 requests, as many as are produced within 3 seconds.
(txfm-requests requests-chan (take 2) {:timeout 3000})
with-requests-chan
(with-requests-chan)
(with-requests-chan handler)
(with-requests-chan handler buf-or-n)
Creates a ring handler that can record the requests that flow through it.
Options:
handler
The handler for `with-requests-chan`` to wrap. Defaults to a handler that returns status 200 with an empty body.buf-or-n
The buffer configuration for the channel, defaults to 100.
Returns:
[requests-chan wrapped-handler]
The requests-chan
holds the requests received by the handler. txfm-requests can extract the requests from it.
The requests are standard ring requests except that the :body
will be a string instead of InputStream.
Example invocations:
;; Returns a 404 response to the http client that hits this handler
(with-requests-chan (constantly {:status 404 :headers {}}))
with-standalone-server
macro
(with-standalone-server bindings & body)
A convenience macro to ensure a standalone-server is stopped.
Example with standalone-server and with-requests-chan
(let [[requests handler] (with-requests-chan)]
(with-standalone-server [server (standalone-server handler)]
(http/get "http://localhost:4334/endpoint")
(is (txfm-request requests))))