use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class ProxyServletTest method testProxyWithBigRequestContentConsumed.
@Test
public void testProxyWithBigRequestContentConsumed() throws Exception {
final byte[] content = new byte[128 * 1024];
new Random().nextBytes(content);
startServer(new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
InputStream input = req.getInputStream();
int index = 0;
byte[] buffer = new byte[16 * 1024];
while (true) {
int value = input.read(buffer);
if (value < 0)
break;
for (int i = 0; i < value; i++) {
Assert.assertEquals("Content mismatch at index=" + index, content[index] & 0xFF, buffer[i] & 0xFF);
++index;
}
}
}
});
startProxy();
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).method(HttpMethod.POST).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class ProxyServletTest method testExpect100ContinueRespond100Continue.
@Test
public void testExpect100ContinueRespond100Continue() throws Exception {
CountDownLatch serverLatch1 = new CountDownLatch(1);
CountDownLatch serverLatch2 = new CountDownLatch(1);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
serverLatch1.countDown();
try {
serverLatch2.await(5, TimeUnit.SECONDS);
} catch (Throwable x) {
throw new InterruptedIOException();
}
// Send the 100 Continue.
ServletInputStream input = request.getInputStream();
// Echo the content.
IO.copy(input, response.getOutputStream());
}
});
startProxy();
startClient();
byte[] content = new byte[1024];
CountDownLatch contentLatch = new CountDownLatch(1);
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).onRequestContent((request, buffer) -> contentLatch.countDown()).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded()) {
if (result.getResponse().getStatus() == HttpStatus.OK_200) {
if (Arrays.equals(content, getContent()))
clientLatch.countDown();
}
}
}
});
// Wait until we arrive on the server.
Assert.assertTrue(serverLatch1.await(5, TimeUnit.SECONDS));
// The client should not send the content yet.
Assert.assertFalse(contentLatch.await(1, TimeUnit.SECONDS));
// Make the server send the 100 Continue.
serverLatch2.countDown();
// The client has sent the content.
Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class ProxyServletTest method testExpect100ContinueRespond417ExpectationFailed.
@Test
public void testExpect100ContinueRespond417ExpectationFailed() throws Exception {
CountDownLatch serverLatch1 = new CountDownLatch(1);
CountDownLatch serverLatch2 = new CountDownLatch(1);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
serverLatch1.countDown();
try {
serverLatch2.await(5, TimeUnit.SECONDS);
} catch (Throwable x) {
throw new InterruptedIOException();
}
// Send the 417 Expectation Failed.
response.setStatus(HttpStatus.EXPECTATION_FAILED_417);
}
});
startProxy();
startClient();
byte[] content = new byte[1024];
CountDownLatch contentLatch = new CountDownLatch(1);
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).onRequestContent((request, buffer) -> contentLatch.countDown()).send(result -> {
if (result.isFailed()) {
if (result.getResponse().getStatus() == HttpStatus.EXPECTATION_FAILED_417)
clientLatch.countDown();
}
});
// Wait until we arrive on the server.
Assert.assertTrue(serverLatch1.await(5, TimeUnit.SECONDS));
// The client should not send the content yet.
Assert.assertFalse(contentLatch.await(1, TimeUnit.SECONDS));
// Make the server send the 417 Expectation Failed.
serverLatch2.countDown();
// The client should not send the content.
Assert.assertFalse(contentLatch.await(1, TimeUnit.SECONDS));
Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class ProxyServletFailureTest method testProxyRequestStallsContentServerIdlesTimeout.
@Test
public void testProxyRequestStallsContentServerIdlesTimeout() throws Exception {
final byte[] content = new byte[] { 'C', '0', 'F', 'F', 'E', 'E' };
int expected;
if (proxyServlet instanceof AsyncProxyServlet) {
// TODO should this be a 502 also???
expected = 500;
proxyServlet = new AsyncProxyServlet() {
@Override
protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
DeferredContentProvider provider = new DeferredContentProvider() {
@Override
public boolean offer(ByteBuffer buffer, Callback callback) {
// Send less content to trigger the test condition.
buffer.limit(buffer.limit() - 1);
return super.offer(buffer.slice(), callback);
}
};
request.getInputStream().setReadListener(newReadListener(request, response, proxyRequest, provider));
return provider;
}
};
} else {
expected = 502;
proxyServlet = new ProxyServlet() {
@Override
protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
return new BytesContentProvider(content) {
@Override
public long getLength() {
// Increase the content length to trigger the test condition.
return content.length + 1;
}
};
}
};
}
prepareProxy();
prepareServer(new EchoHttpServlet());
long idleTimeout = 1000;
serverConnector.setIdleTimeout(idleTimeout);
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(content)).send();
Assert.assertEquals(expected, response.getStatus());
}
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jersey by jersey.
the class JettyConnector method getBytesProvider.
private ContentProvider getBytesProvider(final ClientRequest clientRequest) {
final Object entity = clientRequest.getEntity();
if (entity == null) {
return null;
}
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {
@Override
public OutputStream getOutputStream(final int contentLength) throws IOException {
return outputStream;
}
});
try {
clientRequest.writeEntity();
} catch (final IOException e) {
throw new ProcessingException("Failed to write request entity.", e);
}
return new BytesContentProvider(outputStream.toByteArray());
}
Aggregations