(TK-27) Add tests for request-with-client

Add asynchronous and synchronous tests for request-with-client.
This commit is contained in:
Preben Ingvaldsen 2014-07-17 14:03:39 -07:00
parent e586ea53c8
commit 0aeaf73dd9
2 changed files with 41 additions and 3 deletions

View file

@ -1,5 +1,6 @@
(ns puppetlabs.http.client.async-plaintext-test
(:import (com.puppetlabs.http.client AsyncHttpClient RequestOptions))
(:import (com.puppetlabs.http.client AsyncHttpClient RequestOptions)
(org.apache.http.impl.nio.client HttpAsyncClients))
(:require [clojure.test :refer :all]
[puppetlabs.trapperkeeper.core :as tk]
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
@ -115,4 +116,22 @@
(is (= "Hello, World!" (slurp (:body @response))))))
(testing "client closes properly"
(common/close client)
(is (thrown? IllegalStateException (common/get client "http://localhost:10000/hello/"))))))))
(is (thrown? IllegalStateException (common/get client "http://localhost:10000/hello/"))))))))
(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))))))
(.close client)))))

View file

@ -3,7 +3,8 @@
HttpClientException ResponseBodyType)
(javax.net.ssl SSLHandshakeException)
(java.io ByteArrayInputStream InputStream)
(java.nio.charset Charset))
(java.nio.charset Charset)
(org.apache.http.impl.nio.client HttpAsyncClients))
(:require [clojure.test :refer :all]
[puppetlabs.trapperkeeper.core :as tk]
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
@ -161,6 +162,24 @@
(is (string? (:body response)))
(is (= "Hello, World!" (:body response))))))))
(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 (sync/request-with-client opts client)]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "Client persists when passed to request-with-client"
(let [response (sync/request-with-client opts client)]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(.close client)))))
(defn header-app
[req]
(let [val (get-in req [:headers "fooheader"])]