use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method test_QueuedRequest_IsSent_WhenPreviousRequestClosedConnection.
@Test
public void test_QueuedRequest_IsSent_WhenPreviousRequestClosedConnection() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (target.endsWith("/one"))
baseRequest.getHttpChannel().getEndPoint().close();
else
baseRequest.setHandled(true);
}
});
client.setMaxConnectionsPerDestination(1);
try (StacklessLogging stackless = new StacklessLogging(org.eclipse.jetty.server.HttpChannel.class)) {
final CountDownLatch latch = new CountDownLatch(2);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/one").onResponseFailure((response, failure) -> latch.countDown()).send(null);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/two").onResponseSuccess(response -> {
Assert.assertEquals(200, response.getStatus());
latch.countDown();
}).send(null);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method testRequestSentOnlyAfterConnectionOpen.
@Test
public void testRequestSentOnlyAfterConnectionOpen() throws Exception {
startServer(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
}
});
final AtomicBoolean open = new AtomicBoolean();
client = new HttpClient(new HttpClientTransportOverHTTP() {
@Override
protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
return new HttpConnectionOverHTTP(endPoint, destination, promise) {
@Override
public void onOpen() {
open.set(true);
super.onOpen();
}
};
}
}, sslContextFactory);
client.start();
final CountDownLatch latch = new CountDownLatch(2);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestBegin(request -> {
Assert.assertTrue(open.get());
latch.countDown();
}).send(result -> {
if (result.isSucceeded())
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 HttpClientTest method test_POST_WithContent_TracksProgress.
@Test
public void test_POST_WithContent_TracksProgress() 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);
consume(request.getInputStream(), true);
}
});
final AtomicInteger progress = new AtomicInteger();
ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).onRequestContent((request, buffer) -> {
byte[] bytes = new byte[buffer.remaining()];
Assert.assertEquals(1, bytes.length);
buffer.get(bytes);
Assert.assertEquals(bytes[0], progress.getAndIncrement());
}).content(new BytesContentProvider(new byte[] { 0 }, new byte[] { 1 }, new byte[] { 2 }, new byte[] { 3 }, new byte[] { 4 })).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(5, progress.get());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method setOnCompleteCallbackWithBlockingSend.
@Test
public void setOnCompleteCallbackWithBlockingSend() throws Exception {
final byte[] content = new byte[512];
new Random().nextBytes(content);
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(content);
}
});
final Exchanger<Response> ex = new Exchanger<>();
BufferingResponseListener listener = new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
try {
ex.exchange(result.getResponse());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send(listener);
Response response = ex.exchange(null);
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(content, listener.getContent());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method test_GET_ResponseWithoutContent.
@Test
public void test_GET_ResponseWithoutContent() throws Exception {
start(new EmptyServerHandler());
Response response = client.GET(scheme + "://localhost:" + connector.getLocalPort());
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
}
Aggregations