@Test(groups = {"online", "default_provider"}) public void testRequestProxy() throws IOException, InterruptedException, ExecutionException, TimeoutException { AsyncHttpClientConfig.Builder b = new AsyncHttpClientConfig.Builder(); b.setFollowRedirects(true); ProxyServer ps = new ProxyServer(ProxyServer.Protocol.HTTPS, "127.0.0.1", port1); AsyncHttpClientConfig config = b.build(); AsyncHttpClient asyncHttpClient = getAsyncHttpClient(config); RequestBuilder rb = new RequestBuilder("GET").setProxyServer(ps).setUrl(getTargetUrl2()); Future<Response> responseFuture = asyncHttpClient.executeRequest( rb.build(), new AsyncCompletionHandlerBase() { public void onThrowable(Throwable t) { t.printStackTrace(); log.debug(t.getMessage(), t); } @Override public Response onCompleted(Response response) throws Exception { return response; } }); Response r = responseFuture.get(); assertEquals(r.getStatusCode(), 200); assertEquals(r.getHeader("X-Proxy-Connection"), "keep-alive"); asyncHttpClient.close(); }
public void fetch() throws IOException { long start = System.currentTimeMillis(); LOG.info("Processing " + file); int cnt = 0; try { BufferedReader data = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line; while ((line = data.readLine()) != null) { String[] arr = line.split("\t"); long id = Long.parseLong(arr[0]); String username = (arr.length > 1) ? arr[1] : "a"; String url = getUrl(id, username); connections.incrementAndGet(); crawlURL(url, new TweetFetcherHandler(id, username, url, 0, !this.noFollow)); cnt++; if (cnt % TWEET_BLOCK_SIZE == 0) { LOG.info(cnt + " requests submitted"); } } } catch (IOException e) { e.printStackTrace(); } // Wait for the last requests to complete. LOG.info("Waiting for remaining requests (" + connections.get() + ") to finish!"); while (connections.get() > 0) { try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } asyncHttpClient.close(); long end = System.currentTimeMillis(); long duration = end - start; LOG.info("Total request submitted: " + cnt); LOG.info(crawl.size() + " tweets fetched in " + duration + "ms"); LOG.info("Writing tweets..."); int written = 0; Configuration conf = new Configuration(); OutputStreamWriter out = new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(output))); for (Map.Entry<Long, String> entry : crawl.entrySet()) { written++; out.write(entry.getValue() + "\n"); } out.close(); LOG.info(written + " statuses written."); LOG.info("Done!"); }
@Test(groups = {"standalone", "default_provider"}) public void propFindWebDavTest() throws InterruptedException, IOException, ExecutionException { AsyncHttpClient c = getAsyncHttpClient(null); try { Request mkcolRequest = new RequestBuilder("MKCOL").setUrl(getTargetUrl()).build(); Response response = c.executeRequest(mkcolRequest).get(); assertEquals(response.getStatusCode(), 201); Request putRequest = new RequestBuilder("PUT") .setUrl(String.format("http://127.0.0.1:%s/folder1/Test.txt", port1)) .setBody("this is a test") .build(); response = c.executeRequest(putRequest).get(); assertEquals(response.getStatusCode(), 201); Request propFindRequest = new RequestBuilder("PROPFIND") .setUrl(String.format("http://127.0.0.1:%s/folder1/Test.txt", port1)) .build(); response = c.executeRequest(propFindRequest).get(); assertEquals(response.getStatusCode(), 207); assertTrue(response.getResponseBody().contains("<status>HTTP/1.1 200 OK</status>")); } finally { c.close(); } }
@Test(groups = {"online", "default_provider"}) public void redirected302Test() throws Throwable { isSet.getAndSet(false); AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setFollowRedirects(true).build(); AsyncHttpClient c = getAsyncHttpClient(cg); // once Response response = c.prepareGet(getTargetUrl()) .setHeader("X-redirect", "http://www.google.com/") .execute() .get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); String anyGoogleSubdomain = "http://www.google.[a-z]{1,}:80"; String baseUrl = getBaseUrl(response.getUri()); assertTrue( baseUrl.matches(anyGoogleSubdomain), "response does not show redirection to " + anyGoogleSubdomain); c.close(); }
@Test(groups = {"standalone", "default_provider"}) public void propFindCompletionHandlerWebDavTest() throws InterruptedException, IOException, ExecutionException { AsyncHttpClient c = getAsyncHttpClient(null); try { Request mkcolRequest = new RequestBuilder("MKCOL").setUrl(getTargetUrl()).build(); Response response = c.executeRequest(mkcolRequest).get(); assertEquals(response.getStatusCode(), 201); Request propFindRequest = new RequestBuilder("PROPFIND").setUrl(getTargetUrl()).build(); WebDavResponse webDavResponse = c.executeRequest( propFindRequest, new WebDavCompletionHandlerBase<WebDavResponse>() { /** {@inheritDoc} */ /* @Override */ public void onThrowable(Throwable t) { t.printStackTrace(); } @Override public WebDavResponse onCompleted(WebDavResponse response) throws Exception { return response; } }) .get(); assertNotNull(webDavResponse); assertEquals(webDavResponse.getStatusCode(), 200); } finally { c.close(); } }
@Override @Test public void win7DisconnectTest() throws Throwable { final AtomicInteger count = new AtomicInteger(0); AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().build()); AsyncCompletionHandler<Response> handler = new AsyncCompletionHandlerAdapter() { @Override public Response onCompleted(Response response) throws Exception { count.incrementAndGet(); StackTraceElement e = new StackTraceElement("sun.nio.ch.SocketDispatcher", "read0", null, -1); IOException t = new IOException(); t.setStackTrace(new StackTraceElement[] {e}); throw t; } }; try { client.prepareGet(getTargetUrl()).execute(handler).get(); fail("Must have received an exception"); } catch (ExecutionException ex) { assertNotNull(ex); assertNotNull(ex.getCause()); assertEquals(ex.getCause().getClass(), IOException.class); assertEquals(count.get(), 1); } client.close(); }
@Override @Test public void multipleMaxConnectionOpenTest() throws Throwable { AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder() .setAllowPoolingConnection(true) .setConnectionTimeoutInMs(5000) .setMaximumConnectionsTotal(1) .build(); AsyncHttpClient c = getAsyncHttpClient(cg); String body = "hello there"; // once Response response = c.preparePost(getTargetUrl()).setBody(body).execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getResponseBody(), body); // twice Exception exception = null; try { c.preparePost(String.format("http://127.0.0.1:%d/foo/test", port2)) .setBody(body) .execute() .get(TIMEOUT, TimeUnit.SECONDS); fail("Should throw exception. Too many connections issued."); } catch (Exception ex) { ex.printStackTrace(); exception = ex; } assertNotNull(exception); c.close(); }
@Override protected void finalize() throws Throwable { try { if (!isClosed.get()) { close(); } } finally { super.finalize(); } }
@Test public void happyTest() throws Exception { BasicWSRoute route = context.getRegistry().lookup("basicRoute", BasicWSRoute.class); AsyncHttpClient c = new AsyncHttpClient(); WebSocket websocket = c.prepareGet("ws:" + route.getConnectionUri()) .execute( new WebSocketUpgradeHandler.Builder() .addWebSocketListener( new WebSocketTextListener() { @Override public void onMessage(String message) { received.add(message); System.out.println("received --> " + message); latch.countDown(); } @Override public void onFragment(String fragment, boolean last) {} @Override public void onOpen(WebSocket websocket) {} @Override public void onClose(WebSocket websocket) {} @Override public void onError(Throwable t) { t.printStackTrace(); } }) .build()) .get(); // Jackson ObjectMapper mapper = new ObjectMapper(); WsMessage info = new WsMessage(); info.setHeader("woot"); info.setContent("hest"); websocket.sendTextMessage(mapper.writeValueAsString(info)); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertEquals(1, received.size()); WsMessage result = mapper.readValue(received.get(0), WsMessage.class); assertTrue(result.getContent().startsWith("pong:")); websocket.close(); c.close(); }
@Test(groups = {"standalone", "default_provider"}) public void mkcolWebDavTest1() throws InterruptedException, IOException, ExecutionException { AsyncHttpClient c = getAsyncHttpClient(null); try { Request mkcolRequest = new RequestBuilder("MKCOL").setUrl(getTargetUrl()).build(); Response response = c.executeRequest(mkcolRequest).get(); assertEquals(response.getStatusCode(), 201); } finally { c.close(); } }
private void doTestNegative(final int status, boolean strict) throws Exception { AsyncHttpClient p = getAsyncHttpClient( new AsyncHttpClientConfig.Builder() .setFollowRedirects(true) .setStrict302Handling(strict) .addResponseFilter( new ResponseFilter() { @Override public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException { // pass on the x-expect-get and remove the x-redirect // headers if found in the response ctx.getResponseHeaders().getHeaders().get("x-expect-post"); ctx.getRequest().getHeaders().add("x-expect-post", "true"); ctx.getRequest().getHeaders().remove("x-redirect"); return ctx; } }) .build()); try { Request request = new RequestBuilder("POST") .setUrl(getTargetUrl()) .addParameter("q", "a b") .addHeader("x-redirect", +status + "@" + "http://localhost:" + port1 + "/foo/bar/baz") .addHeader("x-negative", "true") .build(); Future<Integer> responseFuture = p.executeRequest( request, new AsyncCompletionHandler<Integer>() { @Override public Integer onCompleted(Response response) throws Exception { return response.getStatusCode(); } /* @Override */ public void onThrowable(Throwable t) { t.printStackTrace(); Assert.fail("Unexpected exception: " + t.getMessage(), t); } }); int statusCode = responseFuture.get(); Assert.assertEquals(statusCode, 200); } finally { p.close(); } }
@Test(groups = {"standalone", "default_provider"}) public void basicPropFindWebDavTest() throws InterruptedException, IOException, ExecutionException { AsyncHttpClient c = getAsyncHttpClient(null); try { Request propFindRequest = new RequestBuilder("PROPFIND").setUrl(getTargetUrl()).build(); Response response = c.executeRequest(propFindRequest).get(); assertEquals(response.getStatusCode(), 404); } finally { c.close(); } }
@Test(groups = {"standalone", "default_provider"}) public void unexpectingTimeoutTest() throws IOException { final AtomicInteger counts = new AtomicInteger(); final int timeout = 100; final AsyncHttpClient client = getAsyncHttpClient( new AsyncHttpClientConfig.Builder().setRequestTimeoutInMs(timeout).build()); try { Future<Response> responseFuture = client .prepareGet(getTargetUrl()) .execute( new AsyncCompletionHandler<Response>() { @Override public Response onCompleted(Response response) throws Exception { counts.incrementAndGet(); return response; } @Override public void onThrowable(Throwable t) { counts.incrementAndGet(); super.onThrowable(t); } }); // currently, an exception is expected // because the grizzly provider would throw IllegalStateException if WWW-Authenticate header // doesn't exist with 401 response status. try { Response response = responseFuture.get(); assertNull(response); } catch (InterruptedException e) { fail("Interrupted.", e); } catch (ExecutionException e) { assertFalse(e.getCause() instanceof TimeoutException); assertEquals(e.getCause().getMessage(), getExpectedTimeoutMessage()); } // wait for timeout again. try { Thread.sleep(timeout * 2); } catch (InterruptedException e) { fail("Interrupted.", e); } // the result should be either onCompleted or onThrowable. assertEquals(1, counts.get(), "result should be one"); } finally { client.close(); } }
/** Returns the directory nodes retrieved by remote browsing of proxy repository. */ @Override @GET @ResourceMethodSignature(output = MavenRepositoryReaderResponse.class) public Object get(Context context, Request request, Response response, Variant variant) throws ResourceException { String id = request.getAttributes().get(AbstractRepositoryPlexusResource.REPOSITORY_ID_KEY).toString(); ResourceStoreRequest storageItem = getResourceStoreRequest(request); String remotePath = null; try { remotePath = URLDecoder.decode(storageItem.getRequestPath().substring(1), "UTF-8"); } catch (Exception e) { // old way remotePath = storageItem.getRequestPath().substring(1); } ProxyRepository proxyRepository = null; AsyncHttpClient client = null; try { proxyRepository = getUnprotectedRepositoryRegistry().getRepositoryWithFacet(id, ProxyRepository.class); client = getHttpClient(proxyRepository); MavenRepositoryReader mr = new MavenRepositoryReader(client); MavenRepositoryReaderResponse data = new MavenRepositoryReaderResponse(); // we really should not do the encoding here, but this is work around until NEXUS-4058 is // fixed. data.setData( mr.extract( remotePath, createRemoteResourceReference(request, id, "").toString(false, false), proxyRepository, id)); logger.debug("return value is {}", data.toString()); return data; } catch (NoSuchRepositoryException e) { this.logger.warn("Could not find repository: " + id, e); throw new ResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "Could not find repository: " + id, e); } finally { if (client != null) { client.close(); } } }
@Test(groups = {"standalone", "default_provider"}) public void testNonProxyHostsSsl() throws IOException, ExecutionException, TimeoutException, InterruptedException { AsyncHttpClient client = getAsyncHttpClient(null); Response resp = client .prepareGet(getTargetUrl2()) .setProxyServer(new ProxyServer("127.0.0.1", port1 - 1).addNonProxyHost("127.0.0.1")) .execute() .get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("X-pathInfo"), "/foo/test"); client.close(); }
@Test(timeOut = 20000) public void testSuspendTimeout() { logger.info("Running testSuspendTimeout"); AsyncHttpClient c = new AsyncHttpClient(); try { long t1 = System.currentTimeMillis(); Response r = c.prepareGet(urlTarget).execute().get(10, TimeUnit.SECONDS); assertNotNull(r); assertEquals(r.getStatusCode(), 200); String resume = r.getResponseBody(); assertEquals(resume, "resume"); long current = System.currentTimeMillis() - t1; assertTrue(current > 5000 && current < 10000); } catch (Exception e) { logger.error("test failed", e); fail(e.getMessage()); } c.close(); }
@Override protected void updateContext(ProxyRepository repository, RemoteStorageContext context) throws RemoteStorageException { if (context.hasContextObject(CTX_KEY_CLIENT)) { // proper shutdown of AHC, but cannot call getClient() here that would result in endless loop! AsyncHttpClient oldClient = (AsyncHttpClient) context.getContextObject(CTX_KEY_CLIENT); // TODO: AHC-26: current solution would kill ongoing downloads, be smarter oldClient.close(); } final AsyncHttpClientConfig.Builder clientConfigBuilder = ahcProvider.getAsyncHttpClientConfigBuilder(repository, context); final AsyncHttpClient client = new AsyncHttpClient(clientConfigBuilder.build()); context.putContextObject(CTX_KEY_CLIENT, client); context.putContextObject(CTX_KEY_S3_FLAG, new BooleanFlagHolder()); }
@Test(groups = {"standalone", "default_provider"}) public void relativePathRedirectTest() throws Throwable { isSet.getAndSet(false); AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setFollowRedirects(true).build(); AsyncHttpClient c = getAsyncHttpClient(cg); String redirectTarget = "bar/test1"; String destinationUrl = new URI(getTargetUrl()).resolve(redirectTarget).toString(); Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", redirectTarget).execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(response.getUri().toString(), destinationUrl); log.debug("{} was redirected to {}", redirectTarget, destinationUrl); c.close(); }
@Test public void testWSHttpCall() throws Exception { AsyncHttpClient c = new AsyncHttpClient(); WebSocket websocket = c.prepareGet("ws://127.0.0.1:" + port + "/echo") .execute( new WebSocketUpgradeHandler.Builder() .addWebSocketListener( new WebSocketTextListener() { @Override public void onMessage(String message) {} @Override public void onFragment(String fragment, boolean last) {} @Override public void onOpen(WebSocket websocket) {} @Override public void onClose(WebSocket websocket) {} @Override public void onError(Throwable t) { t.printStackTrace(); } }) .build()) .get(); MockEndpoint result = getMockEndpoint("mock:result"); result.expectedBodiesReceived("Test"); websocket.sendTextMessage("Test"); result.assertIsSatisfied(); websocket.close(); c.close(); }
@Test(groups = {"standalone", "default_provider"}) public void redirected302InvalidTest() throws Throwable { isSet.getAndSet(false); AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setFollowRedirects(true).build(); AsyncHttpClient c = getAsyncHttpClient(cg); // If the test hit a proxy, no ConnectException will be thrown and instead of 404 will be // returned. try { Response response = c.prepareGet(getTargetUrl()) .setHeader("X-redirect", String.format("http://127.0.0.1:%d/", port2)) .execute() .get(); assertNotNull(response); assertEquals(response.getStatusCode(), 404); } catch (ExecutionException ex) { assertEquals(ex.getCause().getClass(), ConnectException.class); } c.close(); }
@Test(groups = {"standalone", "default_provider"}) public void testInvalidConnectionsPool() { ConnectionsPool<String, Connection> cp = new ConnectionsPool<String, Connection>() { public boolean offer(String key, Connection connection) { return false; } public Connection poll(String connection) { return null; } public boolean removeAll(Connection connection) { return false; } public boolean canCacheConnection() { return false; } public void destroy() {} }; AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setConnectionsPool(cp).build()); Exception exception = null; try { client.prepareGet(getTargetUrl()).execute().get(TIMEOUT, TimeUnit.SECONDS); } catch (Exception ex) { ex.printStackTrace(); exception = ex; } assertNotNull(exception); client.close(); }
public void closeAsyncClient() { asyncHttpClient.close(); }
@Test(timeOut = 60000, enabled = true) public void onResumeWithTimerTest() { logger.info("{}: running test: onResumeWithTimerTest", getClass().getSimpleName()); final AtomicBoolean onResume = new AtomicBoolean(); final CountDownLatch eventReceived = new CountDownLatch(1); final CountDownLatch suspended = new CountDownLatch(1); final AtomicReference<AtmosphereResource> resource = new AtomicReference<AtmosphereResource>(); final CountDownLatch latch = new CountDownLatch(1); atmoServlet .framework() .addAtmosphereHandler( ROOT, new AbstractHttpAtmosphereHandler() { public void onRequest(final AtmosphereResource r) throws IOException { r.addEventListener( new WebSocketEventListenerAdapter() { @Override public void onSuspend(AtmosphereResourceEvent event) { logger.trace("{}", event); suspended.countDown(); } @Override public void onResume(AtmosphereResourceEvent event) { onResume.set(true); eventReceived.countDown(); } }); if (suspended.getCount() != 0) { resource.set(r.suspend()); } else { try { suspended.await(5, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } String message = r.getRequest().getReader().readLine(); r.getBroadcaster().broadcast(message); new Timer() .schedule( new TimerTask() { @Override public void run() { resource.get().resume(); } }, 2000); } } public void onStateChange(AtmosphereResourceEvent event) throws IOException { if (event.isSuspended()) { event.getResource().write(event.getMessage().toString().getBytes()); } } }, BroadcasterFactory.getDefault().get(DefaultBroadcaster.class, "suspend")); AsyncHttpClient c = new AsyncHttpClient(); try { final AtomicReference<String> response = new AtomicReference<String>(null); WebSocket w = c.prepareGet(urlTarget).execute(new WebSocketUpgradeHandler.Builder().build()).get(); w.addWebSocketListener( new WebSocketTextListener() { @Override public void onMessage(String message) { response.set(message); latch.countDown(); } @Override public void onFragment(String fragment, boolean last) { fail(); } @Override public void onOpen(WebSocket websocket) {} @Override public void onClose(WebSocket websocket) { latch.countDown(); } @Override public void onError(Throwable t) { t.printStackTrace(); fail(t.getMessage()); } }) .sendTextMessage("echo"); try { latch.await(20, TimeUnit.SECONDS); } catch (InterruptedException e) { fail(e.getMessage()); } eventReceived.await(10, TimeUnit.SECONDS); assertNotNull(response.get()); assertEquals(response.get(), "echo"); assertTrue(onResume.get()); } catch (Exception e) { logger.error("test failed", e); fail(e.getMessage()); } c.close(); }
@Test(timeOut = 60000, enabled = true) public void testSuspendTimeout() { logger.info("{}: running test: testSuspendTimeout", getClass().getSimpleName()); final CountDownLatch latch = new CountDownLatch(1); atmoServlet .framework() .addAtmosphereHandler( ROOT, new AbstractHttpAtmosphereHandler() { public void onRequest(AtmosphereResource event) throws IOException { event.suspend(5000); } public void onStateChange(AtmosphereResourceEvent event) throws IOException { event.getResource().getResponse().getOutputStream().write("resume".getBytes()); } }, BroadcasterFactory.getDefault().get(DefaultBroadcaster.class, "suspend")); AsyncHttpClient c = new AsyncHttpClient(); try { final AtomicReference<String> response = new AtomicReference<String>(null); c.prepareGet(urlTarget) .execute(new WebSocketUpgradeHandler.Builder().build()) .get() .addWebSocketListener( new WebSocketTextListener() { @Override public void onMessage(String message) { // There is a regression in Jetty or AHC as we are getting some junk . if (response.get() == null) { response.set(message); } } @Override public void onFragment(String fragment, boolean last) { fail(); } @Override public void onOpen(WebSocket websocket) {} @Override public void onClose(WebSocket websocket) { latch.countDown(); } @Override public void onError(Throwable t) { t.printStackTrace(); fail(t.getMessage()); } }); try { latch.await(20, TimeUnit.SECONDS); } catch (InterruptedException e) { fail(e.getMessage()); } assertNotNull(response.get()); assertEquals(response.get(), "resume"); } catch (Exception e) { logger.error("test failed", e); fail(e.getMessage()); } c.close(); }
public void close() { websocket.close(); client.close(); }
@Override public void cleanup() { if (!asyncHttpClient.isClosed()) { asyncHttpClient.close(); } }
public void close() { httpClient.close(); }
public void close() { closed.set(true); httpClient.close(); }
public void stop() { Logger.trace("Releasing http client connections..."); httpClient.close(); }
@After public void after() { db.deleteDb(); httpClient.close(); }