@Test public void zeroCopyPutTest() throws IOException, ExecutionException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePut("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(); Response resp = f.get(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@Test public void Expect100Continue() throws Exception { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePut("http://localhost:" + port1 + "/") .setHeader(EXPECT, HttpHeaderValues.CONTINUE) .setBody(SIMPLE_TEXT_FILE) .execute(); Response resp = f.get(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@Test public void testPutImageFile() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)); Response response = client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, LARGE_IMAGE_FILE.getName(), LARGE_IMAGE_FILE.length(), "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } }
@Test public void testPutEmptyBody() throws Exception { try (AsyncHttpClient ahc = asyncHttpClient()) { Response response = ahc.preparePut(getTargetUrl()).setBody("String").execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 204); assertEquals(response.getResponseBody(), ""); assertNotNull(response.getResponseBodyAsStream()); assertEquals(response.getResponseBodyAsStream().read(), -1); } }
@Test public void basicByteBufferTest() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { File largeFile = createTempFile(1024 * 100 * 10); final AtomicInteger byteReceived = new AtomicInteger(); Response response = c.preparePut(getTargetUrl()).setBody(largeFile).execute(new AsyncCompletionHandlerAdapter() { @Override public State onBodyPartReceived(final HttpResponseBodyPart content) throws Exception { byteReceived.addAndGet(content.getBodyByteBuffer().capacity()); return super.onBodyPartReceived(content); } }).get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(byteReceived.get(), largeFile.length()); assertEquals(response.getResponseBody().length(), largeFile.length()); } }
@Test public void testPutLargeTextFile() throws Exception { File file = createTempFile(1024 * 1024); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()) .addBodyPart(new InputStreamPart("test", inputStream, file.getName(), file.length(), "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } }
@Test public void testPutImageFileUnknownSize() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)); Response response = client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, LARGE_IMAGE_FILE.getName(), -1, "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } }
@Test public void testPutLargeTextFileUnknownSize() throws Exception { File file = createTempFile(1024 * 1024); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()) .addBodyPart(new InputStreamPart("test", inputStream, file.getName(), -1, "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } } }
@Test public void putFormParametersAsBodyStream() throws Throwable { withClient().run(client -> withServer(server).run(server -> { HttpHeaders h = new DefaultHttpHeaders(); h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 5; i++) { sb.append("param_").append(i).append("=value_").append(i).append("&"); } sb.setLength(sb.length() - 1); ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes()); server.enqueueEcho(); client.preparePut(getTargetUrl()) .setHeaders(h) .setBody(is) .execute(new AsyncCompletionHandlerAdapter() { @Override public Response onCompleted(Response response) { assertEquals(response.getStatusCode(), 200); for (int i = 1; i < 5; i++) { assertEquals(response.getHeader("X-param_" + i), "value_" + i); } return response; } }).get(TIMEOUT, SECONDS); })); }
Response response = client.preparePut(getTargetUrl()).setBody(new FileBodyGenerator(file)).execute(tl).get();
Response response = client.preparePut(getTargetUrl()).setBody(file).execute(tl).get();
private void expectSuccess(Function<BoundRequestBuilder, BoundRequestBuilder> f) throws Exception { File file = createTempFile(1024 * 1024); try (AsyncHttpClient client = asyncHttpClient()) { for (int i = 0; i < 20; i++) { Response response = f.apply(client.preparePut(getTargetUrl()) .addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8))) .execute().get(); assertEquals(response.getStatusCode(), 200); assertEquals(response.getResponseBodyAsBytes().length, Integer.valueOf(response.getHeader("X-" + CONTENT_LENGTH)).intValue()); } } }
@Test public void testStreamingPutImage() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()).setBody(createAsyncPublisher(LARGE_IMAGE_BYTES, 2342)) .execute().get(); assertEquals(response.getStatusCode(), 200); assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES); } }
@Test public void testAsyncStreamingPutImage() throws Exception { // test that streaming works with a publisher that does not invoke onSubscription synchronously from subscribe try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()).setBody(createPublisher(LARGE_IMAGE_BYTES, 2342)) .execute().get(); assertEquals(response.getStatusCode(), 200); assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES); } }
private void expectExecutionException(Function<BoundRequestBuilder, BoundRequestBuilder> f) throws Throwable { File file = createTempFile(1024 * 1024); ExecutionException executionException = null; try (AsyncHttpClient client = asyncHttpClient()) { try { for (int i = 0; i < 20; i++) { f.apply(client.preparePut(getTargetUrl()) .addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8))) .execute() .get(); } } catch (ExecutionException e) { executionException = e; } } assertNotNull(executionException, "Expected ExecutionException"); throw executionException.getCause(); }
@Test public void testPutImageFile() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", LARGE_IMAGE_FILE, "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } }
BoundRequestBuilder requestBuilder = client.preparePut(getTargetUrl()) .setBody(createPublisher(LARGE_IMAGE_BYTES, 1000)) .setHeader("X-" + CONTENT_LENGTH, LARGE_IMAGE_BYTES.length)
@Test public void testPutLargeTextFile() throws Exception { File file = createTempFile(1024 * 1024); try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } } }
private void put(int fileSize) throws Exception { File file = createTempFile(fileSize); try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(2000))) { Response response = client.preparePut(getTargetUrl()).setBody(file).execute().get(); assertEquals(response.getStatusCode(), 200); } }
@Test(expectedExceptions = ExecutionException.class) public void testFailingStream() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Publisher<ByteBuf> failingPublisher = Flowable.error(new FailedStream()); client.preparePut(getTargetUrl()).setBody(failingPublisher).execute().get(); } }