use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletTest method testResponseHeadersAreNotRemoved.
@Test
public void testResponseHeadersAreNotRemoved() throws Exception {
startServer(new EmptyHttpServlet());
startProxy();
proxyContext.stop();
final String headerName = "X-Test";
final String headerValue = "test-value";
proxyContext.addFilter(new FilterHolder(new Filter() {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
((HttpServletResponse) response).addHeader(headerName, headerValue);
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}), "/*", EnumSet.of(DispatcherType.REQUEST));
proxyContext.start();
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(headerValue, response.getHeaders().get(headerName));
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletTest method testTransparentProxyWithPrefix.
private void testTransparentProxyWithPrefix(String prefix) throws Exception {
final String target = "/test";
startServer(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
resp.setStatus(target.equals(req.getRequestURI()) ? 200 : 404);
}
});
String proxyTo = "http://localhost:" + serverConnector.getLocalPort();
proxyServlet = new ProxyServlet.Transparent();
Map<String, String> params = new HashMap<>();
params.put("proxyTo", proxyTo);
params.put("prefix", prefix);
startProxy(params);
startClient();
// Make the request to the proxy, it should transparently forward to the server
ContentResponse response = client.newRequest("localhost", proxyConnector.getLocalPort()).path((prefix + target).replaceAll("//", "/")).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletTest method testRedirectsAreProxied.
@Test
public void testRedirectsAreProxied() throws Exception {
startServer(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
resp.sendRedirect("/");
}
});
startProxy();
startClient();
client.setFollowRedirects(false);
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(302, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletTest method testProxyWithBigRequestContentIgnored.
@Test
public void testProxyWithBigRequestContentIgnored() throws Exception {
startServer(new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// Give some time to the proxy to
// upload the content to the server.
Thread.sleep(1000);
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
startProxy();
startClient();
byte[] content = new byte[128 * 1024];
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.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletTest method testGZIPContentIsProxied.
@Test
public void testGZIPContentIsProxied() throws Exception {
final byte[] content = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
startServer(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
resp.addHeader("Content-Encoding", "gzip");
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(resp.getOutputStream());
gzipOutputStream.write(content);
gzipOutputStream.close();
}
});
startProxy();
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
Assert.assertArrayEquals(content, response.getContent());
}
Aggregations