use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.
the class HttpClientRedirectTest method test_307_WithRequestContent.
@Test
public void test_307_WithRequestContent() throws Exception {
start(new RedirectHandler());
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).path("/307/localhost/done").content(new ByteBufferContentProvider(ByteBuffer.wrap(data))).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
Assert.assertArrayEquals(data, response.getContent());
}
use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.
the class HttpRequestAbortTest method testAbortOnContent.
@Test
public void testAbortOnContent() throws Exception {
try (StacklessLogging suppressor = new StacklessLogging(org.eclipse.jetty.server.HttpChannel.class)) {
CountDownLatch serverLatch = new CountDownLatch(1);
start(new EmptyServerHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
super.handle(target, baseRequest, request, response);
if (request.getDispatcherType() != DispatcherType.ERROR)
IO.copy(request.getInputStream(), response.getOutputStream());
} finally {
serverLatch.countDown();
}
}
});
final Throwable cause = new Exception();
final AtomicBoolean aborted = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
try {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestContent((request, content) -> {
aborted.set(request.abort(cause));
latch.countDown();
}).content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 })) {
@Override
public long getLength() {
return -1;
}
}).timeout(5, TimeUnit.SECONDS).send();
Assert.fail();
} catch (ExecutionException x) {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
if (aborted.get())
Assert.assertSame(cause, x.getCause());
}
Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
}
use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection.
@Test
public void test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
try (StacklessLogging stackless = new StacklessLogging(HttpConnection.class)) {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setHeader("Connection", "close");
baseRequest.setHandled(true);
// Don't read request content; this causes the server parser to be closed
}
});
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
Assert.assertEquals(0, idleConnections.size());
final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
Assert.assertEquals(0, activeConnections.size());
Log.getLogger(HttpConnection.class).info("Expecting java.lang.IllegalStateException: HttpParser{s=CLOSED,...");
final CountDownLatch latch = new CountDownLatch(1);
ByteBuffer buffer = ByteBuffer.allocate(16 * 1024 * 1024);
Arrays.fill(buffer.array(), (byte) 'x');
client.newRequest(host, port).scheme(scheme).content(new ByteBufferContentProvider(buffer)).send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
Assert.assertEquals(1, latch.getCount());
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
latch.countDown();
}
});
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
server.stop();
}
}
use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.
the class HttpRequestAbortTest method testAbortOnCommitWithContent.
@Test
public void testAbortOnCommitWithContent() throws Exception {
final AtomicReference<IOException> failure = new AtomicReference<>();
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
baseRequest.setHandled(true);
if (request.getDispatcherType() != DispatcherType.ERROR)
IO.copy(request.getInputStream(), response.getOutputStream());
} catch (IOException x) {
failure.set(x);
throw x;
}
}
});
final Throwable cause = new Exception();
final AtomicBoolean aborted = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
try {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestCommit(request -> {
aborted.set(request.abort(cause));
latch.countDown();
}).content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 })) {
@Override
public long getLength() {
return -1;
}
}).timeout(5, TimeUnit.SECONDS).send();
Assert.fail();
} catch (ExecutionException x) {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
if (aborted.get())
Assert.assertSame(cause, x.getCause());
}
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.
the class HttpSenderOverHTTPTest method test_Send_SmallRequestContent_Chunked_InTwoChunks.
@Test
public void test_Send_SmallRequestContent_Chunked_InTwoChunks() throws Exception {
ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
destination.start();
HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
Request request = client.newRequest(URI.create("http://localhost/"));
String content1 = "0123456789";
String content2 = "ABCDEF";
request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8))) {
@Override
public long getLength() {
return -1;
}
});
final CountDownLatch headersLatch = new CountDownLatch(1);
final CountDownLatch successLatch = new CountDownLatch(1);
request.listener(new Request.Listener.Adapter() {
@Override
public void onHeaders(Request request) {
headersLatch.countDown();
}
@Override
public void onSuccess(Request request) {
successLatch.countDown();
}
});
connection.send(request, null);
String requestString = endPoint.takeOutputString();
Assert.assertTrue(requestString.startsWith("GET "));
String content = Integer.toHexString(content1.length()).toUpperCase(Locale.ENGLISH) + "\r\n" + content1 + "\r\n";
content += Integer.toHexString(content2.length()).toUpperCase(Locale.ENGLISH) + "\r\n" + content2 + "\r\n";
content += "0\r\n\r\n";
Assert.assertTrue(requestString.endsWith("\r\n\r\n" + content));
Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
Aggregations