use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class ConnectionStatisticsTest method testConnectionStatistics.
@Test
public void testConnectionStatistics() throws Exception {
Assume.assumeThat(transport, Matchers.isOneOf(Transport.H2C, Transport.H2));
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
ConnectionStatistics serverStats = new ConnectionStatistics();
connector.addBean(serverStats);
serverStats.start();
ConnectionStatistics clientStats = new ConnectionStatistics();
client.addBean(clientStats);
clientStats.start();
byte[] content = new byte[3072];
long contentLength = content.length;
ContentResponse response = client.newRequest(newURI()).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
// The bytes have already arrived, but give time to
// the server to finish to run the response logic.
Thread.sleep(1000);
// Close all connections.
stop();
// Give some time to process the stop event.
Thread.sleep(1000);
Assert.assertThat(serverStats.getConnectionsMax(), Matchers.greaterThan(0L));
Assert.assertThat(serverStats.getReceivedBytes(), Matchers.greaterThan(contentLength));
Assert.assertThat(serverStats.getSentBytes(), Matchers.greaterThan(contentLength));
Assert.assertThat(serverStats.getReceivedMessages(), Matchers.greaterThan(0L));
Assert.assertThat(serverStats.getSentMessages(), Matchers.greaterThan(0L));
Assert.assertThat(clientStats.getConnectionsMax(), Matchers.greaterThan(0L));
Assert.assertThat(clientStats.getReceivedBytes(), Matchers.greaterThan(contentLength));
Assert.assertThat(clientStats.getSentBytes(), Matchers.greaterThan(contentLength));
Assert.assertThat(clientStats.getReceivedMessages(), Matchers.greaterThan(0L));
Assert.assertThat(clientStats.getSentMessages(), Matchers.greaterThan(0L));
}
use of org.eclipse.jetty.client.util.BytesContentProvider 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.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientTest method testUploadWithoutResponseContent.
private void testUploadWithoutResponseContent(int length) throws Exception {
final byte[] bytes = new byte[length];
new Random().nextBytes(bytes);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ServletInputStream input = request.getInputStream();
for (int i = 0; i < bytes.length; ++i) Assert.assertEquals(bytes[i] & 0xFF, input.read());
Assert.assertEquals(-1, input.read());
}
});
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).content(new BytesContentProvider(bytes)).timeout(15, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(0, response.getContent().length);
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientTest method testClientManyWritesSlowServer.
@Test
public void testClientManyWritesSlowServer() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
long sleep = 1024;
long total = 0;
ServletInputStream input = request.getInputStream();
byte[] buffer = new byte[1024];
while (true) {
int read = input.read(buffer);
if (read < 0)
break;
total += read;
if (total >= sleep) {
sleep(250);
sleep += 256;
}
}
response.getOutputStream().print(total);
}
});
int chunks = 256;
int chunkSize = 16;
byte[][] bytes = IntStream.range(0, chunks).mapToObj(x -> new byte[chunkSize]).toArray(byte[][]::new);
BytesContentProvider contentProvider = new BytesContentProvider("application/octet-stream", bytes);
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).content(contentProvider).timeout(15, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals(chunks * chunkSize, Integer.parseInt(response.getContentAsString()));
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Redirect_WithExpect100Continue_WithContent.
@Test
public void test_Redirect_WithExpect100Continue_WithContent() throws Exception {
// A request with Expect: 100-Continue cannot receive non-final responses like 3xx
final String data = "success";
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
if (request.getRequestURI().endsWith("/done")) {
// Send 100-Continue and consume the content
IO.copy(request.getInputStream(), new ByteArrayOutputStream());
response.getOutputStream().print(data);
} else {
// Send a redirect
response.sendRedirect("/done");
}
}
});
byte[] content = new byte[10240];
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest(newURI()).method(HttpMethod.POST).path("/redirect").header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
Assert.assertNotNull(result.getRequestFailure());
Assert.assertNull(result.getResponseFailure());
Assert.assertEquals(302, result.getResponse().getStatus());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations