use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientTest method testPOSTWithContentTracksProgress.
@Test
public void testPOSTWithContentTracksProgress() throws Exception {
start(new EmptyServerHandler());
final AtomicInteger progress = new AtomicInteger();
ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).onRequestContent(new Request.ContentListener() {
@Override
public void onContent(Request request, ByteBuffer 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.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientTest method testPOSTWithContentNotifiesRequestContentListener.
@Test
public void testPOSTWithContentNotifiesRequestContentListener() throws Exception {
final byte[] content = { 0, 1, 2, 3 };
start(new EmptyServerHandler());
ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).onRequestContent(new Request.ContentListener() {
@Override
public void onContent(Request request, ByteBuffer buffer) {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
if (!Arrays.equals(content, bytes))
request.abort(new Exception());
}
}).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientTest method testPOSTWithParametersWithContent.
@Test
public void testPOSTWithParametersWithContent() throws Exception {
final byte[] content = { 0, 1, 2, 3 };
final String paramName = "a";
final String paramValue = "€";
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);
String value = request.getParameter(paramName);
if (paramValue.equals(value)) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
IO.copy(request.getInputStream(), response.getOutputStream());
}
}
});
for (int i = 0; i < 256; ++i) {
ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort() + "/?b=1").param(paramName, paramValue).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(content, response.getContent());
}
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_Before100Continue.
@Slow
@Test
public void test_Expect100Continue_WithContent_WithResponseFailure_Before100Continue() throws Exception {
final long idleTimeout = 1000;
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
try {
TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
} catch (InterruptedException x) {
throw new ServletException(x);
}
}
});
client.setIdleTimeout(idleTimeout);
byte[] content = new byte[1024];
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest(newURI()).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.assertNotNull(result.getResponseFailure());
latch.countDown();
}
});
Assert.assertTrue(latch.await(3 * idleTimeout, TimeUnit.MILLISECONDS));
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Expect100Continue_WithTwoResponsesInOneRead.
@Test
public void test_Expect100Continue_WithTwoResponsesInOneRead() throws Exception {
Assume.assumeThat(transport, Matchers.isOneOf(Transport.HTTP, Transport.HTTPS));
// There is a chance that the server replies with the 100 Continue response
// and immediately after with the "normal" response, say a 200 OK.
// These may be read by the client in a single read, and must be handled correctly.
startClient();
try (ServerSocket server = new ServerSocket()) {
server.bind(new InetSocketAddress("localhost", 0));
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", server.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(new byte[] { 0 })).send(result -> {
Assert.assertTrue(result.toString(), result.isSucceeded());
Assert.assertEquals(200, result.getResponse().getStatus());
latch.countDown();
});
try (Socket socket = server.accept()) {
// Read the request headers.
InputStream input = socket.getInputStream();
int crlfs = 0;
while (true) {
int read = input.read();
if (read == '\r' || read == '\n')
++crlfs;
else
crlfs = 0;
if (crlfs == 4)
break;
}
OutputStream output = socket.getOutputStream();
String responses = "" + "HTTP/1.1 100 Continue\r\n" + "\r\n" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "10\r\n" + "0123456789ABCDEF\r\n";
output.write(responses.getBytes(StandardCharsets.UTF_8));
output.flush();
Thread.sleep(1000);
String content = "" + "10\r\n" + "0123456789ABCDEF\r\n" + "0\r\n" + "\r\n";
output.write(content.getBytes(StandardCharsets.UTF_8));
output.flush();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
}
Aggregations