use of org.eclipse.jetty.client.api.Result 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.Result 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.api.Result 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.Result 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.Result in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamResponseListenerFailedBeforeResponse.
@Test
public void testInputStreamResponseListenerFailedBeforeResponse() throws Exception {
start(new EmptyServerHandler());
int port = connector.getLocalPort();
server.stop();
InputStreamResponseListener listener = new InputStreamResponseListener();
// Connect to the wrong port
client.newRequest("localhost", port).scheme(getScheme()).send(listener);
Result result = listener.await(5, TimeUnit.SECONDS);
Assert.assertNotNull(result);
}
Aggregations