Search in sources :

Example 11 with Response

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

the class AsyncHttpObservableTest method testObserveMultiple.

@Test(groups = "standalone")
public void testObserveMultiple() {
    final TestSubscriber<Response> tester = new TestSubscriber<>();
    try (AsyncHttpClient client = asyncHttpClient()) {
        Observable<Response> o1 = AsyncHttpObservable.observe(() -> client.prepareGet("http://gatling.io"));
        Observable<Response> o2 = AsyncHttpObservable.observe(() -> client.prepareGet("http://www.wisc.edu").setFollowRedirect(true));
        Observable<Response> o3 = AsyncHttpObservable.observe(() -> client.prepareGet("http://www.umn.edu").setFollowRedirect(true));
        Observable<Response> all = Observable.merge(o1, o2, o3);
        all.subscribe(tester);
        tester.awaitTerminalEvent();
        tester.assertTerminalEvent();
        tester.assertNoErrors();
        tester.assertCompleted();
        List<Response> responses = tester.getOnNextEvents();
        assertNotNull(responses);
        assertEquals(responses.size(), 3);
        for (Response response : responses) {
            assertEquals(response.getStatusCode(), 200);
        }
    } catch (Exception e) {
        Thread.currentThread().interrupt();
    }
}
Also used : Response(org.asynchttpclient.Response) TestSubscriber(rx.observers.TestSubscriber) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 12 with Response

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

the class AsyncHttpSingleTest method testAbort.

@Test(groups = "standalone")
public void testAbort() throws Exception {
    final TestSubscriber<Response> subscriber = new TestSubscriber<>();
    try (AsyncHttpClient client = asyncHttpClient()) {
        final Single<Response> underTest = AsyncHttpSingle.create(client.prepareGet("http://gatling.io"), () -> new AsyncCompletionHandlerBase() {

            @Override
            public State onStatusReceived(HttpResponseStatus status) {
                return State.ABORT;
            }
        });
        underTest.subscribe(subscriber);
        subscriber.awaitTerminalEvent();
    }
    subscriber.assertTerminalEvent();
    subscriber.assertNoErrors();
    subscriber.assertCompleted();
    subscriber.assertValue(null);
}
Also used : Response(org.asynchttpclient.Response) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) TestSubscriber(rx.observers.TestSubscriber) AsyncCompletionHandlerBase(org.asynchttpclient.AsyncCompletionHandlerBase) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 13 with Response

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

the class SimpleAsyncHttpClient method execute.

private Future<Response> execute(RequestBuilder rb, BodyConsumer bodyConsumer, ThrowableHandler throwableHandler) throws IOException {
    if (throwableHandler == null) {
        throwableHandler = defaultThrowableHandler;
    }
    Request request = rb.build();
    ProgressAsyncHandler<Response> handler = new BodyConsumerAsyncHandler(bodyConsumer, throwableHandler, errorDocumentBehaviour, request.getUri(), listener);
    if (resumeEnabled && request.getMethod().equals("GET") && bodyConsumer != null && bodyConsumer instanceof ResumableBodyConsumer) {
        ResumableBodyConsumer fileBodyConsumer = (ResumableBodyConsumer) bodyConsumer;
        long length = fileBodyConsumer.getTransferredBytes();
        fileBodyConsumer.resume();
        handler = new ResumableBodyConsumerAsyncHandler(length, handler);
    }
    return getAsyncHttpClient().executeRequest(request, handler);
}
Also used : Response(org.asynchttpclient.Response) Request(org.asynchttpclient.Request)

Example 14 with Response

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

the class ProxyTest method testProxyProperties.

// @Test(groups = "standalone")
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 {
            resp = f.get(3, TimeUnit.SECONDS);
            fail("should not be able to connect");
        } catch (ExecutionException e) {
        // ok, no proxy used
        }
    } finally {
        System.setProperties(originalProps);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) Properties(java.util.Properties) ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 15 with Response

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

the class ProxyTest method testGlobalProxy.

@Test(groups = "standalone")
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"), "/");
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

Response (org.asynchttpclient.Response)90 Test (org.testng.annotations.Test)78 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)42 Request (org.asynchttpclient.Request)14 RequestBuilder (org.asynchttpclient.RequestBuilder)14 IOException (java.io.IOException)12 ExecutionException (java.util.concurrent.ExecutionException)12 File (java.io.File)10 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 InputStreamBodyGenerator (org.asynchttpclient.request.body.generator.InputStreamBodyGenerator)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 TestSubscriber (rx.observers.TestSubscriber)6 ArrayList (java.util.ArrayList)5