use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testProxyRequestHeadersNotSentUntilFirstContent.
@Test
public void testProxyRequestHeadersNotSentUntilFirstContent() throws Exception {
startServer(new EchoHttpServlet());
final CountDownLatch proxyRequestLatch = new CountDownLatch(1);
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new ContentTransformer() {
private ByteBuffer buffer;
@Override
public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
// Buffer only the first chunk.
if (buffer == null) {
buffer = ByteBuffer.allocate(input.remaining());
buffer.put(input).flip();
} else if (buffer.hasRemaining()) {
output.add(buffer);
output.add(input);
} else {
output.add(input);
}
}
};
}
@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 be sent.
ByteBuffer chunk2 = ByteBuffer.allocate(512);
content.offer(chunk2);
Assert.assertTrue(proxyRequestLatch.await(5, TimeUnit.SECONDS));
// Finish the content.
content.close();
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals(chunk1.capacity() + chunk2.capacity(), response.getContent().length);
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class SslBytesClientTest method testHandshake.
@Test
public void testHandshake() throws Exception {
Request request = client.newRequest("localhost", proxy.getPort());
FutureResponseListener listener = new FutureResponseListener(request);
request.scheme(HttpScheme.HTTPS.asString()).send(listener);
Assert.assertTrue(proxy.awaitClient(5, TimeUnit.SECONDS));
final SSLSocket server = (SSLSocket) acceptor.accept();
server.setUseClientMode(false);
Future<Object> handshake = threadPool.submit(() -> {
server.startHandshake();
return null;
});
// Client Hello
TLSRecord record = proxy.readFromClient();
Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
proxy.flushToServer(record);
// Server Hello + Certificate + Server Done
record = proxy.readFromServer();
Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
proxy.flushToClient(record);
// Client Key Exchange
record = proxy.readFromClient();
Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
proxy.flushToServer(record);
// Change Cipher Spec
record = proxy.readFromClient();
Assert.assertEquals(TLSRecord.Type.CHANGE_CIPHER_SPEC, record.getType());
proxy.flushToServer(record);
// Client Done
record = proxy.readFromClient();
Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
proxy.flushToServer(record);
// Change Cipher Spec
record = proxy.readFromServer();
Assert.assertEquals(TLSRecord.Type.CHANGE_CIPHER_SPEC, record.getType());
proxy.flushToClient(record);
// Server Done
record = proxy.readFromServer();
Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
proxy.flushToClient(record);
Assert.assertNull(handshake.get(5, TimeUnit.SECONDS));
SimpleProxy.AutomaticFlow automaticProxyFlow = proxy.startAutomaticFlow();
// Read request
BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream(), StandardCharsets.UTF_8));
String line = reader.readLine();
Assert.assertTrue(line.startsWith("GET"));
while (line.length() > 0) line = reader.readLine();
// Write response
OutputStream output = server.getOutputStream();
output.write(("HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n").getBytes(StandardCharsets.UTF_8));
output.flush();
Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS));
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
server.close();
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpReceiverOverHTTPTest method test_Receive_NoResponseContent.
@Test
public void test_Receive_NoResponseContent() throws Exception {
endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: 0\r\n" + "\r\n");
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("0", headers.get(HttpHeader.CONTENT_LENGTH));
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpReceiverOverHTTPTest method test_FillInterested_RacingWith_BufferRelease.
@Test
public void test_FillInterested_RacingWith_BufferRelease() throws Exception {
connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<>()) {
@Override
protected HttpChannelOverHTTP newHttpChannel() {
return new HttpChannelOverHTTP(this) {
@Override
protected HttpReceiverOverHTTP newHttpReceiver() {
return new HttpReceiverOverHTTP(this) {
@Override
protected void fillInterested() {
// Verify that the buffer has been released
// before fillInterested() is called.
Assert.assertNull(getResponseBuffer());
// Fill the endpoint so receive is called again.
endPoint.addInput("X");
super.fillInterested();
}
};
}
};
}
};
endPoint.setConnection(connection);
// Partial response to trigger the call to fillInterested().
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();
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 HttpReceiverOverHTTPTest method test_Receive_BadResponse.
@Test
public void test_Receive_BadResponse() throws Exception {
endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: A\r\n" + "\r\n");
HttpExchange exchange = newExchange();
FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
connection.getHttpChannel().receive();
try {
listener.get(5, TimeUnit.SECONDS);
Assert.fail();
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof HttpResponseException);
}
}
Aggregations