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

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

Introduction

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

Prototype

HttpMethod POST

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

Click Source Link

Document

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

Usage

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

License:Open Source License

/**
 * Tests the common workflow of the {@link NettyResponseChannel} i.e., add some content to response body via
 * {@link NettyResponseChannel#write(ByteBuffer, Callback)} and then completes the response.
 * <p/>/* w w w  .  j  a  va  2 s. c  om*/
 * For a description of what different URIs do, check {@link TestingUri}. For the actual functionality, check
 * {@link MockNettyMessageProcessor}).
 * @throws IOException
 */
@Test
public void commonCaseTest() throws IOException {
    String content = "@@randomContent@@@";
    String lastContent = "@@randomLastContent@@@";
    EmbeddedChannel channel = createEmbeddedChannel();
    AtomicLong requestIdGenerator = new AtomicLong(0);

    final int ITERATIONS = 5;
    for (int i = 0; i < 5; i++) {
        boolean isKeepAlive = i != (ITERATIONS - 1);
        String contentToSend = content + requestIdGenerator.getAndIncrement();
        HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null);
        HttpHeaders.setKeepAlive(httpRequest, isKeepAlive);
        channel.writeInbound(httpRequest);
        channel.writeInbound(createContent(contentToSend, false));
        channel.writeInbound(createContent(lastContent, true));
        // first outbound has to be response.
        HttpResponse response = (HttpResponse) channel.readOutbound();
        assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus());
        // content echoed back.
        String returnedContent = RestTestUtils.getContentString((HttpContent) channel.readOutbound());
        assertEquals("Content does not match with expected content", contentToSend, returnedContent);
        // last content echoed back.
        returnedContent = RestTestUtils.getContentString((HttpContent) channel.readOutbound());
        assertEquals("Content does not match with expected content", lastContent, returnedContent);
        assertTrue("Did not receive end marker", channel.readOutbound() instanceof LastHttpContent);
        assertEquals("Unexpected channel state on the server", isKeepAlive, channel.isActive());
    }
}

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  w w . j  a  v  a2s  .c om
@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.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Tests for the common case request handling flow.
 * @throws IOException//ww  w . j a va  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//from  w  w  w. j a v a  2 s.  c o m
 */
@Test
public void requestHandleWithGoodInputTestWithKeepAlive() throws IOException {
    doRequestHandleWithKeepAliveTest(HttpMethod.POST, "POST");
    doRequestHandleWithKeepAliveTest(HttpMethod.GET, "GET");
    doRequestHandleWithKeepAliveTest(HttpMethod.DELETE, "DELETE");
}

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

License:Open Source License

/**
 * Tests two successive request without completing first request
 * @throws IOException/*from www. jav  a 2s.  c  o m*/
 */
@Test
public void requestHandleWithTwoSuccessiveRequest() throws IOException {
    doRequestHandleWithMultipleRequest(HttpMethod.POST, "POST");
    doRequestHandleWithMultipleRequest(HttpMethod.GET, "GET");
    doRequestHandleWithMultipleRequest(HttpMethod.DELETE, "DELETE");
}

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

License:Open Source License

/**
 * Tests for the request handling flow for close
 * @throws IOException//from w  w  w. j av  a 2s. c  om
 */
@Test
public void requestHandleOnCloseTest() throws IOException {
    doRequestHandleTest(HttpMethod.POST, EchoMethodHandler.CLOSE_URI, true);
    doRequestHandleTest(HttpMethod.GET, EchoMethodHandler.CLOSE_URI, true);
    doRequestHandleTest(HttpMethod.DELETE, EchoMethodHandler.CLOSE_URI, true);
}

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

License:Open Source License

/**
 * Tests for the request handling flow on disconnect
 * @throws IOException/*  w ww  .j a  va2s .com*/
 */
@Test
public void requestHandleOnDisconnectTest() throws IOException {
    // disonnecting the embedded channel, calls close of PubliAccessLogRequestHandler
    doRequestHandleTest(HttpMethod.POST, EchoMethodHandler.DISCONNECT_URI, true);
    doRequestHandleTest(HttpMethod.GET, EchoMethodHandler.DISCONNECT_URI, true);
    doRequestHandleTest(HttpMethod.DELETE, EchoMethodHandler.DISCONNECT_URI, true);
}

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

