use of org.talend.sdk.component.api.service.http.Query in project component-runtime by Talend.
the class HttpClientFactoryImplTest method ignoreNullQueryParam.
@Test
void ignoreNullQueryParam() throws IOException {
final HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/").setHandler(httpExchange -> {
final String query = httpExchange.getRequestURI().getQuery();
httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, query.getBytes("utf-8").length);
httpExchange.getResponseBody().write(query.getBytes("utf-8"));
httpExchange.close();
});
try {
server.start();
final IgnoreNullQueryParam ok = newDefaultFactory().create(IgnoreNullQueryParam.class, null);
ok.base("http://localhost:" + server.getAddress().getPort() + "/api");
String query = ok.get("value", "", null);
final Map<String, String> params = Stream.of(query.split("&")).map(s -> {
final int equal = s.indexOf('=');
if (equal > 0) {
return new String[] { s.substring(0, equal), s.substring(equal + 1, s.length()) };
}
return new String[] { s, "true" };
}).collect(toMap(s -> s[0], s -> s[1]));
assertTrue(params.containsKey("param"));
assertTrue(params.containsKey("emptyParam"));
assertTrue(!params.containsKey("nullParam"));
} finally {
server.stop(0);
}
}
Aggregations