Package org.apache.http

Examples of org.apache.http.ConnectionReuseStrategy


    }

    @Test
    public void testBasicProtocolDowngrade() throws Exception {
        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
        HttpParams params = new SyncBasicHttpParams();

        HttpService httpservice = new HttpService(
                httprocessor,
                connReuseStrategy,
                responseFactory,
                handlerResolver,
                params);
        HttpContext context = new BasicHttpContext();
        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
        HttpRequest request = new BasicHttpRequest("GET", "/", new HttpVersion(20, 45));
        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);

        httpservice.handleRequest(conn, context);

        Mockito.verify(responseFactory).newHttpResponse(HttpVersion.HTTP_1_1, 200, context);
    }
View Full Code Here


    }

    @Test
    public void testExecutionEntityEclosingRequest() throws Exception {
        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
        HttpParams params = new SyncBasicHttpParams();

        HttpService httpservice = new HttpService(
                httprocessor,
                connReuseStrategy,
                responseFactory,
                handlerResolver,
                params);
        HttpContext context = new BasicHttpContext();
        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
        InputStream instream = Mockito.mock(InputStream.class);
        InputStreamEntity entity = new InputStreamEntity(instream, -1);
        request.setEntity(entity);

        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);

        httpservice.handleRequest(conn, context);

        Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getStatusLine().getStatusCode());
View Full Code Here

    }

    @Test
    public void testExecutionEntityEclosingRequestWithExpectContinue() throws Exception {
        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
        HttpParams params = new SyncBasicHttpParams();

        HttpService httpservice = new HttpService(
                httprocessor,
                connReuseStrategy,
                responseFactory,
                handlerResolver,
                params);
        HttpContext context = new BasicHttpContext();
        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
        InputStream instream = Mockito.mock(InputStream.class);
        InputStreamEntity entity = new InputStreamEntity(instream, -1);
        request.setEntity(entity);

        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
        HttpResponse resp100 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 100, context)).thenReturn(resp100);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);

        httpservice.handleRequest(conn, context);

        Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getStatusLine().getStatusCode());
View Full Code Here

    }

    @Test
    public void testExecutionEntityEclosingRequestCustomExpectationVerifier() throws Exception {
        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
        HttpParams params = new SyncBasicHttpParams();

        HttpExpectationVerifier expectationVerifier = new HttpExpectationVerifier() {

            public void verify(
                    final HttpRequest request,
                    final HttpResponse response,
                    final HttpContext context) throws HttpException {
                response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
            }

        };

        HttpService httpservice = new HttpService(
                httprocessor,
                connReuseStrategy,
                responseFactory,
                handlerResolver,
                expectationVerifier,
                params);
        HttpContext context = new BasicHttpContext();
        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
        InputStream instream = Mockito.mock(InputStream.class);
        InputStreamEntity entity = new InputStreamEntity(instream, -1);
        request.setEntity(entity);

        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 100, context)).thenReturn(response);
        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);

        httpservice.handleRequest(conn, context);

        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
View Full Code Here

    }

    @Test
    public void testExecutionExceptionInCustomExpectationVerifier() throws Exception {
        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
        HttpExpectationVerifier expectationVerifier = Mockito.mock(HttpExpectationVerifier.class);
        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
        HttpParams params = new SyncBasicHttpParams();

        HttpService httpservice = new HttpService(
                httprocessor,
                connReuseStrategy,
                responseFactory,
                handlerResolver,
                expectationVerifier,
                params);
        HttpContext context = new BasicHttpContext();
        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
        InputStream instream = Mockito.mock(InputStream.class);
        InputStreamEntity entity = new InputStreamEntity(instream, -1);
        request.setEntity(entity);

        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
        HttpResponse resp100 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 100, context)).thenReturn(resp100);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(response);
        Mockito.doThrow(new HttpException("Oopsie")).when(expectationVerifier).verify(request, resp100, context);
        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);

        httpservice.handleRequest(conn, context);

        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
