use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class AsyncRequestContentTest method testEmptyDeferredContent.
@Test
public void testEmptyDeferredContent() throws Exception {
start(new ConsumeInputHandler());
DeferredContentProvider contentProvider = new DeferredContentProvider();
CountDownLatch latch = new CountDownLatch(1);
client.POST(newURI()).content(contentProvider).send(result -> {
if (result.isSucceeded() && result.getResponse().getStatus() == HttpStatus.OK_200)
latch.countDown();
});
contentProvider.close();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithDeferredContentProviderFailsMultipleOffers.
@Test
public void testUploadWithDeferredContentProviderFailsMultipleOffers() throws Exception {
start(new EmptyServerHandler());
final CountDownLatch failLatch = new CountDownLatch(2);
final Callback callback = new Callback() {
@Override
public void failed(Throwable x) {
failLatch.countDown();
}
};
final CountDownLatch completeLatch = new CountDownLatch(1);
final DeferredContentProvider content = new DeferredContentProvider();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).onRequestBegin(request -> {
content.offer(ByteBuffer.wrap(new byte[256]), callback);
content.offer(ByteBuffer.wrap(new byte[256]), callback);
request.abort(new Exception("explicitly_thrown_by_test"));
}).send(result -> {
if (result.isFailed())
completeLatch.countDown();
});
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(failLatch.await(5, TimeUnit.SECONDS));
// Make sure that adding more content results in the callback to be failed.
final CountDownLatch latch = new CountDownLatch(1);
content.offer(ByteBuffer.wrap(new byte[128]), new Callback() {
@Override
public void failed(Throwable x) {
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithDeferredContentProviderFromInputStream.
@Slow
@Test
public void testUploadWithDeferredContentProviderFromInputStream() throws Exception {
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);
IO.copy(request.getInputStream(), new ByteArrayOutputStream());
}
});
final CountDownLatch latch = new CountDownLatch(1);
try (DeferredContentProvider content = new DeferredContentProvider()) {
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(result -> {
if (result.isSucceeded() && result.getResponse().getStatus() == 200)
latch.countDown();
});
// Make sure we provide the content *after* the request has been "sent".
Thread.sleep(1000);
try (ByteArrayInputStream input = new ByteArrayInputStream(new byte[1024])) {
byte[] buffer = new byte[200];
int read;
while ((read = input.read(buffer)) >= 0) content.offer(ByteBuffer.wrap(buffer, 0, read));
}
}
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Expect100Continue_WithConcurrentDeferredContent_Respond100Continue.
@Test
public void test_Expect100Continue_WithConcurrentDeferredContent_Respond100Continue() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
// Send 100-Continue and echo the content
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
final byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
final DeferredContentProvider content = new DeferredContentProvider();
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).onRequestHeaders(request -> {
content.offer(ByteBuffer.wrap(data));
content.close();
}).content(content).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
Assert.assertArrayEquals(data, getContent());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Expect100Continue_WithDeferredContent_Respond100Continue.
@Slow
@Test
public void test_Expect100Continue_WithDeferredContent_Respond100Continue() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
// Send 100-Continue and echo the content
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
final byte[] chunk1 = new byte[] { 0, 1, 2, 3 };
final byte[] chunk2 = new byte[] { 4, 5, 6, 7 };
final byte[] data = new byte[chunk1.length + chunk2.length];
System.arraycopy(chunk1, 0, data, 0, chunk1.length);
System.arraycopy(chunk2, 0, data, chunk1.length, chunk2.length);
final CountDownLatch latch = new CountDownLatch(1);
DeferredContentProvider content = new DeferredContentProvider();
client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(content).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
Assert.assertArrayEquals(data, getContent());
latch.countDown();
}
});
Thread.sleep(1000);
content.offer(ByteBuffer.wrap(chunk1));
Thread.sleep(1000);
content.offer(ByteBuffer.wrap(chunk2));
content.close();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations