|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 AsyncHttpClient Project. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, |
| 10 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 11 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 13 | + */ |
| 14 | +package org.asynchttpclient.request.body; |
| 15 | + |
| 16 | +import org.asynchttpclient.AbstractBasicTest; |
| 17 | +import org.asynchttpclient.AsyncHttpClient; |
| 18 | +import org.asynchttpclient.Response; |
| 19 | +import org.asynchttpclient.request.body.multipart.InputStreamPart; |
| 20 | +import org.eclipse.jetty.server.Request; |
| 21 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 22 | +import org.testng.annotations.Test; |
| 23 | + |
| 24 | +import javax.servlet.ServletInputStream; |
| 25 | +import javax.servlet.http.HttpServletRequest; |
| 26 | +import javax.servlet.http.HttpServletResponse; |
| 27 | +import java.io.*; |
| 28 | + |
| 29 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 30 | +import static org.asynchttpclient.Dsl.asyncHttpClient; |
| 31 | +import static org.asynchttpclient.Dsl.config; |
| 32 | +import static org.asynchttpclient.test.TestUtils.LARGE_IMAGE_FILE; |
| 33 | +import static org.asynchttpclient.test.TestUtils.createTempFile; |
| 34 | +import static org.testng.Assert.assertEquals; |
| 35 | + |
| 36 | +public class InputStreamPartLargeFileTest extends AbstractBasicTest { |
| 37 | + |
| 38 | + @Override |
| 39 | + public AbstractHandler configureHandler() throws Exception { |
| 40 | + return new AbstractHandler() { |
| 41 | + |
| 42 | + public void handle(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 43 | + |
| 44 | + ServletInputStream in = req.getInputStream(); |
| 45 | + byte[] b = new byte[8192]; |
| 46 | + |
| 47 | + int count; |
| 48 | + int total = 0; |
| 49 | + while ((count = in.read(b)) != -1) { |
| 50 | + b = new byte[8192]; |
| 51 | + total += count; |
| 52 | + } |
| 53 | + resp.setStatus(200); |
| 54 | + resp.addHeader("X-TRANSFERRED", String.valueOf(total)); |
| 55 | + resp.getOutputStream().flush(); |
| 56 | + resp.getOutputStream().close(); |
| 57 | + |
| 58 | + baseRequest.setHandled(true); |
| 59 | + } |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testPutImageFile() throws Exception { |
| 65 | + try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { |
| 66 | + InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)); |
| 67 | + 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(); |
| 68 | + assertEquals(response.getStatusCode(), 200); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testPutImageFileUnknownSize() throws Exception { |
| 74 | + try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { |
| 75 | + InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)); |
| 76 | + Response response = client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, LARGE_IMAGE_FILE.getName(), -1, "application/octet-stream", UTF_8)).execute().get(); |
| 77 | + assertEquals(response.getStatusCode(), 200); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testPutLargeTextFile() throws Exception { |
| 83 | + File file = createTempFile(1024 * 1024); |
| 84 | + InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); |
| 85 | + |
| 86 | + try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { |
| 87 | + Response response = client.preparePut(getTargetUrl()) |
| 88 | + .addBodyPart(new InputStreamPart("test", inputStream, file.getName(), file.length(), "application/octet-stream", UTF_8)).execute().get(); |
| 89 | + assertEquals(response.getStatusCode(), 200); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testPutLargeTextFileUnknownSize() throws Exception { |
| 95 | + File file = createTempFile(1024 * 1024); |
| 96 | + InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); |
| 97 | + |
| 98 | + try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { |
| 99 | + Response response = client.preparePut(getTargetUrl()) |
| 100 | + .addBodyPart(new InputStreamPart("test", inputStream, file.getName(), -1, "application/octet-stream", UTF_8)).execute().get(); |
| 101 | + assertEquals(response.getStatusCode(), 200); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments