Search in sources :

Example 1 with Uri

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);
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Uri(org.asynchttpclient.uri.Uri) Response(org.asynchttpclient.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 2 with Uri

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");
}
Also used : Request(org.asynchttpclient.Request) Uri(org.asynchttpclient.uri.Uri) Test(org.testng.annotations.Test)

Example 3 with Uri

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");
}
Also used : Uri(org.asynchttpclient.uri.Uri) Test(org.testng.annotations.Test)

Example 4 with Uri

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");
}
Also used : Uri(org.asynchttpclient.uri.Uri) Test(org.testng.annotations.Test)

Example 5 with Uri

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);
    }
}
Also used : Channel(io.netty.channel.Channel) HttpRequest(io.netty.handler.codec.http.HttpRequest) Request(org.asynchttpclient.Request) Uri(org.asynchttpclient.uri.Uri) SslHandler(io.netty.handler.ssl.SslHandler) ConnectException(java.net.ConnectException) AsyncHandlerExtensionsUtils.toAsyncHandlerExtensions(org.asynchttpclient.handler.AsyncHandlerExtensionsUtils.toAsyncHandlerExtensions) AsyncHandlerExtensions(org.asynchttpclient.handler.AsyncHandlerExtensions) TimeoutsHolder(org.asynchttpclient.netty.timeout.TimeoutsHolder)

Aggregations

Uri (org.asynchttpclient.uri.Uri)26 Test (org.testng.annotations.Test)14 Request (org.asynchttpclient.Request)5 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 Channel (io.netty.channel.Channel)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 Cookie (io.netty.handler.codec.http.cookie.Cookie)2 InetSocketAddress (java.net.InetSocketAddress)2 Proxy (java.net.Proxy)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 RequestBuilder (org.asynchttpclient.RequestBuilder)2 Response (org.asynchttpclient.Response)2 AsyncHandlerExtensions (org.asynchttpclient.handler.AsyncHandlerExtensions)2 ProxyServerSelector (org.asynchttpclient.proxy.ProxyServerSelector)2 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpVersion (io.netty.handler.codec.http.HttpVersion)1