(TK-29) Add query params to Java client
Add query params back into the Java client, along with tests to validate this functionality is working.
This commit is contained in:
parent
7fe744f49c
commit
e1e283d701
5 changed files with 87 additions and 2 deletions
|
@ -23,6 +23,7 @@ public class RequestOptions {
|
||||||
private Object body;
|
private Object body;
|
||||||
private boolean decompressBody = true;
|
private boolean decompressBody = true;
|
||||||
private ResponseBodyType as = ResponseBodyType.STREAM;
|
private ResponseBodyType as = ResponseBodyType.STREAM;
|
||||||
|
private Map<String, String> queryParams;
|
||||||
|
|
||||||
public RequestOptions(String url) {
|
public RequestOptions(String url) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
@ -121,4 +122,12 @@ public class RequestOptions {
|
||||||
this.as = as;
|
this.as = as;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getQueryParams() {
|
||||||
|
return queryParams;
|
||||||
|
}
|
||||||
|
public RequestOptions setQueryParams(Map<String, String> queryParams) {
|
||||||
|
this.queryParams = queryParams;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.security.KeyManagementException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
import java.net.URLEncoder;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -29,6 +30,25 @@ public class JavaClient {
|
||||||
|
|
||||||
private static final String PROTOCOL = "TLS";
|
private static final String PROTOCOL = "TLS";
|
||||||
|
|
||||||
|
private static String buildQueryString(Map<String, String> params) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
boolean first = true;
|
||||||
|
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||||
|
if (!first) {
|
||||||
|
sb.append("&");
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
try {
|
||||||
|
sb.append(URLEncoder.encode(entry.getKey(), "utf8"));
|
||||||
|
sb.append("=");
|
||||||
|
sb.append(URLEncoder.encode(entry.getValue(), "utf8"));
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new RuntimeException("Error while url-encoding query string", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private static Header[] prepareHeaders(RequestOptions options) {
|
private static Header[] prepareHeaders(RequestOptions options) {
|
||||||
Map<String, Header> result = new HashMap<String, Header>();
|
Map<String, Header> result = new HashMap<String, Header>();
|
||||||
Map<String, String> origHeaders = options.getHeaders();
|
Map<String, String> origHeaders = options.getHeaders();
|
||||||
|
@ -46,7 +66,17 @@ public class JavaClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CoercedRequestOptions coerceRequestOptions(RequestOptions options) {
|
private static CoercedRequestOptions coerceRequestOptions(RequestOptions options) {
|
||||||
String url = options.getUrl();
|
String url;
|
||||||
|
|
||||||
|
if (options.getQueryParams() != null) {
|
||||||
|
if (options.getUrl().indexOf('?') == -1) {
|
||||||
|
url = options.getUrl() + "?" + buildQueryString(options.getQueryParams());
|
||||||
|
} else {
|
||||||
|
url = options.getUrl() + "&" + buildQueryString(options.getQueryParams());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
url = options.getUrl();
|
||||||
|
}
|
||||||
|
|
||||||
SSLContext sslContext = null;
|
SSLContext sslContext = null;
|
||||||
if (options.getSslContext() != null) {
|
if (options.getSslContext() != null) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
(:import (com.puppetlabs.http.client AsyncHttpClient RequestOptions)
|
(:import (com.puppetlabs.http.client AsyncHttpClient RequestOptions)
|
||||||
(org.apache.http.impl.nio.client HttpAsyncClients))
|
(org.apache.http.impl.nio.client HttpAsyncClients))
|
||||||
(:require [clojure.test :refer :all]
|
(:require [clojure.test :refer :all]
|
||||||
|
[puppetlabs.http.client.test-common :refer :all]
|
||||||
[puppetlabs.trapperkeeper.core :as tk]
|
[puppetlabs.trapperkeeper.core :as tk]
|
||||||
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
|
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
|
||||||
[puppetlabs.trapperkeeper.testutils.logging :as testlogging]
|
[puppetlabs.trapperkeeper.testutils.logging :as testlogging]
|
||||||
|
@ -134,4 +135,16 @@
|
||||||
(let [response (async/request-with-client opts nil client)]
|
(let [response (async/request-with-client opts nil client)]
|
||||||
(is (= 200 (:status @response)))
|
(is (= 200 (:status @response)))
|
||||||
(is (= "Hello, World!" (slurp (:body @response))))))
|
(is (= "Hello, World!" (slurp (:body @response))))))
|
||||||
(.close client)))))
|
(.close client)))))
|
||||||
|
|
||||||
|
(deftest query-params-test-async
|
||||||
|
(testlogging/with-test-logging
|
||||||
|
(testutils/with-app-with-config app
|
||||||
|
[jetty9/jetty9-service test-params-web-service]
|
||||||
|
{:webserver {:port 8080}}
|
||||||
|
(testing "URL Query Parameters work with the Java client"
|
||||||
|
(let [options (RequestOptions. "http://localhost:8080/params")]
|
||||||
|
(.setQueryParams options queryparams)
|
||||||
|
(let [response (AsyncHttpClient/get options)]
|
||||||
|
(is (= 200 (.getStatus (.deref response))))
|
||||||
|
(is (= (str queryparams) (slurp (.getBody (.deref response)))))))))))
|
|
@ -6,6 +6,7 @@
|
||||||
(java.nio.charset Charset)
|
(java.nio.charset Charset)
|
||||||
(org.apache.http.impl.nio.client HttpAsyncClients))
|
(org.apache.http.impl.nio.client HttpAsyncClients))
|
||||||
(:require [clojure.test :refer :all]
|
(:require [clojure.test :refer :all]
|
||||||
|
[puppetlabs.http.client.test-common :refer :all]
|
||||||
[puppetlabs.trapperkeeper.core :as tk]
|
[puppetlabs.trapperkeeper.core :as tk]
|
||||||
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
|
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
|
||||||
[puppetlabs.trapperkeeper.testutils.logging :as testlogging]
|
[puppetlabs.trapperkeeper.testutils.logging :as testlogging]
|
||||||
|
@ -299,3 +300,15 @@
|
||||||
(deftest sync-client-decompression-disabled-test
|
(deftest sync-client-decompression-disabled-test
|
||||||
(test-compression "explicit disable" {:headers {"accept-encoding" "gzip"}
|
(test-compression "explicit disable" {:headers {"accept-encoding" "gzip"}
|
||||||
:decompress-body false} "gzip" "gzip" false))
|
:decompress-body false} "gzip" "gzip" false))
|
||||||
|
|
||||||
|
(deftest query-params-test-sync
|
||||||
|
(testlogging/with-test-logging
|
||||||
|
(testutils/with-app-with-config app
|
||||||
|
[jetty9/jetty9-service test-params-web-service]
|
||||||
|
{:webserver {:port 8080}}
|
||||||
|
(testing "URL Query Parameters work with the Java client"
|
||||||
|
(let [options (RequestOptions. "http://localhost:8080/params")]
|
||||||
|
(.setQueryParams options queryparams)
|
||||||
|
(let [response (SyncHttpClient/get options)]
|
||||||
|
(is (= 200 (.getStatus response)))
|
||||||
|
(is (= (str queryparams) (slurp (.getBody response))))))))))
|
||||||
|
|
20
test/puppetlabs/http/client/test_common.clj
Normal file
20
test/puppetlabs/http/client/test_common.clj
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
(ns puppetlabs.http.client.test-common
|
||||||
|
(:require [ring.middleware.params :as ring-params]
|
||||||
|
[puppetlabs.trapperkeeper.core :as tk]))
|
||||||
|
|
||||||
|
(defn query-params-test
|
||||||
|
[req]
|
||||||
|
{:status 200
|
||||||
|
:body (str (:params req))})
|
||||||
|
|
||||||
|
(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))
|
||||||
|
|
||||||
|
(def queryparams {"yellow" "submarine"
|
||||||
|
"eleanor" "rigby"})
|
Loading…
Reference in a new issue