use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class ProxyTest method testRequestLevelProxy.
@Test
public void testRequestLevelProxy() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = asyncHttpClient()) {
String target = "http://localhost:1234/";
Future<Response> f = client.prepareGet(target).setProxyServer(proxyServer("localhost", port1)).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 testProxyProperties.
@Test(enabled = false)
public void testProxyProperties() 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");
AsyncHttpClientConfigHelper.reloadProperties();
try (AsyncHttpClient client = asyncHttpClient(config().setUseProxyProperties(true))) {
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 testBothProxies.
@Test
public void testBothProxies() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(proxyServer("localhost", port1 - 1)))) {
String target = "http://localhost:1234/";
Future<Response> f = client.prepareGet(target).setProxyServer(proxyServer("localhost", port1)).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 EventPipelineTest method asyncPipelineTest.
@Test
public void asyncPipelineTest() throws Exception {
Consumer<Channel> httpAdditionalPipelineInitializer = channel -> channel.pipeline().addBefore("inflater", "copyEncodingHeader", new CopyEncodingHandler());
try (AsyncHttpClient p = asyncHttpClient(config().setHttpAdditionalChannelInitializer(httpAdditionalPipelineInitializer))) {
final CountDownLatch l = new CountDownLatch(1);
p.executeRequest(get(getTargetUrl()), new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) {
try {
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getHeader("X-Original-Content-Encoding"), "<original encoding>");
} finally {
l.countDown();
}
return response;
}
}).get();
if (!l.await(TIMEOUT, TimeUnit.SECONDS)) {
fail("Timeout out");
}
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class SimpleAsyncHttpClientTest method testSimpleTransferListener.
@Test
public void testSimpleTransferListener() throws Exception {
final List<Error> errors = Collections.synchronizedList(new ArrayList<>());
SimpleAHCTransferListener listener = new SimpleAHCTransferListener() {
public void onStatus(Uri uri, int statusCode, String statusText) {
try {
assertEquals(statusCode, 200);
assertEquals(uri.toUrl(), getTargetUrl());
} catch (Error e) {
errors.add(e);
throw e;
}
}
public void onHeaders(Uri uri, HttpHeaders headers) {
try {
assertEquals(uri.toUrl(), getTargetUrl());
assertNotNull(headers);
assertTrue(!headers.isEmpty());
assertEquals(headers.get("X-Custom"), "custom");
} catch (Error e) {
errors.add(e);
throw e;
}
}
public void onCompleted(Uri uri, int statusCode, String statusText) {
try {
assertEquals(statusCode, 200);
assertEquals(uri.toUrl(), getTargetUrl());
} catch (Error e) {
errors.add(e);
throw e;
}
}
public void onBytesSent(Uri uri, long amount, long current, long total) {
try {
assertEquals(uri.toUrl(), getTargetUrl());
// FIXME Netty bug, see
// https://github.com/netty/netty/issues/1855
// assertEquals(total, MY_MESSAGE.getBytes().length);
} catch (Error e) {
errors.add(e);
throw e;
}
}
public void onBytesReceived(Uri uri, long amount, long current, long total) {
try {
assertEquals(uri.toUrl(), getTargetUrl());
assertEquals(total, -1);
} catch (Error e) {
errors.add(e);
throw e;
}
}
};
try (SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setUrl(getTargetUrl()).setHeader("Custom", "custom").setListener(listener).build()) {
ByteArrayOutputStream o = new ByteArrayOutputStream(10);
InputStreamBodyGenerator generator = new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes()));
OutputStreamBodyConsumer consumer = new OutputStreamBodyConsumer(o);
Future<Response> future = client.post(generator, consumer);
Response response = future.get();
if (!errors.isEmpty()) {
for (Error e : errors) {
e.printStackTrace();
}
throw errors.get(0);
}
assertEquals(response.getStatusCode(), 200);
assertEquals(o.toString(), MY_MESSAGE);
}
}
Aggregations