Package org.apache.http.nio.protocol.HttpAsyncService

Examples of org.apache.http.nio.protocol.HttpAsyncService.State


        Mockito.verify(this.responseProducer, Mockito.never()).responseCompleted(exchangeContext);
    }

    @Test
    public void testResponseToHead() throws Exception {
        final State state = new State();
        final HttpContext exchangeContext = new BasicHttpContext();
        final HttpRequest request = new BasicHttpRequest("HEAD", "/", HttpVersion.HTTP_1_1);
        final Incoming incoming = new Incoming(
                request, this.requestHandler, this.requestConsumer, exchangeContext);
        state.setIncoming(incoming);
        state.setRequestState(MessageState.COMPLETED);
        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        final Outgoing outgoing = new Outgoing(
                request, response, this.responseProducer, exchangeContext);
        state.setOutgoing(outgoing);
        state.setResponseState(MessageState.INIT);
        this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);

        response.setEntity(new NStringEntity("stuff"));
        Mockito.when(this.responseProducer.generateResponse()).thenReturn(response);
        Mockito.when(this.reuseStrategy.keepAlive(response, exchangeContext)).thenReturn(Boolean.TRUE);

        this.protocolHandler.responseReady(this.conn);

        Assert.assertEquals(MessageState.READY, state.getResponseState());

        Mockito.verify(this.httpProcessor).process(response, exchangeContext);
        Mockito.verify(this.conn).submitResponse(response);
        Mockito.verify(this.responseProducer).responseCompleted(exchangeContext);
        Mockito.verify(this.conn).requestInput();
View Full Code Here


    @Test
    public void testConnected() throws Exception {
        this.protocolHandler.connected(this.conn);

        final State state = (State) this.connContext.getAttribute(
                HttpAsyncService.HTTP_EXCHANGE_STATE);
        Assert.assertNotNull(state);
        Assert.assertEquals(MessageState.READY, state.getRequestState());
        Assert.assertEquals(MessageState.READY, state.getResponseState());
        Assert.assertEquals("[incoming READY; outgoing READY]", state.toString());
    }
View Full Code Here

        Assert.assertEquals("[incoming READY; outgoing READY]", state.toString());
    }

    @Test
    public void testClosed() throws Exception {
        final State state = new State();
        state.setRequestState(MessageState.COMPLETED);
        state.setResponseState(MessageState.COMPLETED);
        final HttpContext exchangeContext = new BasicHttpContext();

        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        final Incoming incoming = new Incoming(
                request, this.requestHandler, this.requestConsumer, exchangeContext);
        state.setIncoming(incoming);
        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        final Outgoing outgoing = new Outgoing(
                request, response, this.responseProducer, exchangeContext);
        state.setOutgoing(outgoing);
        state.setCancellable(this.cancellable);
        this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);

        this.protocolHandler.closed(this.conn);

        Mockito.verify(this.requestConsumer).close();
View Full Code Here

        Mockito.verify(this.cancellable).cancel();
    }

    @Test
    public void testHttpExceptionHandling() throws Exception {
        final State state = new State();
        state.setRequestState(MessageState.READY);
        state.setResponseState(MessageState.READY);
        final HttpContext exchangeContext = new BasicHttpContext();
        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        final Incoming incoming = new Incoming(
                request, this.requestHandler, this.requestConsumer, exchangeContext);
        state.setIncoming(incoming);
        state.setCancellable(this.cancellable);
        this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);

        final HttpException httpex = new HttpException();
        this.protocolHandler.exception(this.conn, httpex);

        Assert.assertEquals(MessageState.READY, state.getRequestState());
        Assert.assertEquals(MessageState.BODY_STREAM, state.getResponseState());
        final Outgoing outgoing = state.getOutgoing();
        Assert.assertNotNull(outgoing);
        Assert.assertNotNull(outgoing.getProducer());
        Assert.assertNotNull(outgoing.getResponse());
        Assert.assertEquals(500, outgoing.getResponse().getStatusLine().getStatusCode());
