2014-07-03 15:58:46 +00:00
|
|
|
(ns puppetlabs.http.client.async-plaintext-test
|
2014-11-10 19:24:36 +00:00
|
|
|
(:import (com.puppetlabs.http.client Async RequestOptions ClientOptions SimpleRequestOptions)
|
2014-08-08 23:32:48 +00:00
|
|
|
(org.apache.http.impl.nio.client HttpAsyncClients)
|
2014-11-07 21:29:17 +00:00
|
|
|
(java.net URI)
|
|
|
|
(com.puppetlabs.http.client.impl PersistentAsyncHttpClient))
|
2014-07-03 15:58:46 +00:00
|
|
|
(:require [clojure.test :refer :all]
|
2014-08-06 17:49:48 +00:00
|
|
|
[puppetlabs.http.client.test-common :refer :all]
|
2014-07-03 15:58:46 +00:00
|
|
|
[puppetlabs.trapperkeeper.core :as tk]
|
|
|
|
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
|
|
|
|
[puppetlabs.trapperkeeper.testutils.logging :as testlogging]
|
|
|
|
[puppetlabs.trapperkeeper.services.webserver.jetty9-service :as jetty9]
|
2014-07-15 23:46:20 +00:00
|
|
|
[puppetlabs.http.client.common :as common]
|
2014-07-03 15:58:46 +00:00
|
|
|
[puppetlabs.http.client.async :as async]
|
|
|
|
[schema.test :as schema-test]))
|
|
|
|
|
|
|
|
(use-fixtures :once schema-test/validate-schemas)
|
|
|
|
|
|
|
|
(defn app
|
|
|
|
[req]
|
|
|
|
{:status 200
|
|
|
|
:body "Hello, World!"})
|
|
|
|
|
|
|
|
(tk/defservice test-web-service
|
|
|
|
[[:WebserverService add-ring-handler]]
|
|
|
|
(init [this context]
|
|
|
|
(add-ring-handler app "/hello")
|
|
|
|
context))
|
|
|
|
|
2014-07-15 22:07:43 +00:00
|
|
|
(deftest persistent-async-client-test
|
|
|
|
(testlogging/with-test-logging
|
|
|
|
(testutils/with-app-with-config app
|
|
|
|
[jetty9/jetty9-service test-web-service]
|
|
|
|
{:webserver {:port 10000}}
|
2014-11-07 21:29:17 +00:00
|
|
|
(testing "java async client"
|
|
|
|
(let [request-options (RequestOptions. (URI. "http://localhost:10000/hello/"))
|
|
|
|
client-options (ClientOptions.)
|
2014-11-10 19:24:36 +00:00
|
|
|
client (Async/createClient client-options)]
|
2014-11-07 21:29:17 +00:00
|
|
|
(testing "HEAD request with persistent async client"
|
|
|
|
(let [response (.head client request-options)]
|
|
|
|
(is (= 200 (.getStatus (.deref response))))
|
|
|
|
(is (= nil (.getBody (.deref response))))))
|
|
|
|
(testing "GET request with persistent async client"
|
|
|
|
(let [response (.get client request-options)]
|
|
|
|
(is (= 200 (.getStatus (.deref response))))
|
|
|
|
(is (= "Hello, World!" (slurp (.getBody (.deref response)))))))
|
|
|
|
(testing "POST request with persistent async client"
|
|
|
|
(let [response (.post client request-options)]
|
|
|
|
(is (= 200 (.getStatus (.deref response))))
|
|
|
|
(is (= "Hello, World!" (slurp (.getBody (.deref response)))))))
|
|
|
|
(testing "PUT request with persistent async client"
|
|
|
|
(let [response (.put client request-options)]
|
|
|
|
(is (= 200 (.getStatus (.deref response))))
|
|
|
|
(is (= "Hello, World!" (slurp (.getBody (.deref response)))))))
|
|
|
|
(testing "DELETE request with persistent async client"
|
|
|
|
(let [response (.delete client request-options)]
|
|
|
|
(is (= 200 (.getStatus (.deref response))))
|
|
|
|
(is (= "Hello, World!" (slurp (.getBody (.deref response)))))))
|
|
|
|
(testing "TRACE request with persistent async client"
|
|
|
|
(let [response (.trace client request-options)]
|
|
|
|
(is (= 200 (.getStatus (.deref response))))
|
|
|
|
(is (= "Hello, World!" (slurp (.getBody (.deref response)))))))
|
|
|
|
(testing "OPTIONS request with persistent async client"
|
|
|
|
(let [response (.options client request-options)]
|
|
|
|
(is (= 200 (.getStatus (.deref response))))
|
|
|
|
(is (= "Hello, World!" (slurp (.getBody (.deref response)))))))
|
|
|
|
(testing "PATCH request with persistent async client"
|
|
|
|
(let [response (.patch client request-options)]
|
|
|
|
(is (= 200 (.getStatus (.deref response))))
|
|
|
|
(is (= "Hello, World!" (slurp (.getBody (.deref response)))))))
|
2014-11-10 21:20:01 +00:00
|
|
|
(.close client)))
|
2014-11-07 21:29:17 +00:00
|
|
|
(testing "clojure async client"
|
|
|
|
(let [client (async/create-client {})]
|
|
|
|
(testing "HEAD request with persistent async client"
|
|
|
|
(let [response (common/head client "http://localhost:10000/hello/")]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= nil (:body @response)))))
|
|
|
|
(testing "GET request with persistent async client"
|
|
|
|
(let [response (common/get client "http://localhost:10000/hello/")]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
|
|
|
(testing "POST request with persistent async client"
|
|
|
|
(let [response (common/post client "http://localhost:10000/hello/")]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
|
|
|
(testing "PUT request with persistent async client"
|
|
|
|
(let [response (common/put client "http://localhost:10000/hello/")]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
|
|
|
(testing "DELETE request with persistent async client"
|
|
|
|
(let [response (common/delete client "http://localhost:10000/hello/")]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
|
|
|
(testing "TRACE request with persistent async client"
|
|
|
|
(let [response (common/trace client "http://localhost:10000/hello/")]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
|
|
|
(testing "OPTIONS request with persistent async client"
|
|
|
|
(let [response (common/options client "http://localhost:10000/hello/")]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
|
|
|
(testing "PATCH request with persistent async client"
|
|
|
|
(let [response (common/patch client "http://localhost:10000/hello/")]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
|
|
|
(testing "client closes properly"
|
|
|
|
(common/close client)
|
|
|
|
(is (thrown? IllegalStateException (common/get client "http://localhost:10000/hello/")))))))))
|
2014-07-17 21:03:39 +00:00
|
|
|
|
|
|
|
(deftest request-with-client-test
|
|
|
|
(testlogging/with-test-logging
|
|
|
|
(testutils/with-app-with-config app
|
|
|
|
[jetty9/jetty9-service test-web-service]
|
|
|
|
{:webserver {:port 10000}}
|
|
|
|
(let [client (HttpAsyncClients/createDefault)
|
|
|
|
opts {:method :get :url "http://localhost:10000/hello/"}]
|
|
|
|
(.start client)
|
|
|
|
(testing "GET request works with request-with-client"
|
|
|
|
(let [response (async/request-with-client opts nil client)]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
|
|
|
(testing "Client persists when passed to request-with-client"
|
|
|
|
(let [response (async/request-with-client opts nil client)]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
2014-08-06 17:49:48 +00:00
|
|
|
(.close client)))))
|
|
|
|
|
2014-08-25 18:15:22 +00:00
|
|
|
(deftest redirect-test-async
|
|
|
|
(testlogging/with-test-logging
|
|
|
|
(testutils/with-app-with-config app
|
|
|
|
[jetty9/jetty9-service redirect-web-service]
|
|
|
|
{:webserver {:port 8080}}
|
|
|
|
(testing (str "redirects on POST followed by persistent clojure client "
|
|
|
|
"when option is set")
|
|
|
|
(let [client (async/create-client {:force-redirects true})
|
|
|
|
response (common/post client "http://localhost:8080/hello" {:as :text})]
|
|
|
|
(is (= 200 (:status @response)))
|
|
|
|
(is (= "Hello, World!" (:body @response)))
|
2014-08-26 23:36:47 +00:00
|
|
|
(common/close client)))
|
|
|
|
(testing (str "persistent clojure client does not follow redirects when "
|
|
|
|
":follow-redirects is set to false")
|
|
|
|
(let [client (async/create-client {:follow-redirects false})
|
|
|
|
response (common/get client "http://localhost:8080/hello" {:as :text})]
|
|
|
|
(is (= 302 (:status @response)))
|
|
|
|
(common/close client)))
|
|
|
|
(testing ":follow-redirects overrides :force-redirects with persistent clj client"
|
|
|
|
(let [client (async/create-client {:follow-redirects false
|
|
|
|
:force-redirects true})
|
|
|
|
response (common/get client "http://localhost:8080/hello" {:as :text})]
|
|
|
|
(is (= 302 (:status @response)))
|
2014-08-27 17:38:07 +00:00
|
|
|
(common/close client))))))
|