|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2012 Sonatype, Inc. 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 http://www.apache.org/licenses/LICENSE-2.0. |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, |
| 9 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 10 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 12 | + */ |
| 13 | +package org.asynchttpclient.request.body; |
| 14 | + |
| 15 | +import io.github.artsok.RepeatedIfExceptionsTest; |
| 16 | +import io.netty.buffer.ByteBuf; |
| 17 | +import io.netty.buffer.Unpooled; |
| 18 | +import jakarta.servlet.http.HttpServletRequest; |
| 19 | +import jakarta.servlet.http.HttpServletResponse; |
| 20 | +import org.asynchttpclient.AbstractBasicTest; |
| 21 | +import org.asynchttpclient.AsyncHttpClient; |
| 22 | +import org.asynchttpclient.Response; |
| 23 | +import org.eclipse.jetty.server.Request; |
| 24 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.nio.charset.Charset; |
| 28 | +import java.time.Duration; |
| 29 | +import java.util.Arrays; |
| 30 | + |
| 31 | +import static org.asynchttpclient.Dsl.asyncHttpClient; |
| 32 | +import static org.asynchttpclient.Dsl.config; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | + |
| 35 | +public class PutByteBufTest extends AbstractBasicTest { |
| 36 | + |
| 37 | + private void put(String message) throws Exception { |
| 38 | + ByteBuf byteBuf = Unpooled.wrappedBuffer(message.getBytes()); |
| 39 | + try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(Duration.ofSeconds(2)))) { |
| 40 | + Response response = client.preparePut(getTargetUrl()).setBody(byteBuf).execute().get(); |
| 41 | + assertEquals(response.getStatusCode(), 200); |
| 42 | + assertEquals(response.getResponseBody(), message); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 47 | + public void testPutSmallBody() throws Exception { |
| 48 | + put("Hello Test"); |
| 49 | + } |
| 50 | + |
| 51 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 52 | + public void testPutBigBody() throws Exception { |
| 53 | + byte[] array = new byte[2048]; |
| 54 | + Arrays.fill(array, (byte) 97); |
| 55 | + String longString = new String(array, Charset.forName("UTF-8")); |
| 56 | + |
| 57 | + put(longString); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public AbstractHandler configureHandler() throws Exception { |
| 62 | + return new AbstractHandler() { |
| 63 | + |
| 64 | + @Override |
| 65 | + public void handle(String s, Request request, HttpServletRequest httpRequest, HttpServletResponse response) throws IOException { |
| 66 | + int size = 1024; |
| 67 | + if (request.getContentLength() > 0) { |
| 68 | + size = request.getContentLength(); |
| 69 | + } |
| 70 | + byte[] bytes = new byte[size]; |
| 71 | + if (bytes.length > 0) { |
| 72 | + final int read = request.getInputStream().read(bytes); |
| 73 | + response.getOutputStream().write(bytes, 0, read); |
| 74 | + } |
| 75 | + |
| 76 | + response.setStatus(200); |
| 77 | + response.getOutputStream().flush(); |
| 78 | + } |
| 79 | + }; |
| 80 | + } |
| 81 | +} |
0 commit comments