use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpRequestAbortTest method testAbortConversation.
@Test
public void testAbortConversation() 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);
if (!"/done".equals(request.getRequestURI()))
response.sendRedirect("/done");
}
});
// The test may fail to abort the request in this way:
// T1 aborts the request, which aborts the sender, which shuts down the output;
// server reads -1 and closes; T2 reads -1 and the receiver fails the response with an EOFException;
// T1 tries to abort the receiver, but it's already failed.
final Throwable cause = new Exception();
final AtomicBoolean aborted = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
client.getProtocolHandlers().clear();
client.getProtocolHandlers().put(new RedirectProtocolHandler(client) {
@Override
public void onComplete(Result result) {
// Abort the request after the 3xx response but before issuing the next request
if (!result.isFailed()) {
aborted.set(result.getRequest().abort(cause));
latch.countDown();
}
super.onComplete(result);
}
});
try {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/redirect").timeout(5, TimeUnit.SECONDS).send();
Assert.fail();
} catch (ExecutionException x) {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
if (aborted.get())
Assert.assertSame(cause, x.getCause());
}
}
use of org.eclipse.jetty.client.api.Result 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.Result 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));
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientChunkedContentTest method test_Server_HeadersPauseTerminal_Client_Response.
@Test
public void test_Server_HeadersPauseTerminal_Client_Response() throws Exception {
startClient();
try (ServerSocket server = new ServerSocket()) {
server.bind(new InetSocketAddress("localhost", 0));
final AtomicReference<Result> resultRef = new AtomicReference<>();
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", server.getLocalPort()).timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
resultRef.set(result);
completeLatch.countDown();
}
});
try (Socket socket = server.accept()) {
consumeRequestHeaders(socket);
OutputStream output = socket.getOutputStream();
String headers = "" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n";
output.write(headers.getBytes(StandardCharsets.UTF_8));
output.flush();
Thread.sleep(1000);
String terminal = "" + "0\r\n" + "\r\n";
output.write(terminal.getBytes(StandardCharsets.UTF_8));
output.flush();
assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
Result result = resultRef.get();
assertTrue(result.isSucceeded());
Response response = result.getResponse();
Assert.assertEquals(200, response.getStatus());
}
}
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientRedirectTest method testHttpRedirector.
@Test
public void testHttpRedirector() throws Exception {
start(new RedirectHandler());
final HttpRedirector redirector = new HttpRedirector(client);
org.eclipse.jetty.client.api.Request request1 = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/localhost/302/localhost/done").timeout(5, TimeUnit.SECONDS).followRedirects(false);
ContentResponse response1 = request1.send();
Assert.assertEquals(303, response1.getStatus());
Assert.assertTrue(redirector.isRedirect(response1));
Result result = redirector.redirect(request1, response1);
org.eclipse.jetty.client.api.Request request2 = result.getRequest();
Response response2 = result.getResponse();
Assert.assertEquals(302, response2.getStatus());
Assert.assertTrue(redirector.isRedirect(response2));
final CountDownLatch latch = new CountDownLatch(1);
redirector.redirect(request2, response2, r -> {
Response response3 = r.getResponse();
Assert.assertEquals(200, response3.getStatus());
Assert.assertFalse(redirector.isRedirect(response3));
latch.countDown();
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations