use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccessWithExplicitConnection.
@Slow
@Test
public void testTimeoutIsCancelledOnSuccessWithExplicitConnection() throws Exception {
long timeout = 1000;
start(new TimeoutHandler(timeout));
final CountDownLatch latch = new CountDownLatch(1);
Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
FuturePromise<Connection> futureConnection = new FuturePromise<>();
destination.newConnection(futureConnection);
try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).timeout(2 * timeout, TimeUnit.MILLISECONDS);
connection.send(request, new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Response response = result.getResponse();
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(result.isFailed());
latch.countDown();
}
});
Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
TimeUnit.MILLISECONDS.sleep(2 * timeout);
Assert.assertNull(request.getAbortCause());
}
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpCookieTest method test_CookieIsSent.
@Test
public void test_CookieIsSent() throws Exception {
final String name = "foo";
final String value = "bar";
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Cookie[] cookies = request.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.length);
Cookie cookie = cookies[0];
Assert.assertEquals(name, cookie.getName());
Assert.assertEquals(value, cookie.getValue());
}
});
String host = "localhost";
int port = connector.getLocalPort();
String path = "/path";
String uri = scheme + "://" + host + ":" + port;
HttpCookie cookie = new HttpCookie(name, value);
client.getCookieStore().add(URI.create(uri), cookie);
Response response = client.GET(scheme + "://" + host + ":" + port + path);
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpResponseAbortTest method testAbortOnContentBeforeRequestTermination.
@Test
public void testAbortOnContentBeforeRequestTermination() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
baseRequest.setHandled(true);
OutputStream output = response.getOutputStream();
output.write(1);
output.flush();
output.write(2);
output.flush();
} catch (IOException ignored) {
// The client may have already closed, and we'll get an exception here, but it's expected
}
}
});
final CountDownLatch abortLatch = new CountDownLatch(1);
final AtomicInteger completes = new AtomicInteger();
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestSuccess(new org.eclipse.jetty.client.api.Request.SuccessListener() {
@Override
public void onSuccess(org.eclipse.jetty.client.api.Request request) {
try {
abortLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException x) {
x.printStackTrace();
}
}
}).onResponseContent(new Response.ContentListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
try {
response.abort(new Exception());
abortLatch.countDown();
// Delay to let the request side to finish its processing.
Thread.sleep(1000);
} catch (InterruptedException x) {
x.printStackTrace();
}
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
completes.incrementAndGet();
Assert.assertTrue(result.isFailed());
completeLatch.countDown();
}
});
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
// Wait to be sure that the complete event is only notified once.
Thread.sleep(1000);
Assert.assertEquals(1, completes.get());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpResponseAbortTest method testAbortOnBegin.
@Test
public void testAbortOnBegin() throws Exception {
start(new EmptyServerHandler());
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseBegin(new Response.BeginListener() {
@Override
public void onBegin(Response response) {
response.abort(new Exception());
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpResponseAbortTest method testAbortOnHeaders.
@Test
public void testAbortOnHeaders() throws Exception {
start(new EmptyServerHandler());
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseHeaders(new Response.HeadersListener() {
@Override
public void onHeaders(Response response) {
response.abort(new Exception());
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations