use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletTest method testClientExcludedHosts.
@Test
public void testClientExcludedHosts() 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");
}
});
startProxy();
startClient();
int port = serverConnector.getLocalPort();
client.getProxyConfiguration().getProxies().get(0).getExcludedAddresses().add("127.0.0.1:" + port);
// Try with a proxied host
ContentResponse response = client.newRequest("localhost", port).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
// Try again with an excluded host
response = client.newRequest("127.0.0.1", port).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(PROXIED_HEADER));
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletTest method testCachingProxy.
@Test
public void testCachingProxy() throws Exception {
final byte[] content = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
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.getOutputStream().write(content);
}
});
// Don't do this at home: this example is not concurrent, not complete,
// it is only used for this test and to verify that ProxyServlet can be
// subclassed enough to write your own caching servlet
final String cacheHeader = "X-Cached";
proxyServlet = new ProxyServlet() {
private Map<String, ContentResponse> cache = new HashMap<>();
private Map<String, ByteArrayOutputStream> temp = new HashMap<>();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ContentResponse cachedResponse = cache.get(request.getRequestURI());
if (cachedResponse != null) {
response.setStatus(cachedResponse.getStatus());
// Should copy headers too, but keep it simple
response.addHeader(cacheHeader, "true");
response.getOutputStream().write(cachedResponse.getContent());
} else {
super.service(request, response);
}
}
@Override
protected void onResponseContent(HttpServletRequest request, HttpServletResponse response, Response proxyResponse, byte[] buffer, int offset, int length, Callback callback) {
// Accumulate the response content
ByteArrayOutputStream baos = temp.get(request.getRequestURI());
if (baos == null) {
baos = new ByteArrayOutputStream();
temp.put(request.getRequestURI(), baos);
}
baos.write(buffer, offset, length);
super.onResponseContent(request, response, proxyResponse, buffer, offset, length, callback);
}
@Override
protected void onProxyResponseSuccess(HttpServletRequest request, HttpServletResponse response, Response proxyResponse) {
byte[] content = temp.remove(request.getRequestURI()).toByteArray();
ContentResponse cached = new HttpContentResponse(proxyResponse, content, null, null);
cache.put(request.getRequestURI(), cached);
super.onProxyResponseSuccess(request, response, proxyResponse);
}
};
startProxy();
startClient();
// First request
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());
// Second request should be cached
response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(cacheHeader));
Assert.assertArrayEquals(content, response.getContent());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletTest method testProxyBlackList.
@Test
public void testProxyBlackList() throws Exception {
startServer(new EmptyHttpServlet());
startProxy();
startClient();
int port = serverConnector.getLocalPort();
proxyServlet.getBlackListHosts().add("localhost:" + port);
// Try with the wrong host
ContentResponse response = client.newRequest("localhost", port).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(403, response.getStatus());
// Try again with the right host
response = client.newRequest("127.0.0.1", port).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletTest method testProxyWithoutContent.
@Test
public void testProxyWithoutContent() 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");
}
});
startProxy();
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals("OK", response.getReason());
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 testTransparentProxyWithQueryWithSpaces.
@Test
public void testTransparentProxyWithQueryWithSpaces() throws Exception {
final String target = "/test";
final String query = "a=1&b=2&c=1234%205678&d=hello+world";
startServer(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
if (target.equals(req.getRequestURI())) {
if (query.equals(req.getQueryString())) {
resp.setStatus(200);
return;
}
}
resp.setStatus(404);
}
});
String proxyTo = "http://localhost:" + serverConnector.getLocalPort();
String prefix = "/proxy";
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 + "?" + query).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
Aggregations