use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTest method test_HEAD_With_ResponseContentLength.
@Test
public void test_HEAD_With_ResponseContentLength() throws Exception {
final int length = 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(new byte[length]);
}
});
// HEAD requests receive a Content-Length header, but do not
// receive the content so they must handle this case properly
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.HEAD).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(0, response.getContent().length);
// Perform a normal GET request to be sure the content is now read
response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(length, response.getContent().length);
}
use of org.eclipse.jetty.client.api.ContentResponse 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.ContentResponse 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.ContentResponse 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);
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ResponseNotifier method forwardSuccess.
public void forwardSuccess(List<Response.ResponseListener> listeners, Response response) {
notifyBegin(listeners, response);
for (Iterator<HttpField> iterator = response.getHeaders().iterator(); iterator.hasNext(); ) {
HttpField field = iterator.next();
if (!notifyHeader(listeners, response, field))
iterator.remove();
}
notifyHeaders(listeners, response);
if (response instanceof ContentResponse)
notifyContent(listeners, response, ByteBuffer.wrap(((ContentResponse) response).getContent()), Callback.NOOP);
notifySuccess(listeners, response);
}
Aggregations