View Full Code Here

    }

    @Test
    public void testMethodNotSupported() throws Exception {
        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
        HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
        HttpParams params = new SyncBasicHttpParams();

        HttpService httpservice = new HttpService(
                httprocessor,
                connReuseStrategy,
                responseFactory,
                handlerResolver,
                params);
        HttpContext context = new BasicHttpContext();
        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
        HttpRequest request = new BasicHttpRequest("whatever", "/");

        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
        HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(error);
        Mockito.when(handlerResolver.lookup("/")).thenReturn(requestHandler);
        Mockito.doThrow(new MethodNotSupportedException("whatever")).when(
                requestHandler).handle(request, response, context);
        Mockito.when(connReuseStrategy.keepAlive(error, context)).thenReturn(false);

        httpservice.handleRequest(conn, context);

        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
View Full Code Here

    }

    @Test
    public void testUnsupportedHttpVersionException() throws Exception {
        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
        HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
        HttpParams params = new SyncBasicHttpParams();

        HttpService httpservice = new HttpService(
                httprocessor,
                connReuseStrategy,
                responseFactory,
                handlerResolver,
                params);
        HttpContext context = new BasicHttpContext();
        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
        HttpRequest request = new BasicHttpRequest("whatever", "/");

        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
        HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(error);
        Mockito.when(handlerResolver.lookup("/")).thenReturn(requestHandler);
        Mockito.doThrow(new UnsupportedHttpVersionException()).when(
                requestHandler).handle(request, response, context);
        Mockito.when(connReuseStrategy.keepAlive(error, context)).thenReturn(false);

        httpservice.handleRequest(conn, context);

        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
View Full Code Here

    }

    @Test
    public void testProtocolException() throws Exception {
        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
        HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
        HttpParams params = new SyncBasicHttpParams();

        HttpService httpservice = new HttpService(
                httprocessor,
                connReuseStrategy,
                responseFactory,
                handlerResolver,
                params);
        HttpContext context = new BasicHttpContext();
        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
        HttpRequest request = new BasicHttpRequest("whatever", "/");

        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
        HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(error);
        Mockito.when(handlerResolver.lookup("/")).thenReturn(requestHandler);
        Mockito.doThrow(new ProtocolException("oh, this world is wrong")).when(
                requestHandler).handle(request, response, context);
        Mockito.when(connReuseStrategy.keepAlive(error, context)).thenReturn(false);

        httpservice.handleRequest(conn, context);

        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
View Full Code Here

    }

    @Test
    public void testConnectionKeepAlive() throws Exception {
        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
        HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
        HttpParams params = new SyncBasicHttpParams();

        HttpService httpservice = new HttpService(
                httprocessor,
                connReuseStrategy,
                responseFactory,
                handlerResolver,
                params);
        HttpContext context = new BasicHttpContext();
        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
        HttpRequest request = new BasicHttpRequest("GET", "/");
        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
        Mockito.when(handlerResolver.lookup("/")).thenReturn(requestHandler);
        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(true);

        httpservice.handleRequest(conn, context);

        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
View Full Code Here

        HttpContext context = new BasicHttpContext(null);
       
        HttpHost host = new HttpHost("localhost", 8080);
       
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

        try {
           
            HttpEntity[] requestBodies = {
                    new StringEntity(
                            "This is the first test request", "UTF-8"),
                    new ByteArrayEntity(
                            "This is the second test request".getBytes("UTF-8")),
                    new InputStreamEntity(
                            new ByteArrayInputStream(
                                    "This is the third test request (will be chunked)"
                                    .getBytes("UTF-8")), -1)
            };
           
            for (int i = 0; i < requestBodies.length; i++) {
                if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(), host.getPort());
                    conn.bind(socket, params);
                }
                BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
                        "/servlets-examples/servlet/RequestInfoExample");
                request.setEntity(requestBodies[i]);
                System.out.println(">> Request URI: " + request.getRequestLine().getUri());

                context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
                request.setParams(params);
                httpexecutor.preProcess(request, httpproc, context);
                HttpResponse response = httpexecutor.execute(request, conn, context);
                response.setParams(params);
                httpexecutor.postProcess(response, httpproc, context);
               
                System.out.println("<< Response: " + response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
                System.out.println("==============");
                if (!connStrategy.keepAlive(response, context)) {
                    conn.close();
                } else {
                    System.out.println("Connection kept alive...");
                }
            }
View Full Code Here

TOP

Related Classes of org.apache.http.ConnectionReuseStrategy

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.