use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method test_ExchangeIsComplete_OnlyWhenBothRequestAndResponseAreComplete.
@Test
public void test_ExchangeIsComplete_OnlyWhenBothRequestAndResponseAreComplete() throws Exception {
start(new RespondThenConsumeHandler());
// Prepare a big file to upload
Path targetTestsDir = testdir.getEmptyPathDir();
Files.createDirectories(targetTestsDir);
Path file = Paths.get(targetTestsDir.toString(), "http_client_conversation.big");
try (OutputStream output = Files.newOutputStream(file, StandardOpenOption.CREATE)) {
byte[] kb = new byte[1024];
for (int i = 0; i < 10 * 1024; ++i) output.write(kb);
}
final CountDownLatch latch = new CountDownLatch(3);
final AtomicLong exchangeTime = new AtomicLong();
final AtomicLong requestTime = new AtomicLong();
final AtomicLong responseTime = new AtomicLong();
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).file(file).onRequestSuccess(request -> {
requestTime.set(System.nanoTime());
latch.countDown();
}).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
responseTime.set(System.nanoTime());
latch.countDown();
}
@Override
public void onComplete(Result result) {
exchangeTime.set(System.nanoTime());
latch.countDown();
}
});
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertTrue(requestTime.get() <= exchangeTime.get());
Assert.assertTrue(responseTime.get() <= exchangeTime.get());
// Give some time to the server to consume the request content
// This is just to avoid exception traces in the test output
Thread.sleep(1000);
Files.delete(file);
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method testCompleteNotInvokedUntilContentConsumed.
@Test
public void testCompleteNotInvokedUntilContentConsumed() 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);
ServletOutputStream output = response.getOutputStream();
output.write(new byte[1024]);
}
});
final AtomicReference<Callback> callbackRef = new AtomicReference<>();
final CountDownLatch contentLatch = new CountDownLatch(1);
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send(new Response.Listener.Adapter() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
// Do not notify the callback yet.
callbackRef.set(callback);
contentLatch.countDown();
}
@Override
public void onComplete(Result result) {
if (result.isSucceeded())
completeLatch.countDown();
}
});
Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
// Make sure the complete event is not emitted.
Assert.assertFalse(completeLatch.await(1, TimeUnit.SECONDS));
// Consume the content.
callbackRef.get().succeeded();
// Now the complete event is emitted.
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpCookieTest method test_CookieIsStored.
@Test
public void test_CookieIsStored() 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 {
response.addCookie(new Cookie(name, value));
baseRequest.setHandled(true);
}
});
String host = "localhost";
int port = connector.getLocalPort();
String path = "/path";
String uri = scheme + "://" + host + ":" + port + path;
Response response = client.GET(uri);
Assert.assertEquals(200, response.getStatus());
List<HttpCookie> cookies = client.getCookieStore().get(URI.create(uri));
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
Assert.assertEquals(name, cookie.getName());
Assert.assertEquals(value, cookie.getValue());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpResponseAbortTest method testAbortOnContent.
@Test
public void testAbortOnContent() 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 latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseContent(new Response.ContentListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
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 HttpClientAuthenticationTest method test_Authentication_ThrowsException.
@Test
public void test_Authentication_ThrowsException() throws Exception {
startBasic(new EmptyServerHandler());
// Request without Authentication would cause a 401,
// but the client will throw an exception trying to
// send the credentials to the server.
final String cause = "thrown_explicitly_by_test";
client.getAuthenticationStore().addAuthentication(new Authentication() {
@Override
public boolean matches(String type, URI uri, String realm) {
return true;
}
@Override
public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context) {
throw new RuntimeException(cause);
}
});
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure").timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
Assert.assertEquals(cause, result.getFailure().getMessage());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations