use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientRedirectTest method testRelativeLocation.
@Test
public void testRelativeLocation() throws Exception {
start(new RedirectHandler());
Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/localhost/done?relative=true").timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientSynchronizationTest method testSynchronizationOnException.
@Test
public void testSynchronizationOnException() throws Exception {
start(new EmptyServerHandler());
int port = connector.getLocalPort();
server.stop();
int count = 10;
final CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; ++i) {
Request request = client.newRequest("localhost", port).scheme(scheme);
synchronized (this) {
request.send(new Response.Listener.Adapter() {
@Override
public void onFailure(Response response, Throwable failure) {
synchronized (HttpClientSynchronizationTest.this) {
Assert.assertThat(failure, Matchers.instanceOf(ConnectException.class));
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 AsyncIOServletTest method testAsyncWriteLessThanContentLengthFlushed.
@Test
public void testAsyncWriteLessThanContentLengthFlushed() throws Exception {
CountDownLatch complete = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentLength(10);
AsyncContext async = request.startAsync();
ServletOutputStream out = response.getOutputStream();
AtomicInteger state = new AtomicInteger(0);
out.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
while (true) {
if (!out.isReady())
return;
switch(state.get()) {
case 0:
state.incrementAndGet();
WriteListener listener = this;
new Thread(() -> {
try {
Thread.sleep(50);
listener.onWritePossible();
} catch (Exception e) {
}
}).start();
return;
case 1:
state.incrementAndGet();
out.flush();
break;
case 2:
state.incrementAndGet();
out.write("12345".getBytes());
break;
case 3:
async.complete();
complete.countDown();
return;
}
}
}
@Override
public void onError(Throwable t) {
}
});
}
});
AtomicBoolean failed = new AtomicBoolean(false);
CountDownLatch clientLatch = new CountDownLatch(3);
client.newRequest(newURI()).path(servletPath).onResponseHeaders(response -> {
if (response.getStatus() == HttpStatus.OK_200)
clientLatch.countDown();
}).onResponseContent(new Response.ContentListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
// System.err.println("Content: "+BufferUtil.toDetailString(content));
}
}).onResponseFailure(new Response.FailureListener() {
@Override
public void onFailure(Response response, Throwable failure) {
clientLatch.countDown();
}
}).send(result -> {
failed.set(result.isFailed());
clientLatch.countDown();
clientLatch.countDown();
clientLatch.countDown();
});
assertTrue(complete.await(10, TimeUnit.SECONDS));
assertTrue(clientLatch.await(10, TimeUnit.SECONDS));
assertTrue(failed.get());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientLoadTest method test.
private void test(String scheme, String host, String method, boolean clientClose, boolean serverClose, int contentLength, final boolean checkContentLength, final CountDownLatch latch, final List<String> failures) {
long requestId = requestCount.incrementAndGet();
Request request = client.newRequest(host, connector.getLocalPort()).scheme(scheme).path("/" + requestId).method(method);
if (clientClose)
request.header(HttpHeader.CONNECTION, "close");
else if (serverClose)
request.header("X-Close", "true");
switch(method) {
case "GET":
request.header("X-Download", String.valueOf(contentLength));
break;
case "POST":
request.header("X-Upload", String.valueOf(contentLength));
request.content(new BytesContentProvider(new byte[contentLength]));
break;
}
final CountDownLatch requestLatch = new CountDownLatch(1);
request.send(new Response.Listener.Adapter() {
private final AtomicInteger contentLength = new AtomicInteger();
@Override
public void onHeaders(Response response) {
if (checkContentLength) {
String content = response.getHeaders().get("X-Content");
if (content != null)
contentLength.set(Integer.parseInt(content));
}
}
@Override
public void onContent(Response response, ByteBuffer content) {
if (checkContentLength)
contentLength.addAndGet(-content.remaining());
}
@Override
public void onComplete(Result result) {
if (result.isFailed()) {
result.getFailure().printStackTrace();
failures.add("Result failed " + result);
}
if (checkContentLength && contentLength.get() != 0)
failures.add("Content length mismatch " + contentLength);
requestLatch.countDown();
latch.countDown();
}
});
if (!await(requestLatch, 5, TimeUnit.SECONDS)) {
logger.warn("Request {} took too long{}{}{}{}", requestId, System.lineSeparator(), server.dump(), System.lineSeparator(), client.dump());
}
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientStreamTest method testDownloadWithCloseMiddleOfContent.
@Test(expected = AsynchronousCloseException.class)
public void testDownloadWithCloseMiddleOfContent() throws Exception {
final byte[] data1 = new byte[1024];
final byte[] data2 = new byte[1024];
final CountDownLatch latch = new CountDownLatch(1);
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(data1);
response.flushBuffer();
try {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new InterruptedIOException();
}
response.getOutputStream().write(data2);
}
});
InputStreamResponseListener listener = new InputStreamResponseListener();
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
InputStream input = listener.getInputStream();
Assert.assertNotNull(input);
for (byte datum1 : data1) Assert.assertEquals(datum1, input.read());
input.close();
latch.countDown();
// Throws
input.read();
}
Aggregations