Search in sources :

Example 21 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class ProxyServletTest method testTransparentProxyWithoutPrefix.

@Test
public void testTransparentProxyWithoutPrefix() 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);
        }
    });
    final String proxyTo = "http://localhost:" + serverConnector.getLocalPort();
    proxyServlet = new ProxyServlet.Transparent();
    Map<String, String> initParams = new HashMap<>();
    initParams.put("proxyTo", proxyTo);
    startProxy(initParams);
    startClient();
    // Make the request to the proxy, it should transparently forward to the server
    ContentResponse response = client.newRequest("localhost", proxyConnector.getLocalPort()).path(target).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 22 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class ProxyServletTest method testTransparentProxyWithQuery.

private void testTransparentProxyWithQuery(String proxyToContext, String prefix, String target) throws Exception {
    final String query = "a=1&b=2";
    startServer(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (req.getHeader("Via") != null)
                resp.addHeader(PROXIED_HEADER, "true");
            String expectedURI = proxyToContext + target;
            if (expectedURI.isEmpty())
                expectedURI = "/";
            if (expectedURI.equals(req.getRequestURI())) {
                if (query.equals(req.getQueryString())) {
                    resp.setStatus(200);
                    return;
                }
            }
            resp.setStatus(404);
        }
    });
    String proxyTo = "http://localhost:" + serverConnector.getLocalPort() + proxyToContext;
    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));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 23 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class ProxyServletTest method testWrongContentLength.

@Test
public void testWrongContentLength() throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            byte[] message = "tooshort".getBytes("ascii");
            resp.setContentType("text/plain;charset=ascii");
            resp.setHeader("Content-Length", Long.toString(message.length + 1));
            resp.getOutputStream().write(message);
        }
    });
    startProxy();
    startClient();
    try {
        ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertThat(response.getStatus(), Matchers.greaterThanOrEqualTo(500));
    } catch (ExecutionException e) {
        Assert.assertThat(e.getCause(), Matchers.instanceOf(IOException.class));
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 24 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class ForwardProxyServerTest method testRequestTarget.

@Test
public void testRequestTarget() throws Exception {
    startServer(new AbstractConnectionFactory("http/1.1") {

        @Override
        public Connection newConnection(Connector connector, EndPoint endPoint) {
            return new AbstractConnection(endPoint, connector.getExecutor()) {

                @Override
                public void onOpen() {
                    super.onOpen();
                    fillInterested();
                }

                @Override
                public void onFillable() {
                    try {
                        // When using TLS, multiple reads are required.
                        ByteBuffer buffer = BufferUtil.allocate(1024);
                        int filled = 0;
                        while (filled == 0) filled = getEndPoint().fill(buffer);
                        Utf8StringBuilder builder = new Utf8StringBuilder();
                        builder.append(buffer);
                        String request = builder.toString();
                        // ProxyServlet will receive an absolute URI from
                        // the client, and convert it to a relative URI.
                        // The ConnectHandler won't modify what the client
                        // sent, which must be a relative URI.
                        Assert.assertThat(request.length(), Matchers.greaterThan(0));
                        if (serverSslContextFactory == null)
                            Assert.assertFalse(request.contains("http://"));
                        else
                            Assert.assertFalse(request.contains("https://"));
                        String response = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
                        getEndPoint().write(Callback.NOOP, ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8)));
                    } catch (Throwable x) {
                        x.printStackTrace();
                        close();
                    }
                }
            };
        }
    });
    startProxy();
    HttpClient httpClient = new HttpClient(newSslContextFactory());
    httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
    httpClient.start();
    try {
        ContentResponse response = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(serverSslContextFactory == null ? "http" : "https").method(HttpMethod.GET).path("/test").send();
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    } finally {
        httpClient.stop();
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) AbstractConnectionFactory(org.eclipse.jetty.server.AbstractConnectionFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) HttpClient(org.eclipse.jetty.client.HttpClient) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.io.Connection) EndPoint(org.eclipse.jetty.io.EndPoint) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 25 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class ForwardProxyTLSServerTest method testExternalProxy.

@Test
@Ignore("External Proxy Server no longer stable enough for testing")
public void testExternalProxy() throws Exception {
    // Free proxy server obtained from http://hidemyass.com/proxy-list/
    String proxyHost = "81.208.25.53";
    int proxyPort = 3128;
    try {
        new Socket(proxyHost, proxyPort).close();
    } catch (Throwable x) {
        Assume.assumeNoException(x);
    }
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.start();
    HttpClient httpClient = new HttpClient(newSslContextFactory());
    httpClient.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
    httpClient.start();
    try {
        ContentResponse response = httpClient.newRequest("https://www.google.com").timeout(20, TimeUnit.SECONDS).send();
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    } finally {
        httpClient.stop();
    }
}
Also used : HttpProxy(org.eclipse.jetty.client.HttpProxy) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Socket(java.net.Socket) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ContentResponse (org.eclipse.jetty.client.api.ContentResponse)436 Test (org.junit.Test)343 HttpServletRequest (javax.servlet.http.HttpServletRequest)204 IOException (java.io.IOException)166 HttpServletResponse (javax.servlet.http.HttpServletResponse)159 ServletException (javax.servlet.ServletException)150 Request (org.eclipse.jetty.client.api.Request)117 HttpClient (org.eclipse.jetty.client.HttpClient)109 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)92 HttpServlet (javax.servlet.http.HttpServlet)48 Properties (java.util.Properties)45 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)45 InterruptedIOException (java.io.InterruptedIOException)42 Request (org.eclipse.jetty.server.Request)39 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)38 CountDownLatch (java.util.concurrent.CountDownLatch)37 Test (org.testng.annotations.Test)30 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)29 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)28 HardCodedExecutionFactory (org.teiid.runtime.HardCodedExecutionFactory)27