use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class BodyDeferringAsyncHandlerTest method testPipedStreams.
@Test
public void testPipedStreams() throws Exception {
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
PipedOutputStream pout = new PipedOutputStream();
try (PipedInputStream pin = new PipedInputStream(pout)) {
BodyDeferringAsyncHandler handler = new BodyDeferringAsyncHandler(pout);
ListenableFuture<Response> respFut = client.prepareGet(getTargetUrl()).execute(handler);
Response resp = handler.getResponse();
if (resp.getStatusCode() == 200) {
try (BodyDeferringInputStream is = new BodyDeferringInputStream(respFut, handler, pin)) {
String body = IOUtils.toString(is, StandardCharsets.UTF_8);
assertTrue(body.contains("ABCDEF"));
}
} else {
throw new IOException("HTTP error " + resp.getStatusCode());
}
}
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class ProxyTest method testGlobalProxy.
// @Test
// public void asyncDoPostProxyTest() throws Throwable {
// try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(proxyServer("localhost", port2).build()))) {
// HttpHeaders h = new DefaultHttpHeaders();
// h.add(CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 5; i++) {
// sb.append("param_").append(i).append("=value_").append(i).append("&");
// }
// sb.setLength(sb.length() - 1);
//
// Response response = client.preparePost(getTargetUrl()).setHeaders(h).setBody(sb.toString()).execute(new AsyncCompletionHandler<Response>() {
// @Override
// public Response onCompleted(Response response) throws Throwable {
// return response;
// }
//
// @Override
// public void onThrowable(Throwable t) {
// }
// }).get();
//
// assertEquals(response.getStatusCode(), 200);
// assertEquals(response.getHeader("X-" + CONTENT_TYPE), APPLICATION_X_WWW_FORM_URLENCODED);
// }
// }
@Test
public void testGlobalProxy() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(proxyServer("localhost", port1)))) {
String target = "http://localhost:1234/";
Future<Response> f = client.prepareGet(target).execute();
Response resp = f.get(3, TimeUnit.SECONDS);
assertNotNull(resp);
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
assertEquals(resp.getHeader("target"), "/");
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class ProxyTest method testProxyActivationProperty.
@Test(enabled = false)
public void testProxyActivationProperty() throws IOException, ExecutionException, TimeoutException, InterruptedException {
// FIXME not threadsafe!
Properties originalProps = new Properties();
originalProps.putAll(System.getProperties());
System.setProperty(ProxyUtils.PROXY_HOST, "127.0.0.1");
System.setProperty(ProxyUtils.PROXY_PORT, String.valueOf(port1));
System.setProperty(ProxyUtils.PROXY_NONPROXYHOSTS, "localhost");
System.setProperty(AsyncHttpClientConfigDefaults.ASYNC_CLIENT_CONFIG_ROOT + "useProxyProperties", "true");
AsyncHttpClientConfigHelper.reloadProperties();
try (AsyncHttpClient client = asyncHttpClient()) {
String proxifiedTarget = "http://127.0.0.1:1234/";
Future<Response> f = client.prepareGet(proxifiedTarget).execute();
Response resp = f.get(3, TimeUnit.SECONDS);
assertNotNull(resp);
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
assertEquals(resp.getHeader("target"), "/");
String nonProxifiedTarget = "http://localhost:1234/";
f = client.prepareGet(nonProxifiedTarget).execute();
try {
f.get(3, TimeUnit.SECONDS);
fail("should not be able to connect");
} catch (ExecutionException e) {
// ok, no proxy used
}
} finally {
System.setProperties(originalProps);
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class ProxyTest method testRequestNonProxyHost.
@Test
public void testRequestNonProxyHost() throws IOException, ExecutionException, TimeoutException, InterruptedException {
ProxyServer proxy = proxyServer("localhost", port1 - 1).setNonProxyHost("localhost").build();
try (AsyncHttpClient client = asyncHttpClient()) {
String target = "http://localhost:" + port1 + "/";
Future<Response> f = client.prepareGet(target).setProxyServer(proxy).execute();
Response resp = f.get(3, TimeUnit.SECONDS);
assertNotNull(resp);
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
assertEquals(resp.getHeader("target"), "/");
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class ProxyTest method testIgnoreProxyPropertiesByDefault.
@Test(enabled = false)
public void testIgnoreProxyPropertiesByDefault() throws IOException, TimeoutException, InterruptedException {
// FIXME not threadsafe!
Properties originalProps = new Properties();
originalProps.putAll(System.getProperties());
System.setProperty(ProxyUtils.PROXY_HOST, "localhost");
System.setProperty(ProxyUtils.PROXY_PORT, String.valueOf(port1));
System.setProperty(ProxyUtils.PROXY_NONPROXYHOSTS, "localhost");
AsyncHttpClientConfigHelper.reloadProperties();
try (AsyncHttpClient client = asyncHttpClient()) {
String target = "http://localhost:1234/";
Future<Response> f = client.prepareGet(target).execute();
try {
f.get(3, TimeUnit.SECONDS);
fail("should not be able to connect");
} catch (ExecutionException e) {
// ok, no proxy used
}
} finally {
System.setProperties(originalProps);
}
}
Aggregations