use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent_EarlyEOF.
@Test
public void test_Receive_ResponseContent_EarlyEOF() throws Exception {
String content1 = "0123456789";
String content2 = "ABCDEF";
endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + content1);
HttpExchange exchange = newExchange();
FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
connection.getHttpChannel().receive();
endPoint.addInputEOF();
connection.getHttpChannel().receive();
try {
listener.get(5, TimeUnit.SECONDS);
Assert.fail();
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof EOFException);
}
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent.
@Test
public void test_Receive_ResponseContent() throws Exception {
String content = "0123456789ABCDEF";
endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: " + content.length() + "\r\n" + "\r\n" + content);
HttpExchange exchange = newExchange();
FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
connection.getHttpChannel().receive();
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("OK", response.getReason());
Assert.assertSame(HttpVersion.HTTP_1_1, response.getVersion());
HttpFields headers = response.getHeaders();
Assert.assertNotNull(headers);
Assert.assertEquals(1, headers.size());
Assert.assertEquals(String.valueOf(content.length()), headers.get(HttpHeader.CONTENT_LENGTH));
String received = listener.getContentAsString(StandardCharsets.UTF_8);
Assert.assertEquals(content, received);
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent_IdleTimeout.
@Test
public void test_Receive_ResponseContent_IdleTimeout() throws Exception {
endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: 1\r\n" + "\r\n");
HttpExchange exchange = newExchange();
FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
connection.getHttpChannel().receive();
// ByteArrayEndPoint has an idle timeout of 0 by default,
// so to simulate an idle timeout is enough to wait a bit.
Thread.sleep(100);
connection.onIdleExpired();
try {
listener.get(5, TimeUnit.SECONDS);
Assert.fail();
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof TimeoutException);
}
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class Usage method testRequestWithExplicitConnectionControl.
@Test
public void testRequestWithExplicitConnectionControl() throws Exception {
HttpClient client = new HttpClient();
client.start();
// Create an explicit connection, and use try-with-resources to manage it
FuturePromise<Connection> futureConnection = new FuturePromise<>();
client.getDestination("http", "localhost", 8080).newConnection(futureConnection);
try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
Request request = client.newRequest("localhost", 8080);
// Asynchronous send but using FutureResponseListener
FutureResponseListener listener = new FutureResponseListener(request);
connection.send(request, listener);
// Wait for the response on the listener
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
}
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testProxyRequestHeadersNotSentUntilContent.
@Test
public void testProxyRequestHeadersNotSentUntilContent() throws Exception {
startServer(new EchoHttpServlet());
final CountDownLatch proxyRequestLatch = new CountDownLatch(1);
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new BufferingContentTransformer();
}
@Override
protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
proxyRequestLatch.countDown();
super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
}
});
startClient();
DeferredContentProvider content = new DeferredContentProvider();
Request request = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).content(content);
FutureResponseListener listener = new FutureResponseListener(request);
request.send(listener);
// Send one chunk of content, the proxy request must not be sent.
ByteBuffer chunk1 = ByteBuffer.allocate(1024);
content.offer(chunk1);
Assert.assertFalse(proxyRequestLatch.await(1, TimeUnit.SECONDS));
// Send another chunk of content, the proxy request must not be sent.
ByteBuffer chunk2 = ByteBuffer.allocate(512);
content.offer(chunk2);
Assert.assertFalse(proxyRequestLatch.await(1, TimeUnit.SECONDS));
// Finish the content, request must be sent.
content.close();
Assert.assertTrue(proxyRequestLatch.await(1, TimeUnit.SECONDS));
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals(chunk1.capacity() + chunk2.capacity(), response.getContent().length);
}
Aggregations