use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class HttpClientStreamTest method testDownloadWithCloseMiddleOfContent.
@Test(expected = AsynchronousCloseException.class)
public void testDownloadWithCloseMiddleOfContent() throws Exception {
final byte[] data1 = new byte[1024];
final byte[] data2 = new byte[1024];
final CountDownLatch latch = new CountDownLatch(1);
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);
response.getOutputStream().write(data1);
response.flushBuffer();
try {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new InterruptedIOException();
}
response.getOutputStream().write(data2);
}
});
InputStreamResponseListener listener = new InputStreamResponseListener();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
InputStream input = listener.getInputStream();
Assert.assertNotNull(input);
for (byte datum1 : data1) Assert.assertEquals(datum1, input.read());
input.close();
latch.countDown();
// Throws
input.read();
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamResponseListenerWithRedirect.
@Test
public void testInputStreamResponseListenerWithRedirect() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
if (target.startsWith("/303"))
response.sendRedirect("/200");
}
});
InputStreamResponseListener listener = new InputStreamResponseListener();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).path("/303").followRedirects(true).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Result result = listener.await(5, TimeUnit.SECONDS);
Assert.assertTrue(result.isSucceeded());
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamResponseListenerBufferedRead.
@Test
public void testInputStreamResponseListenerBufferedRead() throws Exception {
AtomicReference<AsyncContext> asyncContextRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
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);
asyncContextRef.set(request.startAsync());
latch.countDown();
}
});
InputStreamResponseListener listener = new InputStreamResponseListener();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).timeout(5, TimeUnit.SECONDS).send(listener);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
AsyncContext asyncContext = asyncContextRef.get();
Assert.assertNotNull(asyncContext);
Random random = new Random();
byte[] chunk = new byte[64];
random.nextBytes(chunk);
ServletOutputStream output = asyncContext.getResponse().getOutputStream();
output.write(chunk);
output.flush();
// Use a buffer larger than the data
// written to test that the read returns.
byte[] buffer = new byte[2 * chunk.length];
InputStream stream = listener.getInputStream();
int totalRead = 0;
while (totalRead < chunk.length) {
int read = stream.read(buffer);
Assert.assertTrue(read > 0);
totalRead += read;
}
asyncContext.complete();
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class HttpClientStreamTest method testDownloadWithFailure.
@Test
public void testDownloadWithFailure() throws Exception {
final byte[] data = new byte[64 * 1024];
byte value = 1;
Arrays.fill(data, value);
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);
// Say we want to send this much...
response.setContentLength(2 * data.length);
// ...but write only half...
response.getOutputStream().write(data);
// ...then shutdown output
baseRequest.getHttpChannel().getEndPoint().shutdownOutput();
}
});
InputStreamResponseListener listener = new InputStreamResponseListener();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
InputStream input = listener.getInputStream();
Assert.assertNotNull(input);
int length = 0;
try {
length = 0;
while (input.read() == value) {
if (length % 100 == 0)
Thread.sleep(1);
++length;
}
Assert.fail();
} catch (IOException x) {
// Expected.
}
Assert.assertThat(length, Matchers.lessThanOrEqualTo(data.length));
Result result = listener.await(5, TimeUnit.SECONDS);
Assert.assertNotNull(result);
Assert.assertTrue(result.isFailed());
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class HttpClientStreamTest method testDownloadOfUTF8Content.
@Test
public void testDownloadOfUTF8Content() throws Exception {
// UTF-8 representation of è
final byte[] data = new byte[] { (byte) 0xC3, (byte) 0xA8 };
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);
response.getOutputStream().write(data);
}
});
InputStreamResponseListener listener = new InputStreamResponseListener();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
InputStream input = listener.getInputStream();
Assert.assertNotNull(input);
for (byte b : data) {
int read = input.read();
Assert.assertTrue(read >= 0);
Assert.assertEquals(b & 0xFF, read);
}
Assert.assertEquals(-1, input.read());
Result result = listener.await(5, TimeUnit.SECONDS);
Assert.assertNotNull(result);
Assert.assertFalse(result.isFailed());
Assert.assertSame(response, result.getResponse());
}
Aggregations