use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testServerResponseContentKnownLengthGzipped.
@Test
public void testServerResponseContentKnownLengthGzipped() throws Exception {
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
final byte[] gzipBytes = gzip(bytes);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
response.getOutputStream().write(gzipBytes);
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new GZIPContentTransformer(ContentTransformer.IDENTITY);
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(bytes, response.getContent());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testTransformGzippedHead.
@Test
public void testTransformGzippedHead() throws Exception {
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
String sample = "<a href=\"http://webtide.com/\">Webtide</a>\n<a href=\"http://google.com\">Google</a>\n";
byte[] bytes = sample.getBytes(StandardCharsets.UTF_8);
ServletOutputStream out = response.getOutputStream();
out.write(gzip(bytes));
// create a byte buffer larger enough to create 2 (or more) transforms.
byte[] randomFiller = new byte[64 * 1024];
/* fill with nonsense
* Using random data to ensure compressed buffer size is large
* enough to trigger at least 2 transform() events.
*/
new Random().nextBytes(randomFiller);
out.write(gzip(randomFiller));
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new GZIPContentTransformer(new HeadTransformer());
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
String expectedStr = "<a href=\"http://webtide.com/\">Webtide</a>";
byte[] expected = expectedStr.getBytes(StandardCharsets.UTF_8);
Assert.assertArrayEquals(expected, response.getContent());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testDownstreamTransformationBufferedGzipped.
@Test
public void testDownstreamTransformationBufferedGzipped() throws Exception {
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
ServletInputStream input = request.getInputStream();
ServletOutputStream output = response.getOutputStream();
int read;
while ((read = input.read()) >= 0) {
output.write(read);
output.flush();
}
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new GZIPContentTransformer(new BufferingContentTransformer());
}
});
startClient();
byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
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.assertArrayEquals(bytes, response.getContent());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testDownstreamTransformationKnownContentLengthDroppingLastChunk.
@Test
public void testDownstreamTransformationKnownContentLengthDroppingLastChunk() throws Exception {
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] chunk = new byte[1024];
int contentLength = 2 * chunk.length;
response.setContentLength(contentLength);
ServletOutputStream output = response.getOutputStream();
output.write(chunk);
output.flush();
sleep(1000);
output.write(chunk);
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new ContentTransformer() {
@Override
public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
if (!finished)
output.add(input);
}
};
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method test_QueuedRequest_IsSent_WhenPreviousRequestSucceeded.
@Test
public void test_QueuedRequest_IsSent_WhenPreviousRequestSucceeded() throws Exception {
start(new EmptyServerHandler());
client.setMaxConnectionsPerDestination(1);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch successLatch = new CountDownLatch(2);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestBegin(request -> {
try {
latch.await();
} catch (InterruptedException x) {
x.printStackTrace();
}
}).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
Assert.assertEquals(200, response.getStatus());
successLatch.countDown();
}
});
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestQueued(request -> latch.countDown()).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
Assert.assertEquals(200, response.getStatus());
successLatch.countDown();
}
});
Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
Aggregations