use of org.eclipse.jetty.client.util.OutputStreamContentProvider in project jetty.project by eclipse.
the class AsyncRequestContentTest method testEmptyOutputStream.
@Test
public void testEmptyOutputStream() throws Exception {
start(new ConsumeInputHandler());
OutputStreamContentProvider contentProvider = new OutputStreamContentProvider();
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.OutputStreamContentProvider in project jetty.project by eclipse.
the class AsyncRequestContentTest method testOutputStream.
@Test
public void testOutputStream() throws Exception {
start(new ConsumeInputHandler());
OutputStreamContentProvider contentProvider = new OutputStreamContentProvider();
CountDownLatch latch = new CountDownLatch(1);
client.POST(newURI()).content(contentProvider).send(result -> {
if (result.isSucceeded() && result.getResponse().getStatus() == HttpStatus.OK_200)
latch.countDown();
});
OutputStream output = contentProvider.getOutputStream();
output.write(new byte[1]);
output.flush();
contentProvider.close();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.OutputStreamContentProvider in project jetty.project by eclipse.
the class HttpClientStreamTest method testBigUploadWithOutputStreamFromInputStream.
@Test
public void testBigUploadWithOutputStreamFromInputStream() 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(), response.getOutputStream());
}
});
final byte[] data = new byte[16 * 1024 * 1024];
new Random().nextBytes(data);
final CountDownLatch latch = new CountDownLatch(1);
OutputStreamContentProvider content = new OutputStreamContentProvider();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(new BufferingResponseListener(data.length) {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isSucceeded());
Assert.assertEquals(200, result.getResponse().getStatus());
Assert.assertArrayEquals(data, getContent());
latch.countDown();
}
});
// Make sure we provide the content *after* the request has been "sent".
Thread.sleep(1000);
try (InputStream input = new ByteArrayInputStream(data);
OutputStream output = content.getOutputStream()) {
byte[] buffer = new byte[1024];
while (true) {
int read = input.read(buffer);
if (read < 0)
break;
output.write(buffer, 0, read);
}
}
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.OutputStreamContentProvider in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithOutputStream.
@Test
public void testUploadWithOutputStream() 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(), response.getOutputStream());
}
});
final byte[] data = new byte[512];
final CountDownLatch latch = new CountDownLatch(1);
OutputStreamContentProvider content = new OutputStreamContentProvider();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded() && result.getResponse().getStatus() == 200 && Arrays.equals(data, getContent()))
latch.countDown();
}
});
// Make sure we provide the content *after* the request has been "sent".
Thread.sleep(1000);
try (OutputStream output = content.getOutputStream()) {
output.write(data);
}
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.OutputStreamContentProvider in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithOutputStreamFailureToConnect.
@Test
public void testUploadWithOutputStreamFailureToConnect() throws Exception {
start(new EmptyServerHandler());
final byte[] data = new byte[512];
final CountDownLatch latch = new CountDownLatch(1);
OutputStreamContentProvider content = new OutputStreamContentProvider();
client.newRequest("0.0.0.1", connector.getLocalPort()).scheme(getScheme()).content(content).send(result -> {
if (result.isFailed())
latch.countDown();
});
try (OutputStream output = content.getOutputStream()) {
output.write(data);
Assert.fail();
} catch (IOException x) {
// Expected
}
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations