Search in sources :

Example 16 with Uri

use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.

the class ResumableAsyncHandlerTest method testOnBodyPartReceivedWithDecoratedAsyncHandler.

@Test
public void testOnBodyPartReceivedWithDecoratedAsyncHandler() throws Exception {
    HttpResponseBodyPart bodyPart = PowerMockito.mock(HttpResponseBodyPart.class);
    when(bodyPart.getBodyPartBytes()).thenReturn(new byte[0]);
    ByteBuffer buffer = ByteBuffer.allocate(0);
    when(bodyPart.getBodyByteBuffer()).thenReturn(buffer);
    @SuppressWarnings("unchecked") AsyncHandler<Response> decoratedAsyncHandler = mock(AsyncHandler.class);
    State mockState = mock(State.class);
    when(decoratedAsyncHandler.onBodyPartReceived(bodyPart)).thenReturn(mockState);
    // following is needed to set the url variable
    HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class);
    when(mockResponseStatus.getStatusCode()).thenReturn(200);
    Uri mockUri = mock(Uri.class);
    when(mockUri.toUrl()).thenReturn("http://non.null");
    when(mockResponseStatus.getUri()).thenReturn(mockUri);
    ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler);
    handler.onStatusReceived(mockResponseStatus);
    State state = handler.onBodyPartReceived(bodyPart);
    assertEquals(state, mockState, "State should be equal to the state returned from decoratedAsyncHandler");
}
Also used : Response(org.asynchttpclient.Response) State(org.asynchttpclient.AsyncHandler.State) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) ByteBuffer(java.nio.ByteBuffer) Uri(org.asynchttpclient.uri.Uri) HttpResponseBodyPart(org.asynchttpclient.HttpResponseBodyPart) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with Uri

use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.

the class RealmTest method testStrongDigest.

@Test(groups = "standalone")
public void testStrongDigest() {
    String user = "user";
    String pass = "pass";
    String realm = "realm";
    String nonce = "nonce";
    String method = "GET";
    Uri uri = Uri.create("http://ahc.io/foo");
    String qop = "auth";
    Realm orig = //
    digestAuthRealm(user, pass).setNonce(//
    nonce).setUri(//
    uri).setMethodName(//
    method).setRealmName(//
    realm).setQop(qop).build();
    String nc = orig.getNc();
    String cnonce = orig.getCnonce();
    String ha1 = getMd5(user + ":" + realm + ":" + pass);
    String ha2 = getMd5(method + ":" + uri.getPath());
    String expectedResponse = getMd5(ha1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + ha2);
    assertEquals(orig.getResponse(), expectedResponse);
}
Also used : Uri(org.asynchttpclient.uri.Uri) Test(org.testng.annotations.Test)

Example 18 with Uri

use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.

the class RealmTest method testOldDigest.

private void testOldDigest(String qop) {
    String user = "user";
    String pass = "pass";
    String realm = "realm";
    String nonce = "nonce";
    String method = "GET";
    Uri uri = Uri.create("http://ahc.io/foo");
    Realm orig = //
    digestAuthRealm(user, pass).setNonce(//
    nonce).setUri(//
    uri).setMethodName(//
    method).setRealmName(//
    realm).setQop(qop).build();
    String ha1 = getMd5(user + ":" + realm + ":" + pass);
    String ha2 = getMd5(method + ":" + uri.getPath());
    String expectedResponse = getMd5(ha1 + ":" + nonce + ":" + ha2);
    assertEquals(orig.getResponse(), expectedResponse);
}
Also used : Uri(org.asynchttpclient.uri.Uri)

Example 19 with Uri

use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.

the class ProxyUtils method createProxyServerSelector.

/**
     * Creates a proxy server instance from the given properties.
     * Currently the default http.* proxy properties are supported as well as properties specific for AHC.
     *
     * @param properties the properties to evaluate. Must not be null.
     * @return a ProxyServer instance or null, if no valid properties were set.
     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">Networking Properties</a>
     * @see #PROXY_HOST
     * @see #PROXY_PORT
     * @see #PROXY_NONPROXYHOSTS
     */
public static ProxyServerSelector createProxyServerSelector(Properties properties) {
    String host = properties.getProperty(PROXY_HOST);
    if (host != null) {
        int port = Integer.valueOf(properties.getProperty(PROXY_PORT, "80"));
        String principal = properties.getProperty(PROXY_USER);
        String password = properties.getProperty(PROXY_PASSWORD);
        Realm realm = null;
        if (principal != null) {
            realm = basicAuthRealm(principal, password).build();
        }
        ProxyServer.Builder proxyServer = proxyServer(host, port).setRealm(realm);
        String nonProxyHosts = properties.getProperty(PROXY_NONPROXYHOSTS);
        if (nonProxyHosts != null) {
            proxyServer.setNonProxyHosts(new ArrayList<>(Arrays.asList(nonProxyHosts.split("\\|"))));
        }
        ProxyServer proxy = proxyServer.build();
        return uri -> proxy;
    }
    return ProxyServerSelector.NO_PROXY_SELECTOR;
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) Logger(org.slf4j.Logger) Realm(org.asynchttpclient.Realm) ProxyServerSelector(org.asynchttpclient.proxy.ProxyServerSelector) Request(org.asynchttpclient.Request) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) InetSocketAddress(java.net.InetSocketAddress) ProxyServer(org.asynchttpclient.proxy.ProxyServer) ArrayList(java.util.ArrayList) ProxySelector(java.net.ProxySelector) List(java.util.List) Dsl(org.asynchttpclient.Dsl) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) Proxy(java.net.Proxy) Uri(org.asynchttpclient.uri.Uri) URI(java.net.URI) Realm(org.asynchttpclient.Realm) ProxyServer(org.asynchttpclient.proxy.ProxyServer)

Example 20 with Uri

use of org.asynchttpclient.uri.Uri in project async-http-client by AsyncHttpClient.

the class ProxyUtils method createProxyServerSelector.

/**
     * Create a proxy server selector based on the passed in JDK proxy selector.
     *
     * @param proxySelector The proxy selector to use.  Must not be null.
     * @return The proxy server selector.
     */
public static ProxyServerSelector createProxyServerSelector(final ProxySelector proxySelector) {
    return new ProxyServerSelector() {

        public ProxyServer select(Uri uri) {
            try {
                URI javaUri = uri.toJavaNetURI();
                List<Proxy> proxies = proxySelector.select(javaUri);
                if (proxies != null) {
                    // Loop through them until we find one that we know how to use
                    for (Proxy proxy : proxies) {
                        switch(proxy.type()) {
                            case HTTP:
                                if (!(proxy.address() instanceof InetSocketAddress)) {
                                    logger.warn("Don't know how to connect to address " + proxy.address());
                                    return null;
                                } else {
                                    InetSocketAddress address = (InetSocketAddress) proxy.address();
                                    return proxyServer(address.getHostName(), address.getPort()).build();
                                }
                            case DIRECT:
                                return null;
                            default:
                                logger.warn("ProxySelector returned proxy type that we don't know how to use: " + proxy.type());
                                break;
                        }
                    }
                }
                return null;
            } catch (URISyntaxException e) {
                logger.warn(uri + " couldn't be turned into a java.net.URI", e);
                return null;
            }
        }
    };
}
Also used : Proxy(java.net.Proxy) ProxyServerSelector(org.asynchttpclient.proxy.ProxyServerSelector) InetSocketAddress(java.net.InetSocketAddress) URISyntaxException(java.net.URISyntaxException) Uri(org.asynchttpclient.uri.Uri) URI(java.net.URI)

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