use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpClientSynchronizationTest method testSynchronizationOnComplete.
@Test
public void testSynchronizationOnComplete() throws Exception {
start(new EmptyServerHandler());
int count = 10;
final CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; ++i) {
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme);
synchronized (this) {
request.send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
synchronized (HttpClientSynchronizationTest.this) {
Assert.assertFalse(result.isFailed());
latch.countDown();
}
}
});
}
}
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpClientTest method testRequestListenerForMultipleEventsIsInvokedOncePerEvent.
@Test
public void testRequestListenerForMultipleEventsIsInvokedOncePerEvent() throws Exception {
start(new EmptyServerHandler());
final AtomicInteger counter = new AtomicInteger();
Request.Listener listener = new Request.Listener() {
@Override
public void onQueued(Request request) {
counter.incrementAndGet();
}
@Override
public void onBegin(Request request) {
counter.incrementAndGet();
}
@Override
public void onHeaders(Request request) {
counter.incrementAndGet();
}
@Override
public void onCommit(Request request) {
counter.incrementAndGet();
}
@Override
public void onContent(Request request, ByteBuffer content) {
// Should not be invoked
counter.incrementAndGet();
}
@Override
public void onFailure(Request request, Throwable failure) {
// Should not be invoked
counter.incrementAndGet();
}
@Override
public void onSuccess(Request request) {
counter.incrementAndGet();
}
};
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestQueued(listener).onRequestBegin(listener).onRequestHeaders(listener).onRequestCommit(listener).onRequestContent(listener).onRequestSuccess(listener).onRequestFailure(listener).listener(listener).send();
Assert.assertEquals(200, response.getStatus());
int expectedEventsTriggeredByOnRequestXXXListeners = 5;
int expectedEventsTriggeredByListener = 5;
int expected = expectedEventsTriggeredByOnRequestXXXListeners + expectedEventsTriggeredByListener;
Assert.assertEquals(expected, counter.get());
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpClientAuthenticationTest method test_BasicAuthentication_WithAuthenticationRemoved.
@Test
public void test_BasicAuthentication_WithAuthenticationRemoved() throws Exception {
startBasic(new EmptyServerHandler());
final AtomicReference<CountDownLatch> requests = new AtomicReference<>(new CountDownLatch(2));
Request.Listener.Adapter requestListener = new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.get().countDown();
}
};
client.getRequestListeners().add(requestListener);
AuthenticationStore authenticationStore = client.getAuthenticationStore();
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
BasicAuthentication authentication = new BasicAuthentication(uri, realm, "basic", "basic");
authenticationStore.addAuthentication(authentication);
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
ContentResponse response = request.timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
authenticationStore.removeAuthentication(authentication);
requests.set(new CountDownLatch(1));
request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
response = request.timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
Authentication.Result result = authenticationStore.findAuthenticationResult(request.getURI());
Assert.assertNotNull(result);
authenticationStore.removeAuthenticationResult(result);
requests.set(new CountDownLatch(1));
request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
response = request.timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(401, response.getStatus());
Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpClientAuthenticationTest method test_RequestFailsAfterResponse.
@Test
public void test_RequestFailsAfterResponse() throws Exception {
startBasic(new EmptyServerHandler());
AuthenticationStore authenticationStore = client.getAuthenticationStore();
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
BasicAuthentication authentication = new BasicAuthentication(uri, realm, "basic", "basic");
authenticationStore.addAuthentication(authentication);
CountDownLatch successLatch = new CountDownLatch(1);
CountDownLatch resultLatch = new CountDownLatch(1);
DeferredContentProvider content = new DeferredContentProvider();
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure").content(content).onResponseSuccess(response -> successLatch.countDown());
request.send(result -> {
if (result.isFailed() && result.getResponseFailure() == null)
resultLatch.countDown();
});
// Send some content to make sure the request is dispatched on the server.
content.offer(ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)));
// Wait for the response to arrive to
// the authentication protocol handler.
Thread.sleep(1000);
// Trigger request failure.
request.abort(new Exception());
// Verify that the response was successful, it's the request that failed.
Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpClientAuthenticationTest method test_Redirect_ThenBasicAuthentication.
@Test
public void test_Redirect_ThenBasicAuthentication() throws Exception {
startBasic(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
if (request.getRequestURI().endsWith("/redirect"))
response.sendRedirect(URIUtil.newURI(scheme, request.getServerName(), request.getServerPort(), "/secure", null));
}
});
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
client.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, realm, "basic", "basic"));
final CountDownLatch requests = new CountDownLatch(3);
Request.Listener.Adapter requestListener = new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.countDown();
}
};
client.getRequestListeners().add(requestListener);
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/redirect").timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(requests.await(5, TimeUnit.SECONDS));
client.getRequestListeners().remove(requestListener);
}
Aggregations