use of org.asynchttpclient.testserver.HttpServer.EchoHandler in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postUnboundedInputStreamAsBodyStream.
@Test
public void postUnboundedInputStreamAsBodyStream() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
server.enqueue(new AbstractHandler() {
EchoHandler chain = new EchoHandler();
@Override
public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
assertEquals(request.getHeader(TRANSFER_ENCODING.toString()), HttpHeaderValues.CHUNKED.toString());
assertNull(request.getHeader(CONTENT_LENGTH.toString()));
chain.handle(target, request, httpServletRequest, httpServletResponse);
}
});
server.enqueueEcho();
client.preparePost(getTargetUrl()).setHeaders(h).setBody(new ByteArrayInputStream("{}".getBytes(StandardCharsets.ISO_8859_1))).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getResponseBody(), "{}");
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
use of org.asynchttpclient.testserver.HttpServer.EchoHandler in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postInputStreamWithContentLengthAsBodyGenerator.
@Test
public void postInputStreamWithContentLengthAsBodyGenerator() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
server.enqueue(new AbstractHandler() {
EchoHandler chain = new EchoHandler();
@Override
public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
assertNull(request.getHeader(TRANSFER_ENCODING.toString()));
assertEquals(request.getHeader(CONTENT_LENGTH.toString()), Integer.toString("{}".getBytes(StandardCharsets.ISO_8859_1).length));
chain.handle(target, request, httpServletRequest, httpServletResponse);
}
});
byte[] bodyBytes = "{}".getBytes(StandardCharsets.ISO_8859_1);
InputStream bodyStream = new ByteArrayInputStream(bodyBytes);
client.preparePost(getTargetUrl()).setHeaders(h).setBody(new InputStreamBodyGenerator(bodyStream, bodyBytes.length)).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getResponseBody(), "{}");
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
Aggregations