|
25 | 25 | import io.netty.handler.codec.http.HttpHeaders;
|
26 | 26 |
|
27 | 27 | import java.io.ByteArrayInputStream;
|
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
28 | 30 | import java.net.ConnectException;
|
29 | 31 | import java.net.UnknownHostException;
|
30 | 32 | import java.nio.charset.StandardCharsets;
|
|
42 | 44 | import java.util.concurrent.atomic.AtomicReference;
|
43 | 45 |
|
44 | 46 | import javax.net.ssl.SSLException;
|
| 47 | +import javax.servlet.ServletException; |
| 48 | +import javax.servlet.http.HttpServletRequest; |
| 49 | +import javax.servlet.http.HttpServletResponse; |
45 | 50 |
|
46 | 51 | import org.asynchttpclient.cookie.Cookie;
|
47 | 52 | import org.asynchttpclient.handler.MaxRedirectException;
|
| 53 | +import org.asynchttpclient.request.body.generator.InputStreamBodyGenerator; |
48 | 54 | import org.asynchttpclient.request.body.multipart.StringPart;
|
49 | 55 | import org.asynchttpclient.test.EventCollectingHandler;
|
50 | 56 | import org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter;
|
51 | 57 | import org.asynchttpclient.testserver.HttpServer;
|
| 58 | +import org.asynchttpclient.testserver.HttpServer.EchoHandler; |
52 | 59 | import org.asynchttpclient.testserver.HttpTest;
|
| 60 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
53 | 61 | import org.testng.annotations.AfterClass;
|
54 | 62 | import org.testng.annotations.BeforeClass;
|
55 | 63 | import org.testng.annotations.Test;
|
@@ -911,4 +919,76 @@ public void requestingPlainHttpEndpointOverHttpsThrowsSslException() throws Thro
|
911 | 919 | });
|
912 | 920 | });
|
913 | 921 | }
|
| 922 | + |
| 923 | + @Test |
| 924 | + public void postUnboundedInputStreamAsBodyStream() throws Throwable { |
| 925 | + withClient().run(client -> { |
| 926 | + withServer(server).run(server -> { |
| 927 | + HttpHeaders h = new DefaultHttpHeaders(); |
| 928 | + h.add(CONTENT_TYPE, APPLICATION_JSON); |
| 929 | + server.enqueue(new AbstractHandler() { |
| 930 | + EchoHandler chain = new EchoHandler(); |
| 931 | + @Override |
| 932 | + public void handle(String target, org.eclipse.jetty.server.Request request, |
| 933 | + HttpServletRequest httpServletRequest, |
| 934 | + HttpServletResponse httpServletResponse) throws IOException, ServletException { |
| 935 | + assertEquals(request.getHeader(TRANSFER_ENCODING), CHUNKED); |
| 936 | + assertNull(request.getHeader(CONTENT_LENGTH)); |
| 937 | + chain.handle(target, request, httpServletRequest, httpServletResponse); |
| 938 | + } |
| 939 | + }); |
| 940 | + server.enqueueEcho(); |
| 941 | + |
| 942 | + client.preparePost(getTargetUrl())// |
| 943 | + .setHeaders(h)// |
| 944 | + .setBody(new ByteArrayInputStream("{}".getBytes(StandardCharsets.ISO_8859_1)))// |
| 945 | + .execute(new AsyncCompletionHandlerAdapter() { |
| 946 | + @Override |
| 947 | + public Response onCompleted(Response response) throws Exception { |
| 948 | + assertEquals(response.getStatusCode(), 200); |
| 949 | + assertEquals(response.getResponseBody(), "{}"); |
| 950 | + return response; |
| 951 | + } |
| 952 | + }).get(TIMEOUT, SECONDS); |
| 953 | + }); |
| 954 | + }); |
| 955 | + } |
| 956 | + |
| 957 | + @Test |
| 958 | + public void postInputStreamWithContentLengthAsBodyGenerator() throws Throwable { |
| 959 | + withClient().run(client -> { |
| 960 | + withServer(server).run(server -> { |
| 961 | + HttpHeaders h = new DefaultHttpHeaders(); |
| 962 | + h.add(CONTENT_TYPE, APPLICATION_JSON); |
| 963 | + server.enqueue(new AbstractHandler() { |
| 964 | + EchoHandler chain = new EchoHandler(); |
| 965 | + @Override |
| 966 | + public void handle(String target, org.eclipse.jetty.server.Request request, |
| 967 | + HttpServletRequest httpServletRequest, |
| 968 | + HttpServletResponse httpServletResponse) throws IOException, ServletException { |
| 969 | + assertNull(request.getHeader(TRANSFER_ENCODING)); |
| 970 | + assertEquals(request.getHeader(CONTENT_LENGTH),// |
| 971 | + Integer.toString("{}".getBytes(StandardCharsets.ISO_8859_1).length)); |
| 972 | + chain.handle(target, request, httpServletRequest, httpServletResponse); |
| 973 | + } |
| 974 | + }); |
| 975 | + |
| 976 | + byte[] bodyBytes = "{}".getBytes(StandardCharsets.ISO_8859_1); |
| 977 | + InputStream bodyStream = new ByteArrayInputStream(bodyBytes); |
| 978 | + |
| 979 | + client.preparePost(getTargetUrl())// |
| 980 | + .setHeaders(h)// |
| 981 | + .setBody(new InputStreamBodyGenerator(bodyStream, bodyBytes.length))// |
| 982 | + .execute(new AsyncCompletionHandlerAdapter() { |
| 983 | + |
| 984 | + @Override |
| 985 | + public Response onCompleted(Response response) throws Exception { |
| 986 | + assertEquals(response.getStatusCode(), 200); |
| 987 | + assertEquals(response.getResponseBody(), "{}"); |
| 988 | + return response; |
| 989 | + } |
| 990 | + }).get(TIMEOUT, SECONDS); |
| 991 | + }); |
| 992 | + }); |
| 993 | + } |
914 | 994 | }
|
0 commit comments