use of org.eclipse.jetty.client.api.Response 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.api.Response 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.api.Response 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.api.Response 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());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamResponseListenerFailedWhileWaiting.
@Test
public void testInputStreamResponseListenerFailedWhileWaiting() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
byte[] data = new byte[1024];
response.setContentLength(data.length);
ServletOutputStream output = response.getOutputStream();
output.write(data);
}
});
CountDownLatch failedLatch = new CountDownLatch(1);
CountDownLatch contentLatch = new CountDownLatch(1);
InputStreamResponseListener listener = new InputStreamResponseListener() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
super.onContent(response, content, new Callback() {
@Override
public void failed(Throwable x) {
failedLatch.countDown();
callback.failed(x);
}
});
contentLatch.countDown();
}
};
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
// Wait until we get some content.
Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
// Abort the response.
response.abort(new Exception());
// Make sure that the callback has been invoked.
Assert.assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
}
Aggregations