View Full Code Here

        Mockito.verifyNoMoreInteractions(conn);
    }

    @Test
    public void testExceptionHandlingRuntimeException() throws Exception {
        final State state = new State();
        state.setRequestState(MessageState.READY);
        state.setResponseState(MessageState.READY);
        final HttpContext exchangeContext = new BasicHttpContext();
        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        final Incoming incoming = new Incoming(
                request, this.requestHandler, this.requestConsumer, exchangeContext);
        state.setIncoming(incoming);
        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        final Outgoing outgoing = new Outgoing(
                request, response, this.responseProducer, exchangeContext);
        state.setOutgoing(outgoing);
        state.setCancellable(this.cancellable);
        this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);

        Mockito.doThrow(new RuntimeException()).when(this.httpProcessor).process(
                Mockito.any(HttpResponse.class), Mockito.any(HttpContext.class));
        final HttpException httpex = new HttpException();
View Full Code Here

        }
    }

    @Test
    public void testHttpExceptionHandlingIOException() throws Exception {
        final State state = new State();
        state.setRequestState(MessageState.READY);
        state.setResponseState(MessageState.READY);
        final HttpContext exchangeContext = new BasicHttpContext();
        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        final Incoming incoming = new Incoming(
                request, this.requestHandler, this.requestConsumer, exchangeContext);
        state.setIncoming(incoming);
        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        final Outgoing outgoing = new Outgoing(
                request, response, this.responseProducer, exchangeContext);
        state.setOutgoing(outgoing);
        state.setCancellable(this.cancellable);
        this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);

        Mockito.doThrow(new IOException()).when(this.httpProcessor).process(
                Mockito.any(HttpResponse.class), Mockito.any(HttpContext.class));
        final HttpException httpex = new HttpException();
View Full Code Here

        Mockito.verify(this.cancellable).cancel();
    }

    @Test
    public void testHttpExceptionHandlingResponseSubmitted() throws Exception {
        final State state = new State();
        state.setRequestState(MessageState.READY);
        state.setResponseState(MessageState.READY);
        final HttpContext exchangeContext = new BasicHttpContext();
        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        final Incoming incoming = new Incoming(
                request, this.requestHandler, this.requestConsumer, exchangeContext);
        state.setIncoming(incoming);
        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        final Outgoing outgoing = new Outgoing(
                request, response, this.responseProducer, exchangeContext);
        state.setOutgoing(outgoing);
        this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);
        Mockito.when(this.conn.isResponseSubmitted()).thenReturn(Boolean.TRUE);

        final HttpException httpex = new HttpException();
        this.protocolHandler.exception(this.conn, httpex);

        Assert.assertEquals(MessageState.READY, state.getRequestState());
        Assert.assertEquals(MessageState.READY, state.getResponseState());
        Mockito.verify(this.conn).shutdown();
        Mockito.verify(this.requestConsumer).failed(httpex);
        Mockito.verify(this.requestConsumer).close();
        Mockito.verify(this.responseProducer).failed(httpex);
        Mockito.verify(this.responseProducer).close();
View Full Code Here

        Mockito.verify(this.responseProducer).close();
    }

    @Test
    public void testBasicRequest() throws Exception {
        final State state = new State();
        this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);

        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        Mockito.when(this.conn.getHttpRequest()).thenReturn(request);
        Mockito.when(this.requestHandler.processRequest(
                Mockito.eq(request), Mockito.any(HttpContext.class))).thenReturn(this.requestConsumer);
        Mockito.when(this.requestConsumer.getException()).thenReturn(null);
        final Object data = new Object();
        Mockito.when(this.requestConsumer.getResult()).thenReturn(data);

        this.protocolHandler.requestReceived(this.conn);

        Assert.assertEquals(MessageState.READY, state.getRequestState());
        Assert.assertEquals(MessageState.READY, state.getResponseState());

        final Incoming incoming = state.getIncoming();
        Assert.assertNull(incoming);

        final ArgumentCaptor<HttpContext> argumentCaptor = ArgumentCaptor.forClass(HttpContext.class);
        Mockito.verify(this.httpProcessor).process(Mockito.eq(request), argumentCaptor.capture());
        final HttpContext exchangeContext = argumentCaptor.getValue();
        Assert.assertNotNull(exchangeContext);

        Assert.assertSame(request, exchangeContext.getAttribute(HttpCoreContext.HTTP_REQUEST));
        Assert.assertSame(this.conn, exchangeContext.getAttribute(HttpCoreContext.HTTP_CONNECTION));

        Mockito.verify(this.requestConsumer).requestReceived(request);
        Mockito.verify(this.requestConsumer).requestCompleted(exchangeContext);
        Mockito.verify(this.conn).requestOutput();

        final PipelineEntry entry = state.getPipeline().poll();
        Assert.assertNotNull(entry);
        Assert.assertSame(request, entry.getRequest());
        Assert.assertSame(requestHandler, entry.getHandler());
        Assert.assertNotNull(entry.getResult());
        Assert.assertNull(entry.getException());
