use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientIdleTimeoutTest method testClientIdleTimeout.
@Test
public void testClientIdleTimeout() 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.equals("/timeout")) {
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
}
}
});
client.stop();
client.setIdleTimeout(idleTimeout);
client.start();
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest(newURI()).path("/timeout").send(result -> {
if (result.isFailed())
latch.countDown();
});
Assert.assertTrue(latch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
// Verify that after the timeout we can make another request.
ContentResponse response = client.newRequest(newURI()).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientIdleTimeoutTest method testRequestIdleTimeout.
@Test
public void testRequestIdleTimeout() 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.equals("/timeout")) {
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
}
}
});
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest(newURI()).path("/timeout").idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).send(result -> {
if (result.isFailed())
latch.countDown();
});
Assert.assertTrue(latch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
// Verify that after the timeout we can make another request.
ContentResponse response = client.newRequest(newURI()).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamResponseListenerClosedBeforeReading.
@Test(expected = AsynchronousCloseException.class)
public void testInputStreamResponseListenerClosedBeforeReading() 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());
}
});
InputStreamResponseListener listener = new InputStreamResponseListener();
InputStream stream = listener.getInputStream();
// Close the stream immediately.
stream.close();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(new BytesContentProvider(new byte[] { 0, 1, 2, 3 })).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatus());
// Throws
stream.read();
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithDeferredContentProviderRacingWithIterator.
@Test
public void testUploadWithDeferredContentProviderRacingWithIterator() 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 CountDownLatch latch = new CountDownLatch(1);
final byte[] data = new byte[512];
final AtomicReference<DeferredContentProvider> contentRef = new AtomicReference<>();
final DeferredContentProvider content = new DeferredContentProvider() {
@Override
public Iterator<ByteBuffer> iterator() {
return new Iterator<ByteBuffer>() {
// Data for the deferred content iterator:
// [0] => deferred
// [1] => deferred
// [2] => data
private final byte[][] iteratorData = new byte[3][];
private final AtomicInteger index = new AtomicInteger();
{
iteratorData[0] = null;
iteratorData[1] = null;
iteratorData[2] = data;
}
@Override
public boolean hasNext() {
return index.get() < iteratorData.length;
}
@Override
public ByteBuffer next() {
byte[] chunk = iteratorData[index.getAndIncrement()];
ByteBuffer result = chunk == null ? null : ByteBuffer.wrap(chunk);
if (index.get() < iteratorData.length) {
contentRef.get().offer(result == null ? BufferUtil.EMPTY_BUFFER : result);
contentRef.get().close();
}
return result;
}
@Override
public void remove() {
}
};
}
};
contentRef.set(content);
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();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientStreamTest method testDownloadWithCloseBeforeContent.
@Test(expected = AsynchronousCloseException.class)
public void testDownloadWithCloseBeforeContent() throws Exception {
final byte[] data = new byte[128 * 1024];
byte value = 3;
Arrays.fill(data, value);
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.flushBuffer();
try {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new InterruptedIOException();
}
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);
input.close();
latch.countDown();
// Throws
input.read();
}
Aggregations