Example usage for io.netty.handler.codec.http HttpMethod GET

List of usage examples for io.netty.handler.codec.http HttpMethod GET

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpMethod GET.

Prototype

HttpMethod GET

To view the source code for io.netty.handler.codec.http HttpMethod GET.

Click Source Link

Document

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.

Usage

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Tries different exception scenarios for {@link NettyResponseChannel#setRequest(NettyRequest)}.
 *///w w  w  .  j  a  va  2 s. co  m
@Test
public void setRequestTest() {
    HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.SetRequest.toString());
    HttpHeaders.setKeepAlive(request, false);
    EmbeddedChannel channel = createEmbeddedChannel();
    channel.writeInbound(request);

    HttpResponse response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.ACCEPTED, response.getStatus());
    assertFalse("Channel not closed on the server", channel.isActive());
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Tests setting of different available {@link ResponseStatus} codes and sees that they are recognized and converted
 * in {@link NettyResponseChannel}.//from   w w w.jav  a 2 s  .c  o m
 * <p/>
 * If this test fails, a case for conversion probably needs to be added in {@link NettyResponseChannel}.
 */
@Test
public void setStatusTest() {
    // ask for every status to be set
    for (ResponseStatus expectedResponseStatus : ResponseStatus.values()) {
        HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.SetStatus.toString());
        HttpHeaders.setHeader(request, MockNettyMessageProcessor.STATUS_HEADER_NAME, expectedResponseStatus);
        HttpHeaders.setKeepAlive(request, false);
        EmbeddedChannel channel = createEmbeddedChannel();
        channel.writeInbound(request);

        // pull but discard response
        channel.readOutbound();
        assertFalse("Channel not closed on the server", channel.isActive());
    }
    // check if all the ResponseStatus codes were recognized.
    String metricName = MetricRegistry.name(NettyResponseChannel.class, "UnknownResponseStatusCount");
    long metricCount = MockNettyMessageProcessor.METRIC_REGISTRY.getCounters().get(metricName).getCount();
    assertEquals("Some of the ResponseStatus codes were not recognized", 0, metricCount);
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Tests keep-alive for different HTTP methods and error statuses.
 *///from  w  ww. ja  v a2  s.co m
@Test
public void keepAliveTest() {
    HttpMethod[] HTTP_METHODS = { HttpMethod.POST, HttpMethod.GET, HttpMethod.HEAD, HttpMethod.DELETE };
    EmbeddedChannel channel = createEmbeddedChannel();
    for (HttpMethod httpMethod : HTTP_METHODS) {
        for (Map.Entry<RestServiceErrorCode, HttpResponseStatus> entry : REST_ERROR_CODE_TO_HTTP_STATUS
                .entrySet()) {
            HttpHeaders httpHeaders = new DefaultHttpHeaders();
            httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, entry.getKey());
            channel.writeInbound(RestTestUtils.createRequest(httpMethod,
                    TestingUri.OnResponseCompleteWithRestException.toString(), httpHeaders));
            HttpResponse response = (HttpResponse) channel.readOutbound();
            assertEquals("Unexpected response status", entry.getValue(), response.getStatus());
            if (!(response instanceof FullHttpResponse)) {
                // empty the channel
                while (channel.readOutbound() != null) {
                }
            }
            boolean shouldBeAlive = !httpMethod.equals(HttpMethod.POST)
                    && !NettyResponseChannel.CLOSE_CONNECTION_ERROR_STATUSES.contains(entry.getValue());
            assertEquals("Channel state (open/close) not as expected", shouldBeAlive, channel.isActive());
            assertEquals("Connection header should be consistent with channel state", shouldBeAlive,
                    HttpHeaders.isKeepAlive(response));
            if (!shouldBeAlive) {
                channel = createEmbeddedChannel();
            }
        }
    }
    channel.close();
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Tests that the underlying network channel is closed when {@link NettyResponseChannel#close()} is called.
 *//*from  w w w  .  j  a va  2s. c  om*/
@Test
public void closeTest() {
    // request is keep-alive by default.
    HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.Close.toString());
    EmbeddedChannel channel = createEmbeddedChannel();
    channel.writeInbound(request);

    // drain the channel of content.
    while (channel.readOutbound() != null) {
    }
    assertFalse("Channel should be closed", channel.isOpen());
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Creates a channel and sends a request (that induces an exception) to the {@link EmbeddedChannel}. Checks the
 * response for the {@code expectedResponseStatus}.
 * @param restServiceErrorCode the {@link RestServiceErrorCode} to set in the header. If {@code null}, the testing uri
 *                             {@link TestingUri#OnResponseCompleteWithNonRestException} is used. Otherwise the
 *                             testing uri {@link TestingUri#OnResponseCompleteWithRestException} is used.
 * @param expectedResponseStatus the {@link HttpResponseStatus} that is expected in the response.
 * @param shouldClose {@code true} if the channel should have been closed on this exception. {@code false} if not.
 *//*from  w  ww  . ja  v a 2  s  . c o  m*/
private void doOnResponseCompleteWithExceptionTest(RestServiceErrorCode restServiceErrorCode,
        HttpResponseStatus expectedResponseStatus, boolean shouldClose) {
    HttpHeaders httpHeaders = new DefaultHttpHeaders();
    TestingUri uri = TestingUri.OnResponseCompleteWithNonRestException;
    if (restServiceErrorCode != null) {
        uri = TestingUri.OnResponseCompleteWithRestException;
        httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, restServiceErrorCode);
    }
    EmbeddedChannel channel = createEmbeddedChannel();
    channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, uri.toString(), httpHeaders));

    HttpResponse response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status for " + restServiceErrorCode, expectedResponseStatus,
            response.getStatus());
    assertEquals("Channel state (open/close) not as expected for " + restServiceErrorCode, shouldClose,
            !channel.isActive());
    assertEquals("Connection header should be consistent with channel state for " + restServiceErrorCode,
            shouldClose, !HttpHeaders.isKeepAlive(response));
    channel.close();
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Creates a channel and sends the request to the {@link EmbeddedChannel}. Checks for an exception and verifies the
 * exception class matches. If {@code exceptionClass} is {@link RestServiceException}, then checks the provided
 * {@link RestServiceErrorCode}.//  w w w  .j av a 2 s.co m
 * @param uri the uri to hit.
 * @param exceptionClass the class of the exception expected.
 * @throws Exception
 */
