(TK-27) Add persistent sync client

Add a persistent sync client to the persistent-sync namespace.
With this namespace, a user can now create a persistent
sync client for reuse. Also, add persistent sync client test.
This commit is contained in:
Preben Ingvaldsen 2014-07-15 16:01:36 -07:00
parent cebaeb9eb6
commit 0b2755eef5
3 changed files with 101 additions and 5 deletions

View file

@ -0,0 +1,50 @@
(ns puppetlabs.http.client.persistent-sync
(:import (org.apache.http.impl.nio.client HttpAsyncClients))
(:require [schema.core :as schema]
[puppetlabs.http.client.schemas :as schemas]
[puppetlabs.http.client.sync :refer [request]]
[puppetlabs.http.client.async :refer [configure-ssl]])
(:refer-clojure :exclude (get)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Sync Client protocol
(defprotocol SyncClient
(get [this url] [this url opts])
(head [this url] [this url opts])
(post [this url] [this url opts])
(put [this url] [this url opts])
(delete [this url] [this url opts])
(trace [this url] [this url opts])
(options [this url] [this url opts])
(patch [this url] [this url opts])
(close [this]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Public
(schema/defn create-client :- SyncClient
[opts :- schemas/ClientOptions]
(let [opts (configure-ssl opts)
client (if (:ssl-context opts)
(.. (HttpAsyncClients/custom) (setSSLContext (:ssl-context opts)) build)
(HttpAsyncClients/createDefault))]
(.start client)
(reify SyncClient
(get [this url] (get this url {}))
(get [_ url opts] (request (assoc opts :method :get :url url) client))
(head [this url] (head this url {}))
(head [_ url opts] (request (assoc opts :method :head :url url) client))
(post [this url] (post this url {}))
(post [_ url opts] (request (assoc opts :method :post :url url) client))
(put [this url] (put this url {}))
(put [_ url opts] (request (assoc opts :method :put :url url) client))
(delete [this url] (delete this url {}))
(delete [_ url opts] (request (assoc opts :method :delete :url url) client))
(trace [this url] (trace this url {}))
(trace [_ url opts] (request (assoc opts :method :trace :url url) client))
(options [this url] (options this url {}))
(options [_ url opts] (request (assoc opts :method :options :url url) client))
(patch [this url] (patch this url {}))
(patch [_ url opts] (request (assoc opts :method :patch :url url) client))
(close [_] (.close client)))))

View file

@ -6,11 +6,16 @@
(:refer-clojure :exclude (get)))
(defn request
[req]
(let [{:keys [error] :as resp} @(async/request req nil)]
(if error
(throw error)
resp)))
([req]
(let [{:keys [error] :as resp} @(async/request req nil)]
(if error
(throw error)
resp)))
([req client]
(let [{:keys [error] :as resp} @(async/request req nil client)]
(if error
(throw error)
resp))))
(defn get
"Issue a synchronous HTTP GET request. This will raise an exception if an

View file

@ -10,6 +10,7 @@
[puppetlabs.trapperkeeper.testutils.logging :as testlogging]
[puppetlabs.trapperkeeper.services.webserver.jetty9-service :as jetty9]
[puppetlabs.http.client.sync :as sync]
[puppetlabs.http.client.persistent-sync :as p-sync]
[schema.test :as schema-test]
[clojure.java.io :as io]))
@ -79,6 +80,46 @@
(deftest sync-client-patch-test
(basic-test "PATCH" #(SyncHttpClient/patch %) sync/patch))
(deftest sync-client-persistent-test
(testlogging/with-test-logging
(testutils/with-app-with-config app
[jetty9/jetty9-service test-web-service]
{:webserver {:port 10000}}
(let [client (p-sync/create-client {})]
(testing "HEAD request with persistent sync client"
(let [response (p-sync/head client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= nil (:body response)))))
(testing "GET request with persistent sync client"
(let [response (p-sync/get client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "POST request with persistent sync client"
(let [response (p-sync/post client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "PUT request with persistent sync client"
(let [response (p-sync/put client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "DELETE request with persistent sync client"
(let [response (p-sync/delete client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "TRACE request with persistent sync client"
(let [response (p-sync/trace client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "OPTIONS request with persistent sync client"
(let [response (p-sync/options client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "PATCH request with persistent sync client"
(let [response (p-sync/patch client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(p-sync/close client)))))
(deftest sync-client-as-test
(testlogging/with-test-logging
(testutils/with-app-with-config app