use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.
the class SimpleAsyncHttpClientTest method testSimpleTransferListener.
@Test(groups = "standalone")
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);
}
}
use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.
the class HttpUtilsTest method testGetHostHeaderHasVirtualHost.
@Test
public void testGetHostHeaderHasVirtualHost() {
Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setVirtualHost("example.com").build();
Uri uri = Uri.create("http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs");
String hostHeader = HttpUtils.hostHeader(request, uri);
assertEquals(hostHeader, "example.com", "Incorrect hostHeader returned");
}
use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.
the class HttpUtilsTest method testGetPathWhenPathIsEmpty.
@Test
public void testGetPathWhenPathIsEmpty() {
Uri uri = Uri.create("http://stackoverflow.com");
String path = HttpUtils.getNonEmptyPath(uri);
assertEquals(path, "/", "Incorrect path returned from getNonEmptyPath");
}
use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.
the class HttpUtilsTest method testGetAuthority.
@Test
public void testGetAuthority() {
Uri uri = Uri.create("http://stackoverflow.com/questions/17814461/jacoco-maven-testng-0-test-coverage");
String authority = HttpUtils.getAuthority(uri);
assertEquals(authority, "stackoverflow.com:80", "Incorrect authority returned from getAuthority");
}
use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.
the class NettyConnectListener method onSuccess.
public void onSuccess(Channel channel, InetSocketAddress remoteAddress) {
Channels.setInactiveToken(channel);
TimeoutsHolder timeoutsHolder = future.getTimeoutsHolder();
if (futureIsAlreadyCancelled(channel)) {
return;
}
Request request = future.getTargetRequest();
Uri uri = request.getUri();
timeoutsHolder.initRemoteAddress(remoteAddress);
// in case of proxy tunneling, we'll add the SslHandler later, after the CONNECT request
if (future.getProxyServer() == null && uri.isSecured()) {
SslHandler sslHandler = null;
try {
sslHandler = channelManager.addSslHandler(channel.pipeline(), uri, request.getVirtualHost());
} catch (Exception sslError) {
NettyConnectListener.this.onFailure(channel, sslError);
return;
}
final AsyncHandlerExtensions asyncHandlerExtensions = toAsyncHandlerExtensions(future.getAsyncHandler());
if (asyncHandlerExtensions != null)
asyncHandlerExtensions.onTlsHandshakeAttempt();
sslHandler.handshakeFuture().addListener(new SimpleFutureListener<Channel>() {
@Override
protected void onSuccess(Channel value) throws Exception {
if (asyncHandlerExtensions != null)
asyncHandlerExtensions.onTlsHandshakeSuccess();
writeRequest(channel);
}
@Override
protected void onFailure(Throwable cause) throws Exception {
if (asyncHandlerExtensions != null)
asyncHandlerExtensions.onTlsHandshakeFailure(cause);
NettyConnectListener.this.onFailure(channel, cause);
}
});
} else {
writeRequest(channel);
}
}
Aggregations