private void doBadStateTransitionTest(TestingUri uri, Class exceptionClass) throws Exception {
    EmbeddedChannel channel = createEmbeddedChannel();
    try {
        channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, uri.toString(), null));
        fail("This test was expecting the handler in the channel to throw an exception");
    } catch (Exception e) {
        if (!exceptionClass.isInstance(e)) {
            throw e;
        }
    }
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Checks that idempotent operations do not throw exceptions when called multiple times. Does <b><i>not</i></b>
 * currently test that state changes are idempotent.
 * @param uri the uri to be hit.//w  ww. j a  va2  s  . c  om
 */
private void doIdempotentOperationsTest(TestingUri uri) {
    EmbeddedChannel channel = createEmbeddedChannel();
    // no exceptions.
    channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, uri.toString(), null));
    HttpResponse response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus());
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Checks that no exceptions are thrown by {@link RestResponseChannel#onResponseComplete(Exception)} when
 * there are write failures./*from   www  .  j  a va2 s. c  om*/
 * @param uri the uri to hit.
 */
private void onResponseCompleteUnderWriteFailureTest(TestingUri uri) {
    MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
    ExceptionOutboundHandler exceptionOutboundHandler = new ExceptionOutboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(exceptionOutboundHandler, processor);
    // no exception because onResponseComplete() swallows it.
    channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, uri.toString(), null));
    assertFalse("Channel is not closed at the remote end", channel.isActive());
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Tests for the common case request handling flow.
 * @throws IOException//  w  w  w .  j  ava 2 s  .  c  o  m
 */
@Test
public void requestHandleWithGoodInputTest() throws IOException {
    doRequestHandleTest(HttpMethod.POST, "POST", false);
    doRequestHandleTest(HttpMethod.GET, "GET", false);
    doRequestHandleTest(HttpMethod.DELETE, "DELETE", false);
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Tests for multiple requests with keep alive.
 * @throws IOException//www. j  a  va 2 s. c om
 */
@Test
public void requestHandleWithGoodInputTestWithKeepAlive() throws IOException {
    doRequestHandleWithKeepAliveTest(HttpMethod.POST, "POST");
    doRequestHandleWithKeepAliveTest(HttpMethod.GET, "GET");
    doRequestHandleWithKeepAliveTest(HttpMethod.DELETE, "DELETE");
}