use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class AsyncIOServletTest method testOnAllDataRead.
@Test
public void testOnAllDataRead() throws Exception {
String success = "SUCCESS";
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
response.flushBuffer();
AsyncContext async = request.startAsync();
async.setTimeout(5000);
ServletInputStream in = request.getInputStream();
ServletOutputStream out = response.getOutputStream();
in.setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
assertScope();
try {
sleep(1000);
if (!in.isReady())
throw new IllegalStateException();
if (in.read() != 'X')
throw new IllegalStateException();
if (!in.isReady())
throw new IllegalStateException();
if (in.read() != -1)
throw new IllegalStateException();
} catch (IOException x) {
throw new UncheckedIOException(x);
}
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
out.write(success.getBytes(StandardCharsets.UTF_8));
async.complete();
}
@Override
public void onError(Throwable t) {
assertScope();
t.printStackTrace();
async.complete();
}
});
}
});
byte[] data = "X".getBytes(StandardCharsets.UTF_8);
CountDownLatch clientLatch = new CountDownLatch(1);
DeferredContentProvider content = new DeferredContentProvider() {
@Override
public long getLength() {
return data.length;
}
};
client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(content).timeout(5, TimeUnit.SECONDS).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded()) {
Response response = result.getResponse();
String content = getContentAsString();
if (response.getStatus() == HttpStatus.OK_200 && success.equals(content))
clientLatch.countDown();
}
}
});
sleep(100);
content.offer(ByteBuffer.wrap(data));
content.close();
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientStreamTest method testDownloadWithCloseEndOfContent.
@Test
public void testDownloadWithCloseEndOfContent() throws Exception {
final byte[] data = new byte[1024];
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);
response.flushBuffer();
}
});
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 datum : data) Assert.assertEquals(datum, input.read());
// Read EOF
Assert.assertEquals(-1, input.read());
input.close();
// Must not throw
Assert.assertEquals(-1, input.read());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientStreamTest method testDownload.
@Test
public void testDownload() throws Exception {
final byte[] data = new byte[128 * 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);
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);
int length = 0;
while (input.read() == value) {
if (length % 100 == 0)
Thread.sleep(1);
++length;
}
Assert.assertEquals(data.length, length);
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 testInputStreamResponseListenerClosedWhileWaiting.
@Test
public void testInputStreamResponseListenerClosedWhileWaiting() throws Exception {
byte[] chunk1 = new byte[] { 0, 1 };
byte[] chunk2 = new byte[] { 2, 3 };
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.setContentLength(chunk1.length + chunk2.length);
ServletOutputStream output = response.getOutputStream();
output.write(chunk1);
output.flush();
output.write(chunk2);
}
});
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));
// Close the stream.
InputStream stream = listener.getInputStream();
stream.close();
// Make sure that the callback has been invoked.
Assert.assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithConcurrentServerCloseClosesStream.
@Test
public void testUploadWithConcurrentServerCloseClosesStream() throws Exception {
final CountDownLatch serverLatch = 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);
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
serverLatch.countDown();
}
});
final AtomicBoolean commit = new AtomicBoolean();
final CountDownLatch closeLatch = new CountDownLatch(1);
InputStream stream = new InputStream() {
@Override
public int read() throws IOException {
if (commit.get()) {
try {
Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
connector.stop();
return 0;
} catch (Throwable x) {
throw new IOException(x);
}
} else {
return connector.isStopped() ? -1 : 0;
}
}
@Override
public void close() throws IOException {
super.close();
closeLatch.countDown();
}
};
InputStreamContentProvider provider = new InputStreamContentProvider(stream, 1);
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(provider).onRequestCommit(request -> commit.set(true)).send(result -> {
Assert.assertTrue(result.isFailed());
completeLatch.countDown();
});
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
}
Aggregations