@Test(groups = { "standalone", "default_provider" }) public void testAccumulateErrorBody() throws Throwable { try (SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setUrl(getTargetUrl() + "/nonexistent").setErrorDocumentBehaviour(ErrorDocumentBehaviour.ACCUMULATE).build()) { ByteArrayOutputStream o = new ByteArrayOutputStream(10); Future<Response> future = client.get(new OutputStreamBodyConsumer(o)); System.out.println("waiting for response"); Response response = future.get(); assertEquals(response.getStatusCode(), 404); assertEquals(o.toString(), ""); assertTrue(response.getResponseBody().startsWith("<html>")); } }
@Test(groups = { "standalone", "default_provider" }) public void testOmitErrorBody() throws Throwable { try (SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setUrl(getTargetUrl() + "/nonexistent").setErrorDocumentBehaviour(ErrorDocumentBehaviour.OMIT).build()) { ByteArrayOutputStream o = new ByteArrayOutputStream(10); Future<Response> future = client.get(new OutputStreamBodyConsumer(o)); System.out.println("waiting for response"); Response response = future.get(); assertEquals(response.getStatusCode(), 404); assertEquals(o.toString(), ""); assertEquals(response.getResponseBody(), ""); } }
@Test(groups = { "standalone", "default_provider" }) public void testCloseDerivedValidMaster() throws Exception { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl()).build(); try { SimpleAsyncHttpClient derived = client.derive().build(); derived.get().get(); derived.close(); Response response = client.get().get(); assertEquals(response.getStatusCode(), 200); } finally { client.close(); } }
@Test(groups = { "standalone", "default_provider" }) public void testCloseMasterInvalidDerived() throws Exception { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl()).build(); SimpleAsyncHttpClient derived = client.derive().build(); client.close(); try { derived.get().get(); fail("Expected closed AHC"); // expected -- Seems to me that this behavior conflicts with the requirements of Future.get() } catch (ExecutionException e) { assertTrue(e.getCause() instanceof IOException); } finally { client.close(); derived.close(); } }
@Test(groups = { "online", "default_provider" }) public void testSimpleAHCConfigProxy() throws IOException, InterruptedException, ExecutionException, TimeoutException { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder()// .setProxyProtocol(ProxyServer.Protocol.HTTPS)// .setProxyHost("127.0.0.1")// .setProxyPort(port1)// .setFollowRedirects(true)// .setUrl(getTargetUrl2())// .setAcceptAnyCertificate(true)// .setHeader("Content-Type", "text/html").build(); try { Response r = client.get().get(); assertEquals(r.getStatusCode(), 200); assertEquals(r.getHeader("X-Connection"), "keep-alive"); } finally { client.close(); } }