use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testProxyResponseWriteFails.
private void testProxyResponseWriteFails(final int writeCount) throws Exception {
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream output = response.getOutputStream();
output.write(new byte[512]);
output.flush();
output.write(new byte[512]);
}
});
startProxy(new AsyncMiddleManServlet() {
private int count;
@Override
protected void writeProxyResponseContent(ServletOutputStream output, ByteBuffer content) throws IOException {
if (++count < writeCount)
super.writeProxyResponseContent(output, content);
else
throw new IOException("explicitly_thrown_by_test");
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(502, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testAfterContentTransformerInputStreamReset.
private void testAfterContentTransformerInputStreamReset(final boolean overflow) throws Exception {
final byte[] data = new byte[] { 'c', 'o', 'f', 'f', 'e', 'e' };
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Write the content in two chunks.
int chunk = data.length / 2;
ServletOutputStream output = response.getOutputStream();
output.write(data, 0, chunk);
sleep(1000);
output.write(data, chunk, data.length - chunk);
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new AfterContentTransformer() {
{
setMaxInputBufferSize(overflow ? data.length / 2 : data.length * 2);
}
@Override
public boolean transform(Source source, Sink sink) throws IOException {
// Consume the stream once.
InputStream input = source.getInputStream();
IO.copy(input, IO.getNullStream());
// Reset the stream and re-read it.
input.reset();
IO.copy(input, sink.getOutputStream());
return true;
}
};
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertArrayEquals(data, response.getContent());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testAfterContentTransformer.
@Test
public void testAfterContentTransformer() throws Exception {
final String key0 = "id";
long value0 = 1;
final String key1 = "channel";
String value1 = "foo";
final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
}
});
final String key2 = "c";
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new AfterContentTransformer() {
@Override
public boolean transform(Source source, Sink sink) throws IOException {
InputStream input = source.getInputStream();
@SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(new InputStreamReader(input, "UTF-8"));
// Transform the object.
obj.put(key2, obj.remove(key1));
try (OutputStream output = sink.getOutputStream()) {
output.write(JSON.toString(obj).getBytes(StandardCharsets.UTF_8));
return true;
}
}
};
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
@SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(response.getContentAsString());
Assert.assertNotNull(obj);
Assert.assertEquals(2, obj.size());
Assert.assertEquals(value0, obj.get(key0));
Assert.assertEquals(value1, obj.get(key2));
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testDiscardUpstreamAndDownstreamKnownContentLengthGzipped.
@Test
public void testDiscardUpstreamAndDownstreamKnownContentLengthGzipped() throws Exception {
final byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// decode input stream thru gzip
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IO.copy(new GZIPInputStream(request.getInputStream()), bos);
// ensure decompressed is 0 length
Assert.assertEquals(0, bos.toByteArray().length);
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
response.getOutputStream().write(gzip(bytes));
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new GZIPContentTransformer(new DiscardContentTransformer());
}
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new GZIPContentTransformer(new DiscardContentTransformer());
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").content(new BytesContentProvider(gzip(bytes))).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(0, response.getContent().length);
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testAfterContentTransformerClosingFilesOnClientRequestException.
@Test
public void testAfterContentTransformerClosingFilesOnClientRequestException() throws Exception {
final Path targetTestsDir = prepareTargetTestsDir();
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IO.copy(request.getInputStream(), IO.getNullStream());
}
});
final CountDownLatch destroyLatch = new CountDownLatch(1);
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new AfterContentTransformer() {
{
setOverflowDirectory(targetTestsDir);
setMaxInputBufferSize(0);
setMaxOutputBufferSize(0);
}
@Override
public boolean transform(Source source, Sink sink) throws IOException {
IO.copy(source.getInputStream(), sink.getOutputStream());
return true;
}
@Override
public void destroy() {
super.destroy();
destroyLatch.countDown();
}
};
}
});
long idleTimeout = 1000;
proxyConnector.setIdleTimeout(idleTimeout);
startClient();
// Send only part of the content; the proxy will idle timeout.
final byte[] data = new byte[] { 'c', 'a', 'f', 'e' };
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(data) {
@Override
public long getLength() {
return data.length + 1;
}
}).timeout(5 * idleTimeout, TimeUnit.MILLISECONDS).send();
Assert.assertTrue(destroyLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
Assert.assertEquals(HttpStatus.REQUEST_TIMEOUT_408, response.getStatus());
}
Aggregations