@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 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 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 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); } } }
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()); } } }
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); } }
@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); } } }
@Test public void postSingleStringPart() throws Throwable { withClient().run(client -> withServer(server).run(server -> { server.enqueueEcho(); client.preparePost(getTargetUrl()) .addBodyPart(new StringPart("foo", "bar")) .execute(new AsyncCompletionHandlerAdapter() { @Override public Response onCompleted(Response response) { String requestContentType = response.getHeader("X-" + CONTENT_TYPE); String boundary = requestContentType.substring((requestContentType.indexOf("boundary") + "boundary".length() + 1)); assertTrue(response.getResponseBody().regionMatches(false, "--".length(), boundary, 0, boundary.length())); return response; } }).get(TIMEOUT, SECONDS); })); }
public ArticleAttachments createUploadArticle(long articleId, File file, boolean inline) throws IOException { BoundRequestBuilder builder = client.preparePost(tmpl("/help_center/articles/{id}/attachments.json").set("id", articleId).toString()); if (realm != null) { builder.setRealm(realm); } else { builder.addHeader("Authorization", "Bearer " + oauthToken); } builder.setHeader("Content-Type", "multipart/form-data"); if (inline) builder.addBodyPart(new StringPart("inline", "true")); builder.addBodyPart( new FilePart("file", file, "application/octet-stream", Charset.forName("UTF-8"), file.getName())); final Request req = builder.build(); return complete(submit(req, handle(ArticleAttachments.class, "article_attachment"))); }