Example usage for io.netty.handler.codec.http HttpUtil set100ContinueExpected

List of usage examples for io.netty.handler.codec.http HttpUtil set100ContinueExpected

Introduction

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

Prototype

public static void set100ContinueExpected(HttpMessage message, boolean expected) 

Source Link

Document

Sets or removes the "Expect: 100-continue" header to / from the specified message.

Usage

From source file:org.elasticsearch.http.netty4.Netty4HttpServerTransportTests.java

License:Apache License

/**
 * Test that {@link Netty4HttpServerTransport} supports the "Expect: 100-continue" HTTP header
 */// ww  w .  j a v  a  2s .com
public void testExpectContinueHeader() throws Exception {
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(Settings.EMPTY, networkService,
            bigArrays, threadPool)) {
        transport.httpServerAdapter((request, channel, context) -> channel.sendResponse(
                new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done"))));
        transport.start();
        InetSocketTransportAddress remoteAddress = (InetSocketTransportAddress) randomFrom(
                transport.boundAddress().boundAddresses());

        try (Netty4HttpClient client = new Netty4HttpClient()) {
            FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
            HttpUtil.set100ContinueExpected(request, true);
            HttpUtil.setContentLength(request, 10);

            FullHttpResponse response = client.post(remoteAddress.address(), request);
            assertThat(response.status(), is(HttpResponseStatus.CONTINUE));

            request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/",
                    Unpooled.EMPTY_BUFFER);
            response = client.post(remoteAddress.address(), request);
            assertThat(response.status(), is(HttpResponseStatus.OK));
            assertThat(new String(ByteBufUtil.getBytes(response.content()), StandardCharsets.UTF_8),
                    is("done"));
        }
    }
}