Search in sources :

Example 31 with ContentResponse

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

the class AsyncMiddleManServletTest method testUpstreamTransformationThrowsBeforeCommittingProxyRequest.

@Test
public void testUpstreamTransformationThrowsBeforeCommittingProxyRequest() throws Exception {
    startServer(new EchoHttpServlet());
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
            return new ContentTransformer() {

                @Override
                public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
                    throw new NullPointerException("explicitly_thrown_by_test");
                }
            };
        }
    });
    startClient();
    byte[] bytes = new byte[1024];
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(bytes)).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(500, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.junit.Test)

Example 32 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse 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());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 33 with ContentResponse

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

the class ForwardProxyTLSServerTest method testProxyAuthentication.

private void testProxyAuthentication(String realm, ConnectHandler connectHandler, boolean includeAddress) throws Exception {
    startTLSServer(new ServerHandler());
    startProxy(connectHandler);
    HttpClient httpClient = new HttpClient(newSslContextFactory());
    HttpProxy httpProxy = newHttpProxy();
    if (includeAddress)
        httpProxy.getIncludedAddresses().add("localhost:" + serverConnector.getLocalPort());
    httpClient.getProxyConfiguration().getProxies().add(httpProxy);
    URI uri = URI.create((proxySslContextFactory == null ? "http" : "https") + "://localhost:" + proxyConnector.getLocalPort());
    httpClient.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, realm, "proxyUser", "proxyPassword"));
    httpClient.start();
    try {
        String host = "localhost";
        String body = "BODY";
        ContentResponse response = httpClient.newRequest(host, serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(body, "UTF-8")).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = response.getContentAsString();
        Assert.assertEquals(body, content);
    } finally {
        httpClient.stop();
    }
}
Also used : HttpProxy(org.eclipse.jetty.client.HttpProxy) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) URI(java.net.URI)

Example 34 with ContentResponse

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

the class ProxyServletFailureTest method testServerDown.

@Test
public void testServerDown() throws Exception {
    prepareProxy();
    prepareServer(new EmptyHttpServlet());
    // Shutdown the server
    int serverPort = serverConnector.getLocalPort();
    server.stop();
    ContentResponse response = client.newRequest("localhost", serverPort).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(502, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Test(org.junit.Test)

Example 35 with ContentResponse

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

the class AbstractModifyMaxInactiveIntervalTest method testSessionExpiryAfterModifiedMaxInactiveInterval.

@Test
public void testSessionExpiryAfterModifiedMaxInactiveInterval() throws Exception {
    int oldMaxInactive = 4;
    int newMaxInactive = 20;
    int sleep = oldMaxInactive + (int) (oldMaxInactive * 0.8);
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server = new TestServer(0, oldMaxInactive, __scavenge, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestModServlet.class, "/test");
    server.start();
    int port = server.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform a request to create a session
            ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //do another request to change the maxinactive interval
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //wait for longer than the old inactive interval
            Thread.currentThread().sleep(sleep * 1000L);
            //do another request using the cookie to ensure the session is still there
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) 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