2014-08-06 17:49:48 +00:00
|
|
|
(ns puppetlabs.http.client.test-common
|
|
|
|
(:require [ring.middleware.params :as ring-params]
|
2015-03-17 05:46:07 +00:00
|
|
|
[puppetlabs.trapperkeeper.core :as tk]
|
|
|
|
[puppetlabs.trapperkeeper.testutils.logging :as testlogging])
|
|
|
|
(:import (java.net ConnectException SocketTimeoutException)))
|
2014-08-06 17:49:48 +00:00
|
|
|
|
|
|
|
(defn query-params-test
|
|
|
|
[req]
|
|
|
|
{:status 200
|
2014-08-07 17:36:03 +00:00
|
|
|
:body (str (:query-params req))})
|
2014-08-06 17:49:48 +00:00
|
|
|
|
|
|
|
(def app-wrapped
|
|
|
|
(ring-params/wrap-params query-params-test))
|
|
|
|
|
|
|
|
(tk/defservice test-params-web-service
|
|
|
|
[[:WebserverService add-ring-handler]]
|
|
|
|
(init [this context]
|
|
|
|
(add-ring-handler app-wrapped "/params")
|
|
|
|
context))
|
|
|
|
|
2014-08-07 17:36:03 +00:00
|
|
|
(def queryparams {"foo" "bar"
|
|
|
|
"baz" "lux"})
|
2014-08-06 21:23:56 +00:00
|
|
|
|
2014-08-06 21:33:26 +00:00
|
|
|
(def query-options {:method :get
|
|
|
|
:url "http://localhost:8080/params/"
|
2014-08-06 21:23:56 +00:00
|
|
|
:query-params queryparams
|
2014-08-25 18:15:22 +00:00
|
|
|
:as :text})
|
|
|
|
|
|
|
|
(defn redirect-test-handler
|
|
|
|
[req]
|
|
|
|
(condp = (:uri req)
|
|
|
|
"/hello/world" {:status 200 :body "Hello, World!"}
|
2014-10-18 02:12:43 +00:00
|
|
|
"/hello" {:status 302
|
2014-08-25 18:15:22 +00:00
|
|
|
:headers {"Location" "/hello/world"}
|
|
|
|
:body ""}
|
|
|
|
{:status 404 :body "D'oh"}))
|
|
|
|
|
|
|
|
(tk/defservice redirect-web-service
|
|
|
|
[[:WebserverService add-ring-handler]]
|
|
|
|
(init [this context]
|
|
|
|
(add-ring-handler redirect-test-handler "/hello")
|
2015-03-16 17:50:09 +00:00
|
|
|
context))
|
|
|
|
|
2015-03-17 05:46:07 +00:00
|
|
|
(defmacro connect-exception-thrown?
|
|
|
|
[& body]
|
|
|
|
`(try
|
|
|
|
(testlogging/with-test-logging ~@body)
|
|
|
|
(catch SocketTimeoutException _# true)
|
|
|
|
(catch ConnectException _# true)))
|
|
|
|
|
2015-03-16 17:50:09 +00:00
|
|
|
(defn elapsed-within-range?
|
|
|
|
[start-time-milliseconds duration-milliseconds]
|
|
|
|
(<= (System/currentTimeMillis) (+ start-time-milliseconds
|
|
|
|
duration-milliseconds)))
|