View Full Code Here

        Assert.assertNull(entry.getException());
    }

    @Test
    public void testRequestPipelineIfResponseInitiated() throws Exception {
        final State state = new State();
        state.setRequestState(MessageState.READY);
        state.setResponseState(MessageState.INIT);
        this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);

        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        Mockito.when(this.conn.getHttpRequest()).thenReturn(request);
        Mockito.when(this.requestHandler.processRequest(
                Mockito.eq(request), Mockito.any(HttpContext.class))).thenReturn(this.requestConsumer);
        Mockito.when(this.requestConsumer.getException()).thenReturn(null);
        final Object data = new Object();
        Mockito.when(this.requestConsumer.getResult()).thenReturn(data);

        this.protocolHandler.requestReceived(this.conn);

        Assert.assertEquals(MessageState.READY, state.getRequestState());
        Assert.assertEquals(MessageState.INIT, state.getResponseState());

        final Incoming incoming = state.getIncoming();
        Assert.assertNull(incoming);

        Mockito.verify(this.requestConsumer).requestReceived(request);
        Mockito.verify(this.requestConsumer).requestCompleted(Mockito.<HttpContext>any());
        Mockito.verify(this.requestHandler, Mockito.never()).handle(
                Mockito.any(),
                Mockito.any(HttpAsyncExchange.class),
                Mockito.any(HttpContext.class));

        Assert.assertFalse(state.getPipeline().isEmpty());
        final PipelineEntry entry = state.getPipeline().remove();
        Assert.assertSame(request, entry.getRequest());
        Assert.assertSame(data, entry.getResult());
    }
View Full Code Here

        Assert.assertSame(data, entry.getResult());
    }

    @Test
    public void testRequestPipelineIfPipelineNotEmpty() throws Exception {
        final State state = new State();
        state.setRequestState(MessageState.READY);
        state.setResponseState(MessageState.READY);

        final Queue<PipelineEntry> pipeline = state.getPipeline();

        final HttpContext exchangeContext = new BasicHttpContext();
        final HttpRequest pipelinedRequest = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        final PipelineEntry entry = new PipelineEntry(pipelinedRequest, pipelinedRequest,
                null, requestHandler, exchangeContext);
        pipeline.add(entry);

        this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);

        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        Mockito.when(this.conn.getHttpRequest()).thenReturn(request);
        Mockito.when(this.requestHandler.processRequest(
                Mockito.eq(request), Mockito.any(HttpContext.class))).thenReturn(this.requestConsumer);
        Mockito.when(this.requestConsumer.getException()).thenReturn(null);
        final Object data = new Object();
        Mockito.when(this.requestConsumer.getResult()).thenReturn(data);

        this.protocolHandler.requestReceived(this.conn);

        Assert.assertEquals(MessageState.READY, state.getRequestState());
        Assert.assertEquals(MessageState.READY, state.getResponseState());

        final Incoming incoming = state.getIncoming();
        Assert.assertNull(incoming);

        Mockito.verify(this.requestConsumer).requestReceived(request);
        Mockito.verify(this.requestConsumer).requestCompleted(Mockito.<HttpContext>any());
        Mockito.verify(this.requestHandler, Mockito.never()).handle(
                Mockito.any(),
                Mockito.any(HttpAsyncExchange.class),
                Mockito.any(HttpContext.class));

        Assert.assertFalse(state.getPipeline().isEmpty());
        final PipelineEntry entry1 = state.getPipeline().remove();
        Assert.assertSame(entry, entry1);
        final PipelineEntry entry2 = state.getPipeline().remove();
        Assert.assertSame(request, entry2.getRequest());
        Assert.assertSame(data, entry2.getResult());
    }
View Full Code Here

TOP

Related Classes of org.apache.http.nio.protocol.HttpAsyncService.State

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.