use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpClientTest method testRequestWithResponseContentChunked.
private void testRequestWithResponseContentChunked(int length) throws Exception {
final byte[] chunk1 = new byte[length];
final byte[] chunk2 = new byte[length];
Random random = new Random();
random.nextBytes(chunk1);
random.nextBytes(chunk2);
byte[] bytes = new byte[chunk1.length + chunk2.length];
System.arraycopy(chunk1, 0, bytes, 0, chunk1.length);
System.arraycopy(chunk2, 0, bytes, chunk1.length, chunk2.length);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ServletOutputStream output = response.getOutputStream();
output.write(chunk1);
output.flush();
output.write(chunk2);
}
});
org.eclipse.jetty.client.api.Request request = client.newRequest(newURI());
FutureResponseListener listener = new FutureResponseListener(request, 2 * length);
request.timeout(10, TimeUnit.SECONDS).send(listener);
ContentResponse response = listener.get();
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(bytes, response.getContent());
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpClientTest method testContentDelimitedByEOFWithSlowRequest.
private void testContentDelimitedByEOFWithSlowRequest(final HttpVersion version, int length) throws Exception {
// This test is crafted in a way that the response completes before the request is fully written.
// With SSL, the response coming down will close the SSLEngine so it would not be possible to
// write the last chunk of the request content, and the request will be failed, failing also the
// test, which is not what we want.
// This is a limit of Java's SSL implementation that does not allow half closes.
Assume.assumeTrue(sslContextFactory == null);
final byte[] data = new byte[length];
new Random().nextBytes(data);
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
// Send Connection: close to avoid that the server chunks the content with HTTP 1.1.
if (version.compareTo(HttpVersion.HTTP_1_0) > 0)
response.setHeader("Connection", "close");
response.getOutputStream().write(data);
}
});
DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(new byte[] { 0 }));
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).version(version).content(content);
FutureResponseListener listener = new FutureResponseListener(request);
request.send(listener);
// Wait some time to simulate a slow request.
Thread.sleep(1000);
content.close();
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(data, response.getContent());
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpClientChunkedContentTest method test_Server_ContentTerminal_Client_ContentDelay.
@Test
public void test_Server_ContentTerminal_Client_ContentDelay() throws Exception {
startClient();
try (ServerSocket server = new ServerSocket()) {
server.bind(new InetSocketAddress("localhost", 0));
final AtomicReference<Callback> callbackRef = new AtomicReference<>();
final CountDownLatch firstContentLatch = new CountDownLatch(1);
final AtomicReference<Result> resultRef = new AtomicReference<>();
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", server.getLocalPort()).onResponseContentAsync(new Response.AsyncContentListener() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
if (callbackRef.compareAndSet(null, callback))
firstContentLatch.countDown();
else
callback.succeeded();
}
}).timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
resultRef.set(result);
completeLatch.countDown();
}
});
try (Socket socket = server.accept()) {
consumeRequestHeaders(socket);
OutputStream output = socket.getOutputStream();
String response = "" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "8\r\n" + "01234567\r\n" + "0\r\n" + "\r\n";
output.write(response.getBytes(StandardCharsets.UTF_8));
output.flush();
// Simulate a delay in consuming the content.
assertTrue(firstContentLatch.await(5, TimeUnit.SECONDS));
Thread.sleep(1000);
callbackRef.get().succeeded();
// Wait for the client to read 0 and become idle.
Thread.sleep(1000);
assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
Result result = resultRef.get();
assertTrue(result.isSucceeded());
Assert.assertEquals(200, result.getResponse().getStatus());
// Issue another request to be sure the connection is sane.
Request request = client.newRequest("localhost", server.getLocalPort()).timeout(5, TimeUnit.SECONDS);
FutureResponseListener listener = new FutureResponseListener(request);
request.send(listener);
consumeRequestHeaders(socket);
output.write(response.getBytes(StandardCharsets.UTF_8));
output.flush();
Assert.assertEquals(200, listener.get(5, TimeUnit.SECONDS).getStatus());
}
}
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpClientExplicitConnectionTest method testExplicitConnectionResponseListeners.
@Test
public void testExplicitConnectionResponseListeners() throws Exception {
start(new EmptyServerHandler());
Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
FuturePromise<Connection> futureConnection = new FuturePromise<>();
destination.newConnection(futureConnection);
Connection connection = futureConnection.get(5, TimeUnit.SECONDS);
CountDownLatch responseLatch = new CountDownLatch(1);
Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).onResponseSuccess(response -> responseLatch.countDown());
FutureResponseListener listener = new FutureResponseListener(request);
connection.send(request, listener);
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpClientTest method testRequestWithResponseContent.
private void testRequestWithResponseContent(int length) throws Exception {
final byte[] bytes = new byte[length];
new Random().nextBytes(bytes);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.setContentLength(length);
response.getOutputStream().write(bytes);
}
});
org.eclipse.jetty.client.api.Request request = client.newRequest(newURI());
FutureResponseListener listener = new FutureResponseListener(request, length);
request.timeout(10, TimeUnit.SECONDS).send(listener);
ContentResponse response = listener.get();
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(bytes, response.getContent());
}
Aggregations