License:Open Source License

/**
 * Tests for the request handling flow with transfer encoding chunked
 *///from  w w  w.j a va 2  s .  com
@Test
public void doRequestHandleWithChunkedResponse() throws IOException {
    EmbeddedChannel channel = createChannel();
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(EchoMethodHandler.IS_CHUNKED, "true");
    HttpRequest request = RestTestUtils.createRequest(HttpMethod.POST, "POST", headers);
    HttpHeaders.setKeepAlive(request, true);
    sendRequestCheckResponse(channel, request, "POST", headers, false, true);
    Assert.assertTrue("Channel should not be closed ", channel.isOpen());
    channel.close();
}

From source file:com.github.jonbonazza.puni.core.mux.DefaultMuxer.java

License:Apache License

public DefaultMuxer() {
    methodMap.put(HttpMethod.CONNECT, new HashMap<>());
    methodMap.put(HttpMethod.DELETE, new HashMap<>());
    methodMap.put(HttpMethod.GET, new HashMap<>());
    methodMap.put(HttpMethod.HEAD, new HashMap<>());
    methodMap.put(HttpMethod.OPTIONS, new HashMap<>());
    methodMap.put(HttpMethod.PATCH, new HashMap<>());
    methodMap.put(HttpMethod.POST, new HashMap<>());
    methodMap.put(HttpMethod.PUT, new HashMap<>());
    methodMap.put(HttpMethod.TRACE, new HashMap<>());
}

From source file:com.github.smallcreep.bmp.client.tests.TestProxyBMPClient.java

License:Apache License

@Test
public void testOverridesResponseAsResponseFilterAndListUrl() throws Throwable {
    Headers headersExpected = new Headers();
    List<String> accessControlAllowCredentialsList = new ArrayList<>();
    accessControlAllowCredentialsList.add("test");
    accessControlAllowCredentialsList.add("test2");
    headersExpected.put(ACCESS_CONTROL_ALLOW_CREDENTIALS, accessControlAllowCredentialsList);
    List<String> accessControlMaxAgeList = new ArrayList<>();
    accessControlMaxAgeList.add("test3");
    headersExpected.put(ACCESS_CONTROL_MAX_AGE, accessControlMaxAgeList);
    io.netty.handler.codec.http.HttpResponse responseOverrides = new DefaultFullHttpResponse(
            HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN);
    for (String headers : headersExpected.keySet()) {
        for (String headersValue : headersExpected.get(headers)) {
            responseOverrides.headers().add(headers, headersValue);
        }//from w  w w . j  a v a2  s  .  co  m
    }
    HttpMessageContents contents = new HttpMessageContents(
            new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN));
    contents.setTextContents("<html><body>Response successfully intercepted</body></html>");
    List<FilterUrls> filterUrls = new ArrayList<>();
    filterUrls.add(new FilterUrls("(.*)index\\.html(.*)"));
    filterUrls.add(new FilterUrls("^http:\\/\\/search\\.maven\\.org\\/$", HttpMethod.GET));
    filterUrls.add(new FilterUrls("(.*)test\\.html(.*)", HttpMethod.POST));
    BMPResponseFilter bmpResponseFilter = new BMPResponseFilter(responseOverrides, contents, null, filterUrls);
    getBmpLittleProxy().setFilterResponse(bmpResponseFilter);
    Unirest.setProxy(new HttpHost(getBmpLittleProxy().getAddress(), getBmpLittleProxy().getPort()));

    HttpResponse<String> response = Unirest.get(URL_PROTOCOL + URL_FOR_TEST).asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
    response = Unirest.post(URL_PROTOCOL + URL_FOR_TEST).asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);

    response = Unirest.get("http://search.maven.org/index.html").asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
    response = Unirest.post("http://search.maven.org/index.html").asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);

    response = Unirest.get("http://search.maven.org/test.html").asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
    response = Unirest.post("http://search.maven.org/test.html").asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);

    response = Unirest.get("http://search.maven.org/abracadabra.alibaba").asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
    response = Unirest.post("http://search.maven.org/abracadabra.alibaba